{"id":123769,"date":"2015-04-22T10:31:20","date_gmt":"2015-04-22T08:31:20","guid":{"rendered":"http:\/\/guriddo.net\/?post_type=kbe_knowledgebase&#038;p=123769"},"modified":"2015-04-23T10:56:30","modified_gmt":"2015-04-23T08:56:30","slug":"using-templates-in-form-editing","status":"publish","type":"kbe_knowledgebase","link":"http:\/\/guriddo.net\/?kbe_knowledgebase=using-templates-in-form-editing","title":{"rendered":"Using templates in form editing"},"content":{"rendered":"<p>As of version 4.8 we support templates in the form editing. This allow to customize the edit form in a way the developer want. To use a template it is needed to set the parameter template in the edit add\/or add options of navigator. This can be done in navigator or in the editing method editGridRow :<\/p>\n<p>In navigator the code is:<\/p>\n<p>[js]$(&quot;#grid&quot;).jqGrid(&quot;navGrid&quot;,<br \/>\n    {add:true, edit:true,&#8230;},<br \/>\n    {template: &quot;template string for edit&quot;,&#8230;}<br \/>\n    {template: &quot;template string for add&quot;,&#8230;},<br \/>\n&#8230;<br \/>\n);<br \/>\n[\/js]<\/p>\n<p>and in editGridRow method:<\/p>\n<p>[js]<br \/>\n$(&quot;#grid&quot;).jqGrid(&quot;editGridRow&quot;,<br \/>\n   rowid,<br \/>\n   {template: &quot;template string&quot;,&#8230;}<br \/>\n);[\/js]<\/p>\n<p>where the &#8216;template string&#8217; is a string containing html tags and a special marks which points where the field from the grid should be.<\/p>\n<p>Note that the template for edit can be a different from the template from add operation. The template can be present only for add and only for edit operation.<\/p>\n<p>Let suppose that we have the following colModel (not all fields are present)<\/p>\n<p>[js]<br \/>\n$(&quot;grid&quot;).jqGrid({<br \/>\n&#8230;<br \/>\ncolModel: [<br \/>\n   {<br \/>\n      label: &#8216;Customer ID&#8217;,<br \/>\n      name: &#8216;CustomerID&#8217;,<br \/>\n      width: 75,<br \/>\n      key: true,<br \/>\n      editable: true,<br \/>\n      editrules : { required: true}<br \/>\n   },<br \/>\n   {<br \/>\n      label: &#8216;Company Name&#8217;,<br \/>\n      name: &#8216;CompanyName&#8217;,<br \/>\n      width: 140,<br \/>\n      editable: true \/\/ must set editable to true if you want to make the field editable<br \/>\n   },<br \/>\n   &#8230;<br \/>\n],<\/p>\n<p>&#8230;<\/p>\n<p>});[\/js]<\/p>\n<p>To place the CustomerID field in the template the following code string should be inserted in the template string <\/p>\n<p>[js]{CustomerID}[\/js]<\/p>\n<p>With other words this is the name from colModel put in { }.<\/p>\n<p>To place the Save and Cancel buttons we need to write a special code:<br \/>\nFor the save button this is {sData} and for the cancel button this is a {cData}<\/p>\n<p>The same apply for the navigator buttons for previous and next record.<br \/>\nFor previous button this is {pData} and for the next button this is a {nData}<\/p>\n<p>Below is the simple template which implements divs to represent the form.<\/p>\n<p>[js]<br \/>\nvar template = &quot;&lt;div style=&#8217;margin-left:15px;&#8217;&gt;&lt;div&gt; Customer ID &lt;sup&gt;*&lt;\/sup&gt;:&lt;\/div&gt;&lt;div&gt; {CustomerID} &lt;\/div&gt;&quot;;<br \/>\ntemplate += &quot;&lt;div&gt; Company Name: &lt;\/div&gt;&lt;div&gt;{CompanyName} &lt;\/div&gt;&quot;;<br \/>\ntemplate += &quot;&lt;div&gt; Phone: &lt;\/div&gt;&lt;div&gt;{Phone} &lt;\/div&gt;&quot;;<br \/>\ntemplate += &quot;&lt;div&gt; Postal Code: &lt;\/div&gt;&lt;div&gt;{PostalCode} &lt;\/div&gt;&quot;;<br \/>\ntemplate += &quot;&lt;div&gt; City:&lt;\/div&gt;&lt;div&gt; {City} &lt;\/div&gt;&quot;;<br \/>\ntemplate += &quot;&lt;hr style=&#8217;width:100%;&#8217;\/&gt;&quot;;<br \/>\ntemplate += &quot;&lt;div&gt; {sData} {cData}  &lt;\/div&gt;&lt;\/div&gt;&quot;;<\/p>\n<p>$(&quot;#jqGrid&quot;).jqGrid({<br \/>\n   url: &#8216;data.json&#8217;,<br \/>\n   \/\/ we set the changes to be made at client side using predefined word clientArray<br \/>\n   editurl: &#8216;clientArray&#8217;,<br \/>\n   datatype: &quot;json&quot;,<br \/>\n   colModel: [<br \/>\n      {  label: &#8216;Customer ID&#8217;,<br \/>\n         name: &#8216;CustomerID&#8217;,<br \/>\n         width: 75,<br \/>\n         key: true,<br \/>\n         editable: true,<br \/>\n         editrules : { required: true} },<br \/>\n      {  label: &#8216;Company Name&#8217;,<br \/>\n         name: &#8216;CompanyName&#8217;,<br \/>\n         width: 140,<br \/>\n         editable: true \/* must set editable to true if you want to make the field editable*\/ },<br \/>\n      {<br \/>\n         label : &#8216;Phone&#8217;,<br \/>\n         name: &#8216;Phone&#8217;,<br \/>\n         width: 100,<br \/>\n         editable: true<br \/>\n      },<br \/>\n      {<br \/>\n\t label: &#8216;Postal Code&#8217;,<br \/>\n         name: &#8216;PostalCode&#8217;,<br \/>\n         width: 80,<br \/>\n         editable: true<br \/>\n      },<br \/>\n      {<br \/>\n\t label: &#8216;City&#8217;,<br \/>\n         name: &#8216;City&#8217;,<br \/>\n         width: 140,<br \/>\n         editable: true<br \/>\n      }<br \/>\n   ],<br \/>\n   sortname: &#8216;CustomerID&#8217;,<br \/>\n   sortorder : &#8216;asc&#8217;,<br \/>\n   loadonce: true,<br \/>\n   viewrecords: true,<br \/>\n   width: 780,<br \/>\n   height: 200,<br \/>\n   rowNum: 10,<br \/>\n   pager: &quot;#jqGridPager&quot;<br \/>\n});<\/p>\n<p>$(&#8216;#jqGrid&#8217;).navGrid(&#8216;#jqGridPager&#8217;,<br \/>\n   \/\/ the buttons to appear on the toolbar of the grid<br \/>\n   { edit: true, add: true, del: true, search: false, refresh: false, view: false, position:    &quot;left&quot;, cloneToTop: false },<br \/>\n   \/\/options for the Edit Dialog<br \/>\n   {<br \/>\n      editCaption: &quot;The Edit Dialog&quot;,<br \/>\n      template: template,<br \/>\n      errorTextFormat: function (data) {<br \/>\n         return &#8216;Error: &#8216; + data.responseText<br \/>\n   }<br \/>\n   },<br \/>\n   \/\/ options for the Add Dialog<br \/>\n   {<br \/>\n      template: template,<br \/>\n      errorTextFormat: function (data) {<br \/>\n         return &#8216;Error: &#8216; + data.responseText<br \/>\n      }<br \/>\n   },<br \/>\n   \/\/ options for the Delete Dailog<br \/>\n   {<br \/>\n      errorTextFormat: function (data) {<br \/>\n         return &#8216;Error: &#8216; + data.responseText<br \/>\n      }<br \/>\n   } );<\/p>\n<p>[\/js]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As of version 4.8 we support templates in the form editing. This allow to customize the edit form in a way the developer want. To use a template it is needed to set the parameter template in the edit add\/or [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","kbe_taxonomy":[94,91],"kbe_tags":[],"_links":{"self":[{"href":"http:\/\/guriddo.net\/index.php?rest_route=\/wp\/v2\/kbe_knowledgebase\/123769"}],"collection":[{"href":"http:\/\/guriddo.net\/index.php?rest_route=\/wp\/v2\/kbe_knowledgebase"}],"about":[{"href":"http:\/\/guriddo.net\/index.php?rest_route=\/wp\/v2\/types\/kbe_knowledgebase"}],"author":[{"embeddable":true,"href":"http:\/\/guriddo.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/guriddo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=123769"}],"version-history":[{"count":1,"href":"http:\/\/guriddo.net\/index.php?rest_route=\/wp\/v2\/kbe_knowledgebase\/123769\/revisions"}],"predecessor-version":[{"id":123770,"href":"http:\/\/guriddo.net\/index.php?rest_route=\/wp\/v2\/kbe_knowledgebase\/123769\/revisions\/123770"}],"wp:attachment":[{"href":"http:\/\/guriddo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123769"}],"wp:term":[{"taxonomy":"kbe_taxonomy","embeddable":true,"href":"http:\/\/guriddo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fkbe_taxonomy&post=123769"},{"taxonomy":"kbe_tags","embeddable":true,"href":"http:\/\/guriddo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fkbe_tags&post=123769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}