{"id":1667,"date":"2017-07-25T10:36:07","date_gmt":"2017-07-25T15:36:07","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=1667"},"modified":"2017-08-25T06:03:57","modified_gmt":"2017-08-25T11:03:57","slug":"using-field-events","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2017\/07\/25\/using-field-events\/","title":{"rendered":"Using field events"},"content":{"rendered":"<p>We have two tables: categories and sub_categories. CategoryID is a two-digit category code.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_categories.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_categories-300x100.png\" alt=\"\" title=\"scr_categories\" width=\"300\" height=\"100\" class=\"alignnone size-medium wp-image-1674\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_categories-300x100.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_categories-150x50.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_categories-400x134.png 400w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_categories.png 772w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<!--more--><br \/>\nIn sub_categories table SubID consists of two-digit category code and and two-digit subcategory code. <\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories-300x189.png\" alt=\"\" title=\"scr_subcategories\" width=\"300\" height=\"189\" class=\"alignnone size-medium wp-image-1675\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories-300x189.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories-150x94.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories-400x252.png 400w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>What we need to do is to simplify adding a large number of subcategories. When user types in first two digit (category) we want to show category name underneath and also calculate the next subcategory number. We will do that using field events.<\/p>\n<p>This is how it works in generated application:<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories_animated.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories_animated.gif\" alt=\"\" title=\"scr_subcategories_animated\" width=\"748\" height=\"433\" class=\"alignnone size-full wp-image-1676\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories_animated.gif 748w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories_animated-300x173.gif 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories_animated-150x86.gif 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_subcategories_animated-400x231.gif 400w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><\/a><\/p>\n<h3>Code<\/h3>\n<p>First we need to create Field Event for SubID field. We will use &#8216;editing&#8217; event here which will be called every time the content of field is changed. <\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_field_event_code.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_field_event_code-300x231.png\" alt=\"\" title=\"scr_field_event_code\" width=\"300\" height=\"231\" class=\"alignnone size-medium wp-image-1686\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_field_event_code-300x231.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_field_event_code-150x115.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_field_event_code-400x308.png 400w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2017\/07\/scr_field_event_code.png 967w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>ClientBefore:<\/p>\n<pre name=\"code\" class=\"javascript:nocontrols\">\r\nvar val = this.getValue();\r\nif (val.length==2)\r\n  params[\"val\"]=val;\r\nelse\r\n return false;\r\n<\/pre>\n<p>In ClientBefore code we check the length of entered code and only send it to server side when code is two digits long. <\/p>\n<p>Server:<\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\n$result[\"catName\"] = DBLookup(\"select CategoryName from apps_categories where CategoryID='\".$params[\"val\"].\"'\");\r\n$sub = DBLookup(\"select max(substring(SubID,3,2))+1 from apps_sub_categories where left(SubID,2)='\".$params[\"val\"].\"'\");\r\n$result[\"newCat\"] = $params[\"val\"].str_pad($sub,2,\"0\",STR_PAD_LEFT);\r\n<\/pre>\n<p><strong>C#<\/strong><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nresult[\"catName\"] = tDAL.DBLookup(\"select CategoryName from categories where CategoryID='\" + parameters[\"val\"].ToString() + \"'\");\r\n\r\nstring sub = tDAL.DBLookup(\"select max(substring(SubID,3,2))+1 from sub_categories where left(SubID,2)='\" + parameters[\"val\"].ToString() + \"'\");\r\n\r\nresult[\"newCat\"] = parameters[\"val\"].ToString() + sub.ToString().PadLeft(2, '0');\r\n<\/pre>\n<p><strong>ASP<\/strong><\/p>\n<pre name=\"code\" class=\"vb:nocontrols\">\r\nresult(\"catName\") = DBLookup(\"select CategoryName from categories where CategoryID='\" & params(\"val\") & \"'\")\r\nres = DBLookup(\"select max(substring(SubID,3,2))+1 from sub_categories where left(SubID,2)='\" & params(\"val\") & \"'\")\r\nif len(res)<2 then\r\nres = \"0\" &#038; res\r\nend if\r\nresult(\"newCat\") = params(\"val\") &#038; res\r\n<\/pre>\n<p>On Server side we pull CategoryName from categories table and also calculate next subcategory ID. We pass both category name and new subcategory code back to ClientAfter event. <\/p>\n<p>ClientAfter:<\/p>\n<pre name=\"code\" class=\"javascript:nocontrols\">\r\n$(\"#sub_tip\").remove();\r\n$(\"input[id=value_SubID_1]\").after(\"&lt;div id='sub_tip' style='margin-top: 10px; color: blue;'>\"+result[\"catName\"]+\"&lt;\/div>\");\r\nctrl.setValue(result[\"newCat\"]);\r\n<\/pre>\n<p>In this event we remove previous tooltip with category name and add a new one. We also make it appear in blue color. Then we set the value of SubID with new subcategory code we received from the Server event. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have two tables: categories and sub_categories. CategoryID is a two-digit category code.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,8],"tags":[],"class_list":["post-1667","post","type-post","status-publish","format-standard","hentry","category-asp-net","category-php-category","category-tutorials"],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1667"}],"version-history":[{"count":18,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1667\/revisions"}],"predecessor-version":[{"id":1692,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1667\/revisions\/1692"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}