Simpson Kamonere
Simpson Kamonere

Reputation: 87

Kendo grid - How do I get record id of clicked template button

I have a kendo grid with id="gridtemplate" defined as shown below:

       <div>
            <h4>Download a data import template</h4>
            <div data-role="grid"
                 data-editable="inline"
                 data-toolbar="['create', 'save']"
                 data-columns="[
                                 { 'field': 'TemplateID', 'hidden': 'true', 'width': 270 },
                                 { 'field': 'TemplateType' },
                                 { 'field': 'FileName','title': 'FileName'},
                 {command:{ text: 'download', click: viewModel.Download, name:'Download' } }

                            ]"
                 data-bind="source: templates,
                        visible: isVisible,
                        events: {
                          save: onSave,
                          edit: onEdit
                        }"
                 style="height: 200px"></div>
        </div>

On clicking the download button in each row I want to get the record id associated with that row. I have a function called Download in my viewmodel defined as below:

var viewModel = kendo.observable({
    isVisible: true,
    Download: function (e) {
        console.log(id);//want to see the TemplateID here
    }
});

Being relatively new to Kendo, I have no clue how to get that. Please help. Thanks in advance.

Upvotes: 1

Views: 2577

Answers (2)

Sparrow
Sparrow

Reputation: 2583

You can try this:

var viewModel = kendo.observable({
    isVisible: true,
    Download: function (e) {
        var grid = $("#gridtemplate").data("kendoGrid"); //assuming the grid name is gridtemplate
        var selectedItem = grid.dataItem(grid.select());
        console.log(selectedItem.TemplateID);
    }
});

Upvotes: 0

The Dread Pirate Stephen
The Dread Pirate Stephen

Reputation: 3169

You should be able to get the dataItem of the row of the clicked button in the Download function like so:

var grid = this,
    dataItem = grid.dataItem(e.currentTarget.closest("tr"));

Then with the dataItem, you have access to all of the fields of your model.

Upvotes: 5

Related Questions