{"id":2791,"date":"2015-04-16T16:54:06","date_gmt":"2015-04-16T12:54:06","guid":{"rendered":"http:\/\/www.dhtmlx.com\/blog\/?p=2791"},"modified":"2026-01-30T11:54:05","modified_gmt":"2026-01-30T08:54:05","slug":"segmented-button-for-toolbar","status":"publish","type":"post","link":"https:\/\/dhtmlx.com\/blog\/segmented-button-for-toolbar\/","title":{"rendered":"DHTMLX Little-Known Features: Segmented Button for Toolbar"},"content":{"rendered":"<p>We are glad to share one more DHTMLX tip, and this time you&#8217;ll know how to divide the buttons from toolbar into groups (grouped buttons also known as segmented button).<\/p>\n<p>You can create several segmented buttons for a single toolbar. Only one button at a time can be pressed within the group. It provides you more abilities with <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxToolbar\/\" target=\"_blank\">dhtmlxToolbar<\/a>: click on the screenshot below to open demo and you&#8217;ll see how these groups can be used in practice.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/04\/toolbar-segmented.png\" alt=\"Segmented button in toolbar\" width=\"600\" height=\"452\" class=\"aligncenter size-full wp-image-2792\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/04\/toolbar-segmented.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2015\/04\/toolbar-segmented-300x226.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><!--more--><br \/>\nOn contrast to our previous tips, we have more code this time, so \u201cCtrl+U\u201d and let&#8217;s deal with it together.<\/p>\n<p>The files <strong>ftypes.css<\/strong> and <strong>ftypes.js<\/strong> include description of items for dataview. Data for dataview are included in <strong>docs.xml<\/strong> file, <strong>imgs<\/strong> directory contains icons for the components (toolbar, sidebar and files types).<\/p>\n<p>Now we proceed to the needed code which we can find in <strong>toolbar_segmented.js<\/strong> file. To provide you the ability to easily use this feature, we moved its code to a separate file-extension of toolbar so you need just follow simple steps to include it in your app:<\/p>\n<p>1) Include the file mentioned above into the page (after dhtmlx.js or after dhtmlxtoolbar.js) <\/p>\n<p>2) Then use myToolbar.addSegmented(groupName, ids, selected) method. Let\u2019s review these parameters in detail:<\/p>\n<p><strong> &#8211; groupName<\/strong> &#8211; name for group of buttons<br \/>\n<strong> &#8211; ids<\/strong> &#8211; array with ids of the buttons that should be included in the group. Within one group with the name groupName only one button (specified in ids) can be pressed.<br \/>\n<strong> &#8211; selected<\/strong> &#8211; id of the buton which will be pressed by default (otherwise the 1st button from ids list will be pressed).<\/p>\n<p><strong>Note:<\/strong> the buttons should have buttonTwoState type.<\/p>\n<p>Additionally we created <em>onSegmentedChange<\/em> event which fires when a user changes the status of the button:<\/p>\n<p>myToolbar.attachEvent(&#8220;onSegmentedChange&#8221;, function(groupName, buttonId, buttonState){})<\/p>\n<p><strong> &#8211; groupName<\/strong> &#8211; name for group of buttons<br \/>\n<strong> &#8211; buttonId<\/strong> &#8211; pressed button id<br \/>\n<strong> &#8211; buttonState<\/strong> &#8211; <em>true<\/em> for the pressed button, <em>false<\/em> for the button that was pressed and then automatically unpressed<\/p>\n<p>Now let&#8217;s look at the example:<\/p>\n<p>1) At first we init toolbar<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\"><span class=\"kw1\">var<\/span> myToolbar <span class=\"sy0\">=<\/span> mySidebar.<span class=\"me1\">items<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;documents&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">attachToolbar<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#123;<\/span><br \/>\nitems<span class=\"sy0\">:<\/span> <span class=\"br0\">&#91;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span>id<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;open&quot;<\/span><span class=\"sy0\">,<\/span> type<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;button&quot;<\/span><span class=\"sy0\">,<\/span> img<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;open.gif&quot;<\/span><span class=\"br0\">&#125;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span>id<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;copy&quot;<\/span><span class=\"sy0\">,<\/span> type<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;button&quot;<\/span><span class=\"sy0\">,<\/span> img<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;copy.gif&quot;<\/span><span class=\"br0\">&#125;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span>type<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;separator&quot;<\/span><span class=\"br0\">&#125;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span>id<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;ftiles&quot;<\/span><span class=\"sy0\">,<\/span> type<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;buttonTwoState&quot;<\/span><span class=\"sy0\">,<\/span> img<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;large.gif&quot;<\/span><span class=\"br0\">&#125;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span>id<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;ficons&quot;<\/span><span class=\"sy0\">,<\/span> type<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;buttonTwoState&quot;<\/span><span class=\"sy0\">,<\/span> img<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;medium.gif&quot;<\/span><span class=\"br0\">&#125;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span>id<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;flabels&quot;<\/span><span class=\"sy0\">,<\/span> type<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;buttonTwoState&quot;<\/span><span class=\"sy0\">,<\/span> img<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;small.gif&quot;<\/span><span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#93;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>2) Then we add segmentedButton<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">myToolbar.<span class=\"me1\">addSegmented<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;mode&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;ftiles&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;ficons&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;flabels&quot;<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;ficons&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>3) The next step is to attach the event<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">myToolbar.<span class=\"me1\">attachEvent<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;onSegmentedChange&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>group<span class=\"sy0\">,<\/span> id<span class=\"sy0\">,<\/span> state<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>state <span class=\"sy0\">==<\/span> <span class=\"kw2\">true<\/span> <span class=\"sy0\">&amp;&amp;<\/span> group <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;mode&quot;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; myDataView.<span class=\"me1\">define<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;type&quot;<\/span><span class=\"sy0\">,<\/span> id<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>You can also contact our support and request for the demo in .zip.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are glad to share one more DHTMLX tip, and this time you&#8217;ll know how to divide the buttons from toolbar into groups (grouped buttons also known as segmented &#8230;<\/p>\n","protected":false},"author":2,"featured_media":3302,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,71,1,38],"tags":[33,43,14],"class_list":["post-2791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-posts-for-codeproject-com","category-tutorials","category-uncategorized","category-visible","tag-all-posts","tag-dhtmlx-tips","tag-tips"],"_links":{"self":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2791","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=2791"}],"version-history":[{"count":1,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2791\/revisions"}],"predecessor-version":[{"id":11403,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2791\/revisions\/11403"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media\/3302"}],"wp:attachment":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media?parent=2791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/categories?post=2791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/tags?post=2791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}