设置当datagrid无数据时显示空白行

//参数为填充总数
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");
		}
	});
}

在这里插入图片描述

发布了262 篇原创文章 · 获赞 104 · 访问量 8万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览