I\'m trying to display a friendly message (like \"No records found, try again later\") within the grid content, when there are no records in the database.
F
// Kendo Grid
dataSource: dataSource,
dataBound:gridDataBound,
//No data in the grid show message
function gridDataBound(e) {
var grid = e.sender;
if (grid.dataSource.total() == 0) {
var colCount = grid.columns.length;
$(e.sender.wrapper)
.find('tbody')
.append('<tr class="kendo-data-row"><td colspan="' + colCount + '" class="no-data">There is no data to show in the grid.</td></tr>');
}
};
Not sure what was the exact version this question was asked, but in my case none of the above solutions worked.
I used the following one:
config : {
noRecords: {
message: "No records found."
},
}
If your grid has detail grids (nested grids) then the above examples wont work on the nested grids. To ensure you apply this to all of your kendo grids you can do the following:
function kendoEmptyGridFix() {
$("[data-role='grid']").each(function() {
$(this).data("kendoGrid").bind('detailInit', function(e) {
kendoEmptyGridFix();
});
$(this).data("kendoGrid").bind('dataBound', function(e) {
var colCount = this.table.find("tHead tr th").length;
if ($(this)[0].dataSource._view.length == 0) {
var msg = ($(this)[0].dataSource.options.emptyMsg == undefined ? "No Results Found!" : $(this)[0].dataSource.options.emptyMsg);
this.table.find('tbody').html('<tr class="kendo-data-row"><td colspan="' + colCount + '" style="text-align:center; padding-top:20px; padding-bottom:20px;"><div class="k-empty-grid-row">' + msg + '</div></td></tr>');
// optional to hide pager section
this.table.parent().find('.k-grid-pager').hide();
};
});
});
}
Then call this function after all of your content has been loaded, there is no need to add it to each grid individually.
$(document).ready(function () {
kendoEmptyGridFix();
});
if you wanted to change the message then add emptyMsg to your dataSource i.e.
dataSource: {
transport: {
read: {
url: "/getItems/" + e.data.id,
dataType: "xml"
}
},
emptyMsg: 'There are currently no items available',
schema: {
type: "xml",
data: "/a/b",
model: {
fields: {
"id": "id/text()",
"status": "status/text()"
}
}
},
pageSize: 20
}
I know I'm late to the party but here's how I just did it. It's mostly copied from the TreeList's no data feature (I was annoyed that you didn't have the same thing with the standard grid). I made it into a prototype extension so it's automatically added to every grid. An option could also be added to make the message configurable.
// Replace the grid content with a status message (Can be reused for data errors if you want to show "Request failed [Reload]" or something like that.
kendo.ui.Grid.prototype._showStatus = function (message) {
var status = this.content.find(".k-status");
if (!status.length) {
status = $("<div class='k-status' />").appendTo(this.content.closest(".k-grid-content"));
}
status.html(message);
};
// Put back the grid content instead of the status message
kendo.ui.Grid.prototype._hideStatus = function () {
this.content.find(".k-status").remove();
};
// Keep the original render function so we can call it int our override
kendo.ui.Grid.prototype.__renderContent = kendo.ui.Grid.prototype._renderContent;
// Override the render function
kendo.ui.Grid.prototype._renderContent = function (data, colspan, groups) {
this.__renderContent(data, colspan, groups);
if (data.length)
this._hideStatus();
else
this._showStatus("No data."); // Could also add an option for that text so you can choose the message in a grid config
};
Can't you do something like this -
if(this.tbody.rows.length === 0) {
alert('no records');
return;
}
Or you are looking for something even cleaner something inbuilt in Kendo? I think, this is an issue still there in Kendo UI which hasn't been fixed yet See this - http://www.telerik.com/forums/empty-grid-norecords-template
On Grid Data Bound..
Add the following script to show Message.
//ondatabound on user assginment grid grid
function onUserAssignGridDataBound(e) {
//Get the number of Columns in the grid
var colCount = $("#UserAssignGrid").find('.k-grid-header colgroup > col').length;
//If There are no results place an indicator row
if ($("#UserAssignGrid").data("kendoGrid").dataSource._view.length == 0) {
$("#UserAssignGrid").find('.k-grid-content tbody')
.append('<tr class="kendo-data-row"><td colspan="' +
colCount +
'" style="text-align:center; padding-top:10px;background-color:#AFE4FA"><b>No Results Found!</b></td></tr>');
}