//参数为填充总数
function showBlankRow(number) {
$.extend($.fn.datagrid.methods,{
fillRows: function (jq) {
var datacounts = number;
var grid = $(jq);
var pageSize = number;
var rows = grid.datagrid("getRows");
var length = rows.length;
if (pageSize >= datacounts && length < datacounts) {
var options = grid.datagrid("options");
var gpanel = grid.datagrid("getPanel");
var gbody1 = gpanel.find(".datagrid-view1 .datagrid-body");
var tbody1 = gbody1.find("table>tbody");
var gbody2 = gpanel.find(".datagrid-view2 .datagrid-body");
var tbody2 = gbody2.find("table>tbody");
var column1s = grid.datagrid("getColumnFields",true);
var column2s = grid.datagrid("getColumnFields");
var td1s = "";
var td2s = "";
if(tbody1.length > 0){
td1s += options.rownumbers ? "<td class='datagrid-td-rownumber'></td>":"";
if(column1s.length > 0){
$.each(column1s,function(i,field){
var opt = grid.datagrid("getColumnOption",field);
if(opt != null && !opt.hidden){
td1s +="<td field=\""+field+"\"></td>";
}
});
}
var events = $._data(gbody1.children().get(0),'events');
if(events != null){
//重新绑定click、dblclick事件
var clickfunc = events.click[0].handler;
events.click[0].handler = function(e){
var tt=$(e.target);
var tr=tt.closest("tr.datagrid-row");
if(tr.hasClass("datagrid-blank-row")){
return;
}
clickfunc(e);
};
var dblclickfunc = events.dblclick[0].handler;
events.dblclick[0].handler = function(e){
var tt=$(e.target);
var tr=tt.closest("tr.datagrid-row");
if(tr.hasClass("datagrid-blank-row")){
return;
}
dblclickfunc(e);
};
}
}
if(tbody2.length > 0 && column2s.length > 0){
$.each(column2s,function(i,field){
var opt = grid.datagrid("getColumnOption",field);
if(opt != null && !opt.hidden){
td2s +="<td field=\""+field+"\"></td>";
}
});
var events = $._data(gbody2.get(0),'events');
if(events != null){
//重新绑定click、dblclick事件
var clickfunc = events.click[0].handler;
events.click[0].handler = function(e){
var tt=$(e.target);
var tr=tt.closest("tr.datagrid-row");
if(tr.hasClass("datagrid-blank-row")){
return;
}
clickfunc(e);
};
var dblclickfunc = events.dblclick[0].handler;
events.dblclick[0].handler = function(e){
var tt=$(e.target);
var tr=tt.closest("tr.datagrid-row");
if(tr.hasClass("datagrid-blank-row")){
return;
}
dblclickfunc(e);
};
}
}
for (var i = length; i < datacounts; i++) {
if(td1s != ""){
var tr = '<tr class="datagrid-blank-row datagrid-row'
+ (options.striped && i % 2 != 0 ? ' datagrid-row-alt' : '') + '">' + td1s + '</tr>';
$(tr).appendTo(tbody1);
}
if(td2s != ""){
var tr = '<tr class="datagrid-blank-row datagrid-row' + (options.striped && i % 2 != 0 ? ' datagrid-row-alt' : '') + '">' + td2s + '</tr>';
$(tr).appendTo(tbody2);
}
}
}
}
});
}
//用法
function loadDatagrideTable(tableId) {
$(tableId).datagrid({
...
showBlankRow(20);
onLoadSuccess: function(data) {
$(tableId).datagrid("fillRows");
}
});
}
设置当datagrid无数据时显示空白行
最新推荐文章于 2024-06-26 18:56:32 发布