Helpful JavaScript Methods for the Telerik Grid for ASP.NET MVC
Photo by Jan van der Wolf on Pexels
The Telerik MVC Grid is quite a cool component. Nevertheless, some client-side API functions are desperately missing. For example, you cannot select or deselect rows by code. Also, there’s no way to do client-side-filtering (i.e. hide rows based on values withoud re-populating the whole grid). So here is a set of some functions that were helpful to me – suite yourselves…
//
// In a hierarchical grid, the function returns the parent row of the specified child row.
//
function getParentRow(row) {
var l = $(row).parents('.t-detail-row').prev();
return l.length == 0 ? null : l[0];
}
//
// In a hierarchical grid, the function returns the child grid of the specified parent row.
//
function getChildGrid(row) {
var nextRow = $(row).next();
return nextRow.hasClass('t-detail-row') ? $('.t-grid', nextRow).data('tGrid') : null;
}
//
// Selects the specified row. The row will be highlighted and the onRowSelect event handler will be called.
//
function selectRow(row) {
var $row = $(row);
$row.addClass('t-state-selected');
var grid = $row.parents('.t-grid').data('tGrid');
if(grid.onRowSelect) grid.onRowSelect({ row: row });
}
//
// Unselects all rows in the specified grid.
//
function unselectRows(grid) {
$(grid.$rows()).removeClass('t-state-selected');
}
//
// Unselects the specified row.
//
function unselectRow(row) {
$(row).removeClass('t-state-selected');
}
//
// Hides all rows in the specified grid that have a data member that equals teh specified value.
// If the data member is omitted, all rows get shown again.
//
function filterWhenEquals(grid, dataMember, value) {
var columnIndex = -1;
if (dataMember) {
$.each(grid.columns, function (i, c) {
if (c.member == dataMember) {
columnIndex = i;
}
});
}
if (columnIndex >= 0) {
grid.$rows().each(function (i, r) {
if ($($('td', r)[columnIndex]).html() != value) {
$(r).hide();
}
else {
$(r).show();
}
});
}
else { $(grid.$rows()).show(); }
}
Category: Browser JavaScript