We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Add DropDownButton to cell in Grid with rowTemplate

Hi,

I'm trying to add a DropDownButton to te first cell of all rows in a Grid. The problem is that the DropDownButton does not want to load, even if I place it in the "created" event.
I'm using a rowTemplate. How can I add a dropdownbutton (with custom actions) to this template?

Alternative solution would be to open the context menu of the grid when clicking on a button in my grid.

<script>
        $(function() {
            var grid = new ejs.grids.Grid({
                dataSource: @Html.Raw(ViewBag.employees),
                allowPaging: true,
                allowSorting: true,
                rowTemplate: '#rowTemplate',
                columns: [
                    { columnName: 'Id', field: 'Id', visible: false },
                    { columnName: 'FirstName', field: 'FirstName', visible: false },
                    { columnName: 'LastName', field: 'LastName', visible: false },
                    { columnName: 'Email', field: 'Email', visible: false }
                ],
                created: function() {
                    var items = [
                        {
                            text: 'Detail'
                        },
                        {
                            text: 'Send Message'
                        },
                        {
                            text: 'Delete'
                        }];

                    // initialize DropDownButton control
                    var drpDownBtn = new ejs.splitbuttons.DropDownButton({items: items});

                    // Render initialized DropDownButton.
                    drpDownBtn.appendTo('.employee-button');
                }
            });
            grid.appendTo('#employee-grid');

            //var gridObj = document.getElementById("employee-grid").ej2_instances[0];
            //gridObj.sortColumn('Id', 'ascending'):
        });
       
    </script>
   
    <script id="rowTemplate" type="text/x-template">
    <tr class="">
        <td>
            <div class="col-sm-2">
                <button class="employee-button">Options</button>
            </div>
            <div class="col-sm-3">
                ${FirstName}
            </div>
            <div class="col-sm-3">
                ${LastName}
            </div>
            <div class="col-sm-3">
                ${Email}
            </div>
        </td>       
    </tr>
   
    </script>

5 Replies

MS Madhu Sudhanan P Syncfusion Team February 4, 2019 06:08 AM UTC

Hi Pieter, 

Thanks for contacting Syncfusion support. 

You can render the template element to custom component inside the dataBound event as follows. 


new ejs.grids.Grid({ 
        .... 
        dataBound: function() { 
            var items = [ 
                { 
                    text: 'Detail' 
                }, 
                { 
                    text: 'Send Message' 
                }, 
                { 
                    text: 'Delete' 
                }]; 
 
            // initialize DropDownButton control 
            var drpDownBtn = new ejs.splitbuttons.DropDownButton({items: items}); 
 
            // Render initialized DropDownButton. 
            drpDownBtn.appendTo('.employee-button'); 
        } 
    }); 
    grid.appendTo('#Grid'); 
 

Regards, 
Madhu Sudhanan P 



PI Pieter-Jan February 5, 2019 09:50 AM UTC

Hi Madhu,

Thanks for the quick reply. Your solution works, but only for the first element. The dropdown menu is rendered on the first button in the first row, but not in the following rows.
As you can see in the attached screenshot, the second and third button are not dropdowns.

As a follow-up question, is it possible to attach events to the dropdown menu? Or do I register these events after the rendering?

Thanks!


MS Madhu Sudhanan P Syncfusion Team February 6, 2019 06:14 AM UTC

Hi Pieter, 

Thanks for the update. 

We have modified the solution using dataBound event as follow to render all row dropdowns and also registered selected event to the DropDownButton list item. 


var grid = new ejs.grids.Grid({ 
       .... 
        dataBound: function () { 
            var items = [ 
                { 
                    text: 'Detail' 
                }, 
                { 
                    text: 'Send Message' 
                }, 
                { 
                    text: 'Delete' 
                }]; 
            var btns = this.element.querySelectorAll('.employee-button'); 
            for (var i = 0; i < btns.length; i++) { 
                var drpDownBtn = new ejs.splitbuttons.DropDownButton({ 
                    items: items, 
                    select: function (e) { 
                        alert(e.item.text) 
                    } 
                }); 
 
                drpDownBtn.appendTo(btns[i]); 
            } 
        } 
    }); 




Regards, 
Madhu Sudhanan P 



PI Pieter-Jan February 6, 2019 08:52 AM UTC

Perfect! It works!
Thanks for the help.


MS Madhu Sudhanan P Syncfusion Team February 6, 2019 08:58 AM UTC

Hi Pieter, 
Thanks for the update. Please get back to us if you need any assistance. 
Regards, 
Madhu Sudhanan P 


Loader.
Up arrow icon