|
| 1 | +--- |
| 2 | +title: How to add a click handler to an icon in the ListBox template |
| 3 | +description: How do I add a small icon to the listbox items that would launch a window? |
| 4 | +type: how-to |
| 5 | +page_title: How to add a click handler to an item in a draggable ListBox |
| 6 | +slug: listbox-add-click-handler-to-item-template |
| 7 | +position: |
| 8 | +tags: listbox, click, icon, template, function, click not working, draggable |
| 9 | +ticketid: 1401928 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product Version</td> |
| 18 | + <td>2019.1.220</td> |
| 19 | + </tr> |
| 20 | + <tr> |
| 21 | + <td>Product</td> |
| 22 | + <td>jQuery Kendo UI ListBox and wrappers</td> |
| 23 | + </tr> |
| 24 | + </tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +I would like to add a small icon to the template of the Kendo UI ListBox that is clickable. No matter what I try the function is not executed. |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +The Kendo UI ListBox prevents the `mousedown` event as it needs it for dragging and selection. To be able to attach a handler that responds to an external mouse event, you can wrap the list box in another div and check for the target element during `mousedown` or `click`. |
| 34 | + |
| 35 | +```tab-jQuery |
| 36 | + <div id="listbox-container"> |
| 37 | + <select id="listBox"></select> |
| 38 | + </div> |
| 39 | +
|
| 40 | + <script> |
| 41 | + // the item template has an icon with class see-more |
| 42 | + $("#listbox-container").on("click", ".see-more", function(e){ |
| 43 | + var dataItem = listbox.dataItem($(e.target).closest(".k-item")); |
| 44 | + kendo.alert("Children: " + kendo.stringify(dataItem.children)); |
| 45 | + }); |
| 46 | + </script> |
| 47 | +``` |
| 48 | +```tab-Razor |
| 49 | + <div id="lb"> |
| 50 | + @(Html.Kendo().ListBox() |
| 51 | + .Name("listBoxAvailableRoles") |
| 52 | + .TemplateId("customer-item-template") |
| 53 | + ) |
| 54 | + </div> |
| 55 | + <script id="customer-item-template" type="text/x-kendo-template"> |
| 56 | + <span class="k-icon k-i-edit edit"></span> |
| 57 | + <span class="k-state-default"><p>#: data.Name #</p></span> |
| 58 | + </script> |
| 59 | + <script> |
| 60 | + $("#lb").on("mousedown", ".edit", function (e) { |
| 61 | + kendo.alert("test"); |
| 62 | + }); |
| 63 | + </script> |
| 64 | +``` |
| 65 | + |
| 66 | +###### Example |
| 67 | + |
| 68 | +```dojo |
| 69 | + <script type="text/kendo-x-tmpl" id="template"> |
| 70 | + <span class="k-icon k-i-eye see-more"></span> |
| 71 | + <span class="k-state-default"><div>#: data.name #</div></span> |
| 72 | + </script> |
| 73 | + <div id="listbox-container"> |
| 74 | + <select id="listBox"></select> |
| 75 | + </div> |
| 76 | + <script> |
| 77 | + var listbox = $("#listBox").kendoListBox({ |
| 78 | + draggable:true, |
| 79 | + dataSource: { |
| 80 | + data: [ |
| 81 | + { name: "Jane Doe", children: [{name: "Mary"}] }, |
| 82 | + { name: "John Doe", children: [{name: "Tom"}, {name: "George"}] } |
| 83 | + ] |
| 84 | + }, |
| 85 | + template: kendo.template($("#template").html()) |
| 86 | + }).data("kendoListBox"); |
| 87 | +
|
| 88 | + $("#listbox-container").on("click", ".see-more", function(e){ |
| 89 | + var dataItem = listbox.dataItem($(e.target).closest(".k-item")); |
| 90 | + kendo.alert("Children: " + kendo.stringify(dataItem.children)); |
| 91 | + }); |
| 92 | +
|
| 93 | + </script> |
| 94 | + <style> |
| 95 | + .see-more { |
| 96 | + color: #515967; |
| 97 | + background-color: #f3f3f4; |
| 98 | + padding:4px; |
| 99 | + border-radius:5px; |
| 100 | + border: 1px solid #515967; |
| 101 | + } |
| 102 | + </style> |
| 103 | +``` |
0 commit comments