{"id":2533,"date":"2014-11-04T18:48:43","date_gmt":"2014-11-04T14:48:43","guid":{"rendered":"http:\/\/www.dhtmlx.com\/blog\/?p=2533"},"modified":"2023-05-05T13:56:40","modified_gmt":"2023-05-05T10:56:40","slug":"showing-custom-labels-in-dhtmlxgantt","status":"publish","type":"post","link":"https:\/\/dhtmlx.com\/blog\/showing-custom-labels-in-dhtmlxgantt\/","title":{"rendered":"DHTMLX Little-Known Features: Showing Custom Labels in dhtmlxGantt"},"content":{"rendered":"<p>We&#8217;ve <a href=\"https:\/\/dhtmlx.com\/blog\/dhtmlxgantt-3-1-bug-fixes-release\/\" target=\"_blank\">recently updated<\/a> dhtmlxGantt and we really like what we&#8217;ve got as a result. Every single feature completes the component and makes it a powerful <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxGantt\/\" target=\"_blank\">JavaScript Gantt Chart<\/a>.<\/p>\n<p>In this article we&#8217;ll tell you about the ability to <a href=\"https:\/\/docs.dhtmlx.com\/gantt\/desktop__text_block_for_task.html\" target=\"_blank\">show custom labels near the Gantt bars<\/a>. <\/p>\n<p>Sometimes you need to display additional content near the Gantt bars. It can be a person&#8217;s name, the priority of the task, the timeframe or something else.<br \/>\n<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/assigned_text.png\" alt=\"labels-near-gantt\" width=\"600\" height=\"180\" class=\"aligncenter size-full wp-image-2534\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/assigned_text.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/assigned_text-300x90.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>It can be easily done with a few lines of code.<br \/>\n<!--more--><br \/>\nTo place the text on the left side of a task bar, use the <a href=\"https:\/\/docs.dhtmlx.com\/gantt\/api__gantt_leftside_text_template.html\" target=\"_blank\">leftside_text<\/a> template:<br \/>\n<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">gantt.<span class=\"me1\">templates<\/span>.<span class=\"me1\">leftside_text<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>start<span class=\"sy0\">,<\/span> end<span class=\"sy0\">,<\/span> task<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> <span class=\"st0\">&quot;&lt;b&gt;Priority: &lt;\/b&gt;&quot;<\/span> <span class=\"sy0\">+<\/span>task.<span class=\"me1\">priority<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>If you need to place the content on the right side, use the <a href=\"https:\/\/docs.dhtmlx.com\/gantt\/api__gantt_rightside_text_template.html\" target=\"_blank\">rightside_text<\/a> template:<br \/>\n<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">gantt.<span class=\"me1\">templates<\/span>.<span class=\"me1\">rightside_text<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>start<span class=\"sy0\">,<\/span> end<span class=\"sy0\">,<\/span> task<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> <span class=\"st0\">&quot;&lt;b&gt;Holders: &lt;\/b&gt;&quot;<\/span> <span class=\"sy0\">+<\/span> task.<span class=\"me1\">users<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/outer-content.png\" alt=\"content-near-gantt\" width=\"558\" height=\"354\" class=\"aligncenter size-full wp-image-2535\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/outer-content.png 558w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/outer-content-300x190.png 300w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/p>\n<p>Moreover, you can change the inner text of the task bars and add the needed content to them. For this, use the <a href=\"https:\/\/docs.dhtmlx.com\/gantt\/api__gantt_task_text_template.html\" target=\"_blank\">task_text<\/a> template:<br \/>\n<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">gantt.<span class=\"me1\">templates<\/span>.<span class=\"me1\">task_text<\/span><span class=\"sy0\">=<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>start<span class=\"sy0\">,<\/span>end<span class=\"sy0\">,<\/span>task<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> <span class=\"st0\">&quot;&lt;b&gt;Text:&lt;\/b&gt; &quot;<\/span><span class=\"sy0\">+<\/span>task.<span class=\"me1\">text<\/span><span class=\"sy0\">+<\/span><span class=\"st0\">&quot;,&lt;b&gt; Holders:&lt;\/b&gt; &quot;<\/span><span class=\"sy0\">+<\/span>task.<span class=\"me1\">users<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p><\/p>\n<p>Note that all template functions output the HTML string. This means you can specify not only the text content, but also various visual indicators, icons, etc.<br \/>\n<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/text-on-task-bar.png\" alt=\"text-on-task-bar\" width=\"575\" height=\"385\" class=\"aligncenter size-full wp-image-2536\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/text-on-task-bar.png 575w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/11\/text-on-task-bar-300x200.png 300w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/p>\n<p>In case you want to have the <strong>different labels for each type of tasks<\/strong>, you can add the conditions into the template functions.<\/p>\n<p>The template will be called for each task that is displayed in the gantt, providing the task object as an argument of the template function. That allows you to inspect the current item and return appropriate content depending of the type or other properties of the task. <\/p>\n<p>Check the following example:<br \/>\n<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">gantt.<span class=\"me1\">templates<\/span>.<span class=\"me1\">rightside_text<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>start<span class=\"sy0\">,<\/span> end<span class=\"sy0\">,<\/span> task<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span><span class=\"br0\">&#40;<\/span>task.<span class=\"me1\">type<\/span> <span class=\"sy0\">==<\/span> gantt.<span class=\"me1\">config<\/span>.<span class=\"me1\">types<\/span>.<span class=\"me1\">milestone<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> task.<span class=\"me1\">text<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> <span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>Read the <a href=\"https:\/\/docs.dhtmlx.com\/gantt\/desktop__text_block_for_task.html\" target=\"_blank\">related article<\/a> in documentation and check the <a href=\"https:\/\/docs.dhtmlx.com\/gantt\/samples\/04_customization\/01_outer_content.html\" target=\"_blank\">demo<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve recently updated dhtmlxGantt and we really like what we&#8217;ve got as a result. Every single feature completes the component and makes it a powerful JavaScript Gantt Chart. In &#8230;<\/p>\n","protected":false},"author":2,"featured_media":3316,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,71,1],"tags":[33,43,20,14],"class_list":["post-2533","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-posts-for-codeproject-com","category-tutorials","category-uncategorized","tag-all-posts","tag-dhtmlx-tips","tag-gantt","tag-tips"],"_links":{"self":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2533","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=2533"}],"version-history":[{"count":1,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2533\/revisions"}],"predecessor-version":[{"id":11410,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2533\/revisions\/11410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media\/3316"}],"wp:attachment":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media?parent=2533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/categories?post=2533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/tags?post=2533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}