Kendo Grid Selection

Kendo grid support selection of data on them. It can be a single or multiple selection.
The following is an extract from my kendo grid helper javasscript library that is used to help with the selection processing.

//Get the kendo grid item. Defaults to grid A
function GetGrid(name) {  
    if (name === undefined) {
        return $("#kgridA").data("kendoGrid");
    } else {
        return $(name).data("kendoGrid");
    }
}


//returns true if anything is selected on the grid
HasSelection: function (name) {  
    var grid = GetGrid(name);
    var row = grid.select();
    return (row.length !== 0);
},

//Returns the list of dataitems that are selected
SelectedDataItems: function (name) {  
    var grid = GetGrid(name);
    var row = grid.select();
    if (row.length === 0) {
        return null;
    }

    //Get all the grid data items
    var dataItemList = $.map(row, function (item) {
        return grid.dataItem(item);
    });

    return dataItemList;
},

SelectedSingleDataItem: function (name) {  
    var grid = GetGrid(name);
    var row = grid.select();
    if (row.length === 0) {
        return null;
    }
    return grid.dataItem(row[0]);
},

//select all rows on the grids page
SelectAll: function (name) {  
    var grid = GetGrid(name);
    grid.select("tr");
},

//clear any selections
ClearAll: function (name) {  
    var grid = GetGrid(name);
    grid.clearSelection();
},


//Select the first row in the grid that contains the given text in any cell.
SelectRowWithText: function (searchtext, name){  
    var grid = GetGrid(name),
        foundrow;

    var rows = grid.tbody.find(">tr:not(.k-grouping-row)");
    rows.each(function (index, row) {
        var allCells = $(row).find('td');
        if (allCells.length > 0) {
            var found = false;
            allCells.each(function (index, td) {
                var tdtext = $(td).text();
                if ((tdtext === searchtext)) {
                    found = true;
                    return false;
                }
            });
            if (found === true) {
                foundrow = row;
                return false;
            }
        }
    });

    grid.select(foundrow);

},

SelectRowWithId: function ( id, name ){  
    var grid = GetGrid(name);
    var dataSource = grid.dataSource;
    var item = dataSource.get(id);   //Find the item with the TagId
    var foundrow = grid.table.find("tr[data-uid='" + item.uid + "']");   //use that to find the row
    grid.select(foundrow);
},

HasData: function (name) {  
    var grid = GetGrid(name);
    var length = grid.dataSource.data().length;
    return (length > 0);
},

//Redraw the grid.
Refresh: function (name) {  
    var grid = GetGrid(name);
    grid.refresh();
},

//Force a reload of the data. Will pull from server
Reload: function (name) {  
    var grid = GetGrid(name);
    grid.dataSource.read();
},

Above referring to version
Telerik Kendo UI Professional Q1 2015
Telerik UI for ASP.NET MVC Q1 2015


comments powered by Disqus