{"id":1249,"date":"2015-10-30T11:48:48","date_gmt":"2015-10-30T16:48:48","guid":{"rendered":"http:\/\/xlinesoft.com\/blog\/?p=1249"},"modified":"2019-07-15T21:37:14","modified_gmt":"2019-07-16T02:37:14","slug":"excel-like-grid-in-phprunner-applications","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2015\/10\/30\/excel-like-grid-in-phprunner-applications\/","title":{"rendered":"Excel like grid in PHPRunner applications"},"content":{"rendered":"<p>Some applications may require to provide users with quick editing capabilities. While Inline Edit does just that entering inline edit mode for multiple records can be painful. It would be much easier is some or all fields appear as edit controls when page is loaded. <\/p>\n<p>While PHPRunner\/ASPRunner.NET\/ASPRunnerPro do not have such functionality built-in it&#8217;s fairly easy to implement it in your project. In this sample project we&#8217;ll show how to make fields ProductName, UnitPrice and Discontinued editable automatically. For now we only support text boxes and check boxes. Data is saved automatically once you leave the text box or check off check box. To see that data is actually saved in the database simply reload the page. <\/p>\n<p>You can also see how server-side validation works. Enter Unit Price that is less than $20 and move to the next field to see it in action. Record won&#8217;t be saved until you enter $20 or more price value. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2015\/10\/ExcelGrid1.png\" alt=\"\" title=\"ExcelGrid\" width=\"617\" height=\"370\" class=\"alignnone size-full wp-image-1253\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2015\/10\/ExcelGrid1.png 617w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2015\/10\/ExcelGrid1-300x179.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2015\/10\/ExcelGrid1-150x89.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2015\/10\/ExcelGrid1-400x239.png 400w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/p>\n<p><a target=_blank class=\"myButton\" href=\"http:\/\/xlinesoft.com\/livedemo\/ExcelGrid\/products_list.php\">Live demo<\/a><br \/>\n<!--more--><br \/>\nHere is how this can be done. <\/p>\n<h2>1. Enable &#8216;Inline Edit&#8217; for the table in question<\/h2>\n<p>If you do not need inline edit functionality feel free to remove the inline edit icon manually in Visual Editor. The option still needs to be turned on for this functionality to work.<\/p>\n<h2>2. &#8216;View as&#8217; Custom<\/h2>\n<p>For each field that you want to edit on the List page set &#8216;View as&#8217; type &#8216;Custom&#8217;. The code below is for PHPRunner only now but we&#8217;ll update it with ASP\/C# code shortly. <\/p>\n<h3>Check box field code<\/h3>\n<p>PHP:<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\n$field=\"Discontinued\";\r\n$keyField=\"ProductID\";\r\n\r\n$value = \"&lt;input data-fieldname='\".$field.\"' type=checkbox data-editid='\".\r\n$data[$keyField].\"' class='chAutoUpdate' \";\r\nif ($data[$field]) \r\n\t$value.=\"checked \";\r\n$value.=\">\";\r\n<\/pre>\n<p>C#:<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\ndynamic field = null, keyField = null;\r\n\t\t\tfield = new XVar(\"Discontinued\");\r\n\t\t\tkeyField = new XVar(\"ProductID\");\r\n\t\t\tvalue = XVar.Clone(MVCFunctions.Concat(\"<input data-fieldname='\", field, \"' type=checkbox data-editid='\", data[keyField], \"' class='chAutoUpdate' \"));\r\n\t\t\tif(XVar.Pack(data[field]))\r\n\t\t\t{\r\n\t\t\t\tvalue = MVCFunctions.Concat(value, \"checked \");\r\n\t\t\t}\r\n\t\t\tvalue = MVCFunctions.Concat(value, \">\");\r\n\t\t\treturn null;\r\n<\/pre>\n<h3>Text box field code<\/h3>\n<p>PHP:<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\n$field=\"ProductName\";\r\n$keyField=\"ProductID\";\r\n$value = \"&lt;input data-fieldname='\".$field.\"' data-editid='\".$data[$keyField].\"' \r\ntype=text class='txtAutoUpdate' value=\\\"\".runner_htmlspecialchars($data[$field]).\"\\\">\";\r\n<\/pre>\n<p>C#:<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\ndynamic field = null, keyField = null;\r\n\t\t\tfield = new XVar(\"ProductName\");\r\n\t\t\tkeyField = new XVar(\"ProductID\");\r\n\t\t\tvalue = XVar.Clone(MVCFunctions.Concat(\"<input data-fieldname='\", field, \"' data-editid='\", data[keyField], \"'  \\r\\ntype=text class='txtAutoUpdate' value=\\\"\", MVCFunctions.runner_htmlspecialchars((XVar)(data[field])), \"\\\">\"));\r\n\t\t\treturn null;\r\n<\/pre>\n<p>Obvious changes are field name and key column name. You can also change field styling, width etc. <\/p>\n<h2>3. List page Javascript OnLoad code<\/h2>\n<pre name=\"code\" class=\"javascript:nocontrols\">\r\nvar elem;\r\n$(document).ready(function() {\r\n $('.chAutoUpdate').change(function() {\r\n\tvar id = $(this).attr(\"data-editid\");\r\n\tvar field=$(this).attr(\"data-fieldname\");\r\n\tvar val=\"\";\t\r\n\telem = $(this);\t\r\n        if($(this).is(\":checked\")) {\r\n            val=\"on\";\r\n        }\r\n\r\n\tvar data = { \r\n\t\tid: 1,\r\n\t\teditType: \"inline\",\r\n\t\ta: \"edited\",\r\n\t\teditid1: id,\r\n                page: \"list\"\t \r\n\t};\r\n\tdata[\"value_\"+field+\"_1\"]=val;\r\n\tdata[\"type_\"+field+\"_1\"]=\"checkbox\";\r\n\r\n\t\/\/ save data\r\n\r\n\t$.ajax({\r\n\t  type: \"POST\",\r\n\t  url: \"products_edit.php?submit=1\",\r\n\t  data: data\r\n\t\t}).done(\tfunction(jsondata) {\r\n\t\t\tvar decoded = $('&lt;div\/>').html(jsondata).text();\r\n\t\t\tresponse = jQuery.parseJSON( decoded );\r\n\t\t\tif (response[\"success\"]==false) {\r\n\t\t\t\t$(\"&lt;div class=rnr-error\/>\").insertAfter(elem).html(response[\"message\"]);\t\r\n\t\t\t}\r\n\t\t});\r\n\t});\r\n\r\n $('.txtAutoUpdate').change(function() {\r\n\t\t\r\n\tvar id = $(this).attr(\"data-editid\");\r\n\tvar val=$(this).val();\t\t\r\n\tvar field=$(this).attr(\"data-fieldname\");\r\n\telem = $(this);\r\n\tvar data = { \r\n\t\tid: 1,\r\n\t\teditType: \"inline\",\r\n\t\ta: \"edited\",\r\n\t\teditid1: id,\r\n                page: \"list\"\t \r\n\t};\r\n\tdata[\"value_\"+field+\"_1\"]=val;\r\n\r\n\t\/\/ clear error message if any\r\n\tif ($(this).next().attr('class')==\"rnr-error\")\r\n\t\t\t$(this).next().remove();\r\n\r\n\t\/\/ save data\r\n\t$.ajax({\r\n\t  type: \"POST\",\r\n\t  url: \"products_edit.php?submit=1\",\r\n\t  data: data\r\n\t\t}).done(\tfunction(jsondata) {\r\n\t\t\tvar decoded = $('&lt;div\/>').html(jsondata).text();\r\n\t\t\tresponse = jQuery.parseJSON( decoded );\r\n\t\t\tif (response[\"success\"]==false) {\r\n\t\t\t\t$(\"&lt;div class=rnr-error\/>\").insertAfter(elem).html(response[\"message\"]);\t\r\n\t\t\t}\r\n\t\t});\r\n    });\r\n});\r\n<\/pre>\n<p>Changes in the code above: replace <strong>products_edit.php<\/strong> with the name of your edit page. <\/p>\n<h2>4. Validation part<\/h2>\n<p>This part is optional but we post it to make example complete. This code can be pasted to BeforeEdit event and we make sure that price is $20 or greater. <\/p>\n<p>PHP:<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\nif (array_key_exists(\"UnitPrice\", $values) ) {\r\n\tif ($values[\"UnitPrice\"]<20.0) {\r\n\t\t$message=\"Price should be greater than 20.0\";\r\n\t\treturn false;\r\n\t}\r\n}\r\nreturn true;\r\n<\/pre>\n<p>C#:<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\nif(XVar.Pack(values.KeyExists(\"UnitPrice\")))\r\n\t\t\t{\r\n\t\t\t\tif(values[\"UnitPrice\"] < 20.0)\r\n\t\t\t\t{\r\n\t\t\t\t\tmessage = new XVar(\"Price should be greater than 20.0\");\r\n\t\t\t\t\treturn false;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn true;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some applications may require to provide users with quick editing capabilities. While Inline Edit does just that entering inline edit mode for multiple records can be painful. It would be much easier is some or all fields appear as edit controls when page is loaded. While PHPRunner\/ASPRunner.NET\/ASPRunnerPro do not have such functionality built-in it&#8217;s fairly easy to implement it in your project. In this sample project we&#8217;ll show how to make fields ProductName, UnitPrice and Discontinued editable automatically. For now we only support text boxes&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2015\/10\/30\/excel-like-grid-in-phprunner-applications\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Excel like grid in PHPRunner applications&#8221;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,3,8],"tags":[],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1249"}],"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=1249"}],"version-history":[{"count":20,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1249\/revisions"}],"predecessor-version":[{"id":2014,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1249\/revisions\/2014"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}