{"id":3133,"date":"2015-12-15T10:00:03","date_gmt":"2015-12-15T06:00:03","guid":{"rendered":"http:\/\/www.dhtmlx.com\/blog\/?p=3133"},"modified":"2023-05-05T13:28:17","modified_gmt":"2023-05-05T10:28:17","slug":"dhtmlx-little-known-features-custom-column-type-in-grid","status":"publish","type":"post","link":"https:\/\/dhtmlx.com\/blog\/dhtmlx-little-known-features-custom-column-type-in-grid\/","title":{"rendered":"DHTMLX Little-Known Features: Custom Column Type in Grid"},"content":{"rendered":"<p>In this article, we would like to continue to share with you small tips and tricks that allow you use DHTMLX components more efficiently and enjoy every single minute you spend with our library.<\/p>\n<p>This time, we&#8217;ll talk about dhtmlxGrid. It&#8217;s a full-featured <a href=\"\/\/dhtmlx.com\/docs\/products\/dhtmlxGrid\/\"> javascript data grid<\/a> control that provides advanced functionality, powerful data binding, and fast performance with large data sets. This grid component gives you a possibility to specify the content type for a column. There is a plenty of available column types from a simple editable text field to date picker. But what if provided functionality is not enough for you? Well, it&#8217;s always possible to use custom column type. Let&#8217;s look at some examples.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-3.png\" alt=\"Grid custom columns\" width=\"600\" height=\"233\" class=\"aligncenter size-full wp-image-3139\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-3.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-3-300x117.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><!--more--><\/p>\n<p>First of all, let&#8217;s talk about the basic initialization. You can check this <a href=\"https:\/\/docs.dhtmlx.com\/grid__basic_initialization.html\" target=\"_blank\">documentation page<\/a> for the details, but for now, this initialization sequence is what we need:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">myGrid <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> dhtmlXGridObject<span class=\"br0\">&#40;<\/span><span class=\"st0\">'gridbox'<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmyGrid.<span class=\"me1\">setImagePath<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;.\/codebase\/imgs\/&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmyGrid.<span class=\"me1\">setHeader<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Sales, Book Title, Author, Price&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmyGrid.<span class=\"me1\">setInitWidths<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;70,230,230, *&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmyGrid.<span class=\"me1\">setColAlign<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;right,left,left,right&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmyGrid.<span class=\"me1\">setColTypes<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;dyn,ed,ed,price&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmyGrid.<span class=\"me1\">setColSorting<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;int,str,str,int&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmyGrid.<span class=\"me1\">init<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmyGrid.<span class=\"me1\">load<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;.\/data.json&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;json&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>What we&#8217;re particularly interested in is this line of code:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">myGrid.<span class=\"me1\">setColTypes<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;dyn,ed,ed,price&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>This is where you define what column types you want to use. In this case we&#8217;ve created one dyn column that applies different coloring and marking based on the value, two simple column with editable cells that can be created using the ed parameter, and a price column that treats data as price.<\/p>\n<p>The result is shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-11.png\" alt=\"Grid custom columns\" width=\"600\" height=\"275\" class=\"aligncenter size-full wp-image-3143\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-11.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-11-300x138.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>As you can see, the dollar sign is used by default in the Price column. In case, you already have an array of data you want to use and such behavior is not what you expected, you can create your own column type.<\/p>\n<p>Let&#8217;s start from read-only cells. For example, we want use euros as a currency. Here\u2019s the code:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\"><span class=\"kw1\">function<\/span> eXcell_myPrice<span class=\"br0\">&#40;<\/span>cell<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> <span class=\"co1\">\/\/the eXcell name is defined here<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>cell<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"co1\">\/\/ the default pattern, just copy it<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">cell<\/span> <span class=\"sy0\">=<\/span> cell<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">grid<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">this<\/span>.<span class=\"me1\">cell<\/span>.<span class=\"me1\">parentNode<\/span>.<span class=\"me1\">grid<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"coMULTI\">\/* read-only cell doesn't have edit method *\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">edit<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span> &nbsp;<br \/>\n&nbsp; &nbsp; <span class=\"coMULTI\">\/* the cell is read-only, so it's always disabled *\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">isDisabled<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> <span class=\"kw1\">return<\/span> <span class=\"kw2\">true<\/span><span class=\"sy0\">;<\/span> <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">setValue<\/span><span class=\"sy0\">=<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>val<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"coMULTI\">\/* actual data processing *\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">setCValue<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;span&gt;&amp;#8364; &lt;\/span&gt;&lt;span&gt;&quot;<\/span><span class=\"sy0\">+<\/span>val<span class=\"sy0\">+<\/span><span class=\"st0\">&quot;&lt;\/span&gt;&quot;<\/span><span class=\"sy0\">,<\/span>val<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"coMULTI\">\/* nests all other methods from the base class *\/<\/span><br \/>\neXcell_myPrice.<span class=\"kw1\">prototype<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> eXcell<span class=\"sy0\">;<\/span><\/div><\/div>\n<p>The most important parts in this code are the name of the new type and data processing formula. To use this newly created cell type you can change this line:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">myGrid.<span class=\"me1\">setColTypes<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;dyn,ed,ed,myPrice&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>Here&#8217;s the result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-21.png\" alt=\"Grid custom columns\" width=\"599\" height=\"275\" class=\"aligncenter size-full wp-image-3144\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-21.png 599w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-21-300x138.png 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<p>If you want to add a possibility to edit the cells, you should do the following:<\/p>\n<ul>\n<li>add to your new cell an option to use methods of the \u201ced\u201d type;<\/li>\n<li>define the getValue() method that returns the current cell value; it may be used by standard API, and it\u2019s necessary for a normal edit operation;<\/li>\n<li>define the edit() method that is called when the grid is switched to the edit state (not declared explicitly, nested from the \u2018ed\u2019 type);<\/li>\n<li>define the detach() method that is called when the grid is switched back to the normal state (not declared explicitly, nested from the \u2018ed\u2019 type);<\/li>\n<\/ul>\n<p><\/p>\n<p>So, here&#8217;s the minimal amount of code required to add the editing feature:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\"><span class=\"kw1\">function<\/span> eXcell_myPrice<span class=\"br0\">&#40;<\/span>cell<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> <span class=\"co1\">\/\/the eXcell name is defined here<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>cell<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"co1\">\/\/ the default pattern<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">cell<\/span> <span class=\"sy0\">=<\/span> cell<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">grid<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">this<\/span>.<span class=\"me1\">cell<\/span>.<span class=\"me1\">parentNode<\/span>.<span class=\"me1\">grid<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; eXcell_ed.<span class=\"me1\">call<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">this<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> <span class=\"co1\">\/\/uses methods of the &quot;ed&quot; type<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">setValue<\/span><span class=\"sy0\">=<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>val<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"coMULTI\">\/* actual data processing *\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">setCValue<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;span&gt;&amp;#8364; &lt;\/span&gt;&lt;span&gt;&quot;<\/span><span class=\"sy0\">+<\/span>val<span class=\"sy0\">+<\/span><span class=\"st0\">&quot;&lt;\/span&gt;&quot;<\/span><span class=\"sy0\">,<\/span>val<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">this<\/span>.<span class=\"me1\">getValue<\/span><span class=\"sy0\">=<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"coMULTI\">\/* getting the value *\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> <span class=\"kw1\">this<\/span>.<span class=\"me1\">cell<\/span>.<span class=\"me1\">childNodes<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#93;<\/span>.<span class=\"me1\">innerHTML<\/span><span class=\"sy0\">;<\/span> <br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"coMULTI\">\/* nests all other methods from the base class *\/<\/span><br \/>\neXcell_myPrice.<span class=\"kw1\">prototype<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> eXcell<span class=\"sy0\">;<\/span><\/div><\/div>\n<p>Now, newly created cell type is editable:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-31.png\" alt=\"Grid custom columns\" width=\"600\" height=\"276\" class=\"aligncenter size-full wp-image-3142\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-31.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/12\/Grid-custom-columns-31-300x138.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Both edit() and detach() methods are not declared explicitly in this case. But you can always define them by yourself if you want to enable some extra functionality. To learn more about the custom cell types, check this <a href=\"https:\/\/docs.dhtmlx.com\/grid__columns_types.html#customcolumntypes\" target=\"_blank\">documentation page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we would like to continue to share with you small tips and tricks that allow you use DHTMLX components more efficiently and enjoy every single minute &#8230;<\/p>\n","protected":false},"author":2,"featured_media":3390,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,72,1,38],"tags":[33,43,6,14],"class_list":["post-3133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-posts-for-codeproject-com","category-general","category-uncategorized","category-visible","tag-all-posts","tag-dhtmlx-tips","tag-dhtmlxgrid","tag-tips"],"_links":{"self":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/3133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/comments?post=3133"}],"version-history":[{"count":1,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/3133\/revisions"}],"predecessor-version":[{"id":11388,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/3133\/revisions\/11388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media\/3390"}],"wp:attachment":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media?parent=3133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/categories?post=3133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/tags?post=3133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}