var _____WB$wombat$assign$function_____ = function(name) {return (self._wb_wombat && self._wb_wombat.local_init && self._wb_wombat.local_init(name)) || self[name]; }; if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; return this; } } { let window = _____WB$wombat$assign$function_____("window"); let self = _____WB$wombat$assign$function_____("self"); let document = _____WB$wombat$assign$function_____("document"); let location = _____WB$wombat$assign$function_____("location"); let top = _____WB$wombat$assign$function_____("top"); let parent = _____WB$wombat$assign$function_____("parent"); let frames = _____WB$wombat$assign$function_____("frames"); let opener = _____WB$wombat$assign$function_____("opener"); $c.fn.datagrid = function (config) { var obj = function (selector, config) { this.selector = selector; this.config = $.extendEx({}, this.defConfig, config); this._frozen(); } obj.prototype.defConfig = { queryParams: { pageNo: 1, pageSize: 15 }, pagerConfig: { pagerItems: 'page-size-dropdown,|,first,pre,|,goto,|,next,last,|,load,remark', pageList:[10,15,20,30,40,50] }, showLoading: true,//是否显示loading弹出框. showHeader:true, showLoadingDelay: 1000,//单位:毫秒 showLoadingWindow: window.kwindow //top.kwindow }; obj.prototype.datagrid = function () { this.selector.addClass('kui-datagrid'); this.loadData(); } obj.prototype.loadData = function (config) {//切换分页时,外部调用这个方法即可. this.config = $.extendEx(this.config, config); var _obj = this; if (_obj.config.data) { _obj.draw(data); } else if (_obj.config.url) { var loadingIndex = 0; var isLoaded = false; if (_obj.config.showLoading == true && _obj.config.showLoadingWindow) { if (_obj.config.showLoadingDelay > 0) { var _timeoutID = window.setTimeout(function () { window.clearTimeout(_timeoutID); if (!isLoaded) { loadingIndex = _obj.config.showLoadingWindow.loading(_obj.config.loadingConfig); } }, _obj.config.showLoadingDelay) } else{ loadingIndex = _obj.config.showLoadingWindow.loading(_obj.config.loadingConfig); } } $.post(_obj.config.url, _obj.config.queryParams, function (result, status) { isLoaded = true; if (result.rows.length == 0 && _obj.config.queryParams.pageNo > 1 && _obj.config.queryParams.pageNo > result.pageTotal) {//最后一页的记录被删光,重新加载前一页. _obj.config.queryParams.pageNo--; _obj.loadData(_obj.config); return; } _obj.pageResult = result; _obj.draw(result.rows); if (_obj.config.pager) { var pagerConfig = $.extendEx({}, { pageTotal: result.pageTotal, pageNo: result.pageNo, pagerItemClick: function (_pageNo, trigger) { if ($(trigger).hasClass('kui-pager-item-load')) { $(trigger).removeClass('kui-pager-item-load'); $(trigger).addClass('kui-pager-item-loading'); } _obj.loadData({ queryParams: { pageNo: _pageNo } }); }, onPageSizeChange:function(elem,o){ _obj.loadData({ queryParams: { pageNo: 1, pageSize: o.value} }) }, prevPageText: '', firstPageText: '', nextPageText: '', lastPageText: '' }, _obj.config.pagerConfig, { pageSize: _obj.config.queryParams.pageSize }); var pager = $c(_obj.config.pager).pager(pagerConfig); var recordStartNo = ((result.pageNo - 1) * result.pageSize + 1); pager.addRemark('显示' + recordStartNo + '到' + (recordStartNo + result.rows.length - 1) + ',共' + result.rowTotal + '记录'); } if (loadingIndex > 0) { _obj.config.showLoadingWindow.close(loadingIndex); } }); } } obj.prototype.checkAll = function (isCheck) { var _obj = this; if (isCheck == undefined) { isCheck = true; } $.each(_obj.selector.find('.kui-tr'), function (i, tr) { if (i == 0) {//表头 return; } var cb = $(tr).find('.kui-td-checkbox :checkbox'); if (isCheck) { //cb.attr('checked', 'true'); cb[0].checked = true; $(tr).addClass('kui-tr-selected'); } else { //cb.removeAttr('checked'); cb[0].checked = false; $(tr).removeClass('kui-tr-selected'); } }); } obj.prototype.checkRow = function (index, isCheck) { var _obj = this; if (isCheck == undefined) { isCheck = true; } var tr=_obj.selector.find('.kui-tr:eq(' + (index + 1) + ')'); var cb = tr.find('.kui-td-checkbox :checkbox'); if (isCheck) { cb[0].checked = true; tr.addClass('kui-tr-selected'); } else { cb[0].checked = false; tr.removeClass('kui-tr-selected'); } } obj.prototype.getChecked = function () { var _obj = this; var rows = []; $.each(_obj.selector.find('.kui-tr'), function (i,tr) { if (tr.index==-1) {//表头 return; } if ($(tr).find('.kui-td-checkbox :checkbox').is(':checked')) { rows.push(tr); } }); return rows; } obj.prototype.setRowData=function(index,data){ var _obj = this; var trs = _obj.selector.find('.kui-tr'); $.each(trs, function (i,tr) { if (tr.index != index) { return; } tr.data = data; var columns = _obj.config.columns; $.each(columns, function (i, column) { if (column.checkbox == true||column.rowNo==true) { return; } var td = $(tr).find('.kui-td-' + (i + 1) + ''); var formatData = data[column.field]; if (column.formatter) { formatData = column.formatter(data[column.field], data, index) } td.find('span').html(formatData); }); }); } obj.prototype.getRows = function () { var _obj = this; var trList; if(_obj.config.showHeader){ trList = _obj.selector.find('.kui-tr:not(:eq(0))'); } else { trList = _obj.selector.find('.kui-tr'); } return trList; } obj.prototype.getRow = function (index) { var _obj = this; var trs = _obj.selector.find('.kui-tr'); var result; $.each(trs, function (i, tr) { if (tr.index == index) { result = tr; } }); return result; } obj.prototype.draggable = function (dragConfig) { var _obj = this; return _obj.divTableObj.draggable(dragConfig); } obj.prototype._frozen = function () { var _obj = this; if (!_obj.config.frozen) { return; } var frozenRow = _obj.config.frozen.row || _obj.config.frozen.row == 0; var frozenCol = _obj.config.frozen.col; if (!frozenRow && !frozenCol) { return; } var dragMode; if (frozenRow && frozenCol) { dragMode = $k.dragMode.hOrv; } else if (frozenRow) { dragMode = $k.dragMode.v; } else if (frozenCol) { dragMode = $k.dragMode.h; } var dragPagerDistance = 0;//需要拖动这么个距离才能分页. var haveDragDistance = 0;//已经拖动的距离. var config = _obj.config; var _config = $.extend({}, config, { showLoading:false, onPreRender: function (_dg) { if (_dg.selector[0].static_position) { _dg.selector.attr('style', _dg.selector[0].static_position.style); } if (config.onPreRender) { config.onPreRender(_dg); } _obj.selector[0].isLoading = undefined; }, onRender: function (_dg) { //alert(_dg.selector.height()); //return; if (_dg.selector[0].sepContainer[0].isHidden) { _dg.selector[0].sepContainer[0].isHidden = undefined; _dg.selector[0].sepContainer.show(); var tdList = _dg.selector.find('>.kui-tr>.kui-td'); $.each(tdList, function (i, td) { $(td).css('border-left-color', 'transparent'); }); } _dg.selector[0].static_position = { style: _dg.selector.attr('style') || null //undefined||null为null,$('..').attr('style',null)相当于$('..').removeAttr('style') }; $.each(_dg.selector.find('.kui-tr'), function (i, tr) { tr.static_position = { style: $(tr).attr('style') || null }; var tdList = $(tr).find('.kui-td'); $.each(tdList, function (j, td) { td.static_position = { style: $(td).attr('style') || null } }); }); //_dg.selector.height(Math.ceil(_dg.selector.height())); //_dg.selector.css('position', 'absolute'); var frozenRowIndex = config.frozen.row-1;//这里假设为0,即表头 var frozenColIndex = config.frozen.col - 1;//这里假设为0,即第一列 var trList = _dg.selector.find('.kui-tr'); var trLast; if(trList.length>0){ trLast = $(trList[trList.length - 1]); } var trFrozen; if (frozenRowIndex >= 0) { trFrozen = $(trList[frozenRowIndex]); } var pagerTop = _dg.selector[0].pagerTop; var pagerBottom = _dg.selector[0].pagerBottom; if (!pagerTop) { var _id = Guid.NewGuid(); $('body').prepend('
'); //pagerTop = $('
').appendTo($('body')); pagerTop = $('#' + _id + ''); _dg.selector[0].pagerTop = pagerTop; } if (!pagerBottom) { var _id = Guid.NewGuid(); $('body').prepend('
'); pagerBottom = $('#' + _id + ''); //pagerBottom = $('
').appendTo($('body')); _dg.selector[0].pagerBottom = pagerBottom; } pagerTop.width(_dg.selector.width()); pagerBottom.width(_dg.selector.width()); var _divPagerTop = pagerTop.find('>div'); var _divPagerBottom = pagerBottom.find('>div'); if (_dg.pageResult.pageNo == 1) { _divPagerTop.html('没有上一页了'); } else { _divPagerTop.html('
下拉显示上一页
查看' + ((_dg.pageResult.pageNo - 2) * _dg.pageResult.pageSize + 1) + ' ~ ' + ((_dg.pageResult.pageNo - 1) * _dg.pageResult.pageSize) + ' 条
'); //pagerTop.find('.kui-mobile-pager-top-text-2')[0]._html = pagerTop.find('.kui-mobile-pager-top-text-2').html(); } if (_dg.pageResult.pageNo == _dg.pageResult.pageTotal) { _divPagerBottom.html('没有下一页了'); } else { _divPagerBottom.html('
上拉显示下一页
查看' + (_dg.pageResult.pageNo * _dg.pageResult.pageSize + 1) + ' ~ ' + ((_dg.pageResult.pageNo + 1) * _dg.pageResult.pageSize) + ' 条
'); //pagerBottom.find('.kui-mobile-pager-bottom-text-2')[0]._html = pagerTop.find('.kui-mobile-pager-bottom-text-2').html(); } pagerTop.show(); //先显示出来,pagerTop.find('>div').outerHeight() 才有高度值. pagerBottom.show(); //先显示出来, pagerBottom.find('>div').outerHeight() 才有高度值. if (!trFrozen) { if(trList.length>0){ pagerTop.css('top',$(trList[0]).offset().top); } //else { // pagerTop.css('top', 0); //} } else { pagerTop.css('top', trFrozen.offset().top + trFrozen.outerHeight()); } _divPagerTop.css('left', (pagerTop.width() - _divPagerTop.width()) / 2); if (trLast) { if (trLast.offset().top + trLast.outerHeight() > _dg.selector.parent().offset().top + _dg.selector.parent().height()) { pagerBottom.css('top', _dg.selector.parent().offset().top + _dg.selector.parent().height() - pagerBottom.find('>div').outerHeight()); } else { pagerBottom.css('top', trLast.offset().top + trLast.outerHeight() - pagerBottom.find('>div').outerHeight()); } } _divPagerBottom.css('left', (pagerBottom.width() - _divPagerBottom.width()) / 2); dragPagerDistance = Math.max(_divPagerBottom.outerHeight(), _divPagerTop.outerHeight()) + 10; pagerTop.hide(); //测试完成后启用 pagerBottom.hide(); //测试完成后启用 if (_dg.selector[0].dragObj) { return; } _dg.selector[0].dragObj = _dg.draggable({ dragMode: dragMode, onMoveStart: function (_drag, e) { haveDragDistance = 0; if (_obj.selector[0].isLoading) {//正在加载数据. return; } //将表格布局转为绝对定位,以便拖动. _obj.selector[0].absolute_position = { left: _obj.selector.position().left, top: _obj.selector.position().top, width:_obj.selector.width(), height: _obj.selector.height() }; _obj.selector.css('left', _obj.selector[0].absolute_position.left); _obj.selector.css('top', _obj.selector[0].absolute_position.top + _obj.selector.parent().scrollTop()); _obj.selector.css('width', _obj.selector[0].absolute_position.width); _obj.selector.css('height', _obj.selector[0].absolute_position.height); _obj.selector.css('position', 'absolute'); $.each(_dg.selector.find('.kui-tr'), function (i, tr) { tr.absolute_position = { left: $(tr).position().left, top: $(tr).position().top, width: $(tr).width(), height: $(tr).height() }; }); $.each(_dg.selector.find('.kui-tr'), function (i, tr) { $(tr).css('left', tr.absolute_position.left); $(tr).css('top', tr.absolute_position.top); $(tr).css('width', tr.absolute_position.width); $(tr).css('height', tr.absolute_position.height); $(tr).css('position', 'absolute'); }); $.each(_dg.selector.find('.kui-tr'), function (i, tr) { var tdList = $(tr).find('.kui-td'); $.each(tdList, function (j, td) { td.absolute_position = { left: $(td).position().left, top: $(td).position().top, width: $(td).width(), height: $(td).height() } //console.log(td.absolute_position.top); }); }); $.each(_dg.selector.find('.kui-tr'), function (i, tr) { var tdList = $(tr).find('.kui-td'); $.each(tdList, function (j, td) { $(td).css('left', td.absolute_position.left); $(td).css('top', td.absolute_position.top); $(td).css('width', td.absolute_position.width); $(td).css('height', td.absolute_position.height); $(td).css('position', 'absolute'); }); }); //转为绝对定位代码结束. }, onMove: function (_drag, e) { if (_obj.selector[0].isLoading) { return { h: false, v: false }; } var trList = _dg.selector.find('.kui-tr'); var trLast = $(trList[trList.length - 1]); var trFrozen; if(frozenRowIndex>=0){ trFrozen = $(trList[frozenRowIndex]); } var trFirst; if (trList.length > frozenRowIndex + 1) { trFirst = $(trList[frozenRowIndex + 1]); } if ((_drag.dragState.vOffsetDirection == 'up' || _drag.dragState.vOffsetDirection == 'down')&&_dg.pageResult.rows.length>0) { if (!_dg.selector[0].sepContainer[0].isHidden) { _dg.selector[0].sepContainer[0].isHidden = true; _dg.selector[0].sepContainer.hide(); var tdList = _dg.selector.find('>.kui-tr>.kui-td'); $.each(tdList, function (i, td) { $(td).css('border-left-color', $(td).css('border-bottom-color')); }); } //var cssFirstTop = $(trFrozen).css('top'); //var top = cssFirstTop.substr(0, cssFirstTop.length - 2); //top = parseFloat(top); //var cssLastTop = $(trLast).css('top'); //var trLastTop = cssLastTop.substr(0, cssLastTop.length - 2); //trLastTop = parseFloat(trLastTop); //var trLastHeight = trLast.find('.kui-td').outerHeight(); //var bottom = trLastTop + trLastHeight; var moveScoll = false; if (haveDragDistance == 0) { //拖父容器滚动条 var scrollTop = _obj.selector.parent().scrollTop(); _obj.selector.parent().scrollTop(_obj.selector.parent().scrollTop() + _drag.dragState.preY - e.pageY); if (_obj.selector.parent().scrollTop() != scrollTop) { moveScoll = true; } } if (!moveScoll) {//没有拖动滚动条. var factor = dragPagerDistance / (Math.abs(haveDragDistance)*5 + dragPagerDistance); haveDragDistance += (e.pageY - _drag.dragState.preY) * factor; //console.log(haveDragDistance); $.each(trList, function (i, tr) { if (i <= frozenRowIndex) { return; } $(tr).css('top', $(tr).position().top + (e.pageY - _drag.dragState.preY) * factor); }); } //调整pagerTop if (_drag.dragState.vOffsetDirection == 'down') {//向下移动 pagerTop.show(); var _top = pagerTop.offset().top; var _height = $(trList[frozenRowIndex + 1]).offset().top - _top; pagerTop.css('height', _height); var _div = pagerTop.find('>div'); _div.css('top', _height - _div.outerHeight()); trFirst.find('>.kui-td').css('border-top-width', $(trList[0]).find('>.kui-td').css('border-bottom-width')); } if (_drag.dragState.vOffsetDirection == 'up') {//向下移动 pagerBottom.show(); trLast.find('>.kui-td').css('border-bottom-width', $(trList[0]).find('>.kui-td').css('border-bottom-width')); } if (haveDragDistance > dragPagerDistance) { pagerTop.find('i').removeClass('icon-jiantou-copy').addClass('icon-jiantou-copy-copy'); pagerTop.find('.kui-mobile-pager-top-text-1').html('松开立刻刷新...'); } else { pagerTop.find('i').removeClass('icon-jiantou-copy-copy').addClass('icon-jiantou-copy'); pagerTop.find('.kui-mobile-pager-top-text-1').html('下拉显示上一页'); } if (haveDragDistance < 0 - dragPagerDistance) { pagerBottom.find('i').removeClass('icon-jiantou-copy-copy').addClass('icon-jiantou-copy'); pagerBottom.find('.kui-mobile-pager-bottom-text-1').html('松开立刻刷新...'); } else { pagerBottom.find('i').removeClass('icon-jiantou-copy').addClass('icon-jiantou-copy-copy'); pagerBottom.find('.kui-mobile-pager-bottom-text-1').html('上拉显示下一页.'); } } if (_drag.dragState.hOffsetDirection == 'left' || _drag.dragState.hOffsetDirection == 'right') { //控制不要向右把表拖断. var tdFrozen = _obj.selector.find('>.kui-tr:eq(0)>.kui-td:eq(' + frozenColIndex + ')'); if (_obj.selector.find('>.kui-tr:eq(0)>.kui-td:eq(' + (frozenColIndex + 1) + ')').offset().left + (e.pageX - _drag.dragState.preX) > $(tdFrozen).offset().left + $(tdFrozen).width()) { return { h: false, v: false }; } //调整表格宽度 var tableWidth = _obj.selector.outerWidth(); _obj.selector.outerWidth(tableWidth + e.pageX - _drag.dragState.preX) $.each(trList, function (i, tr) { var tdList = $(tr).find('.kui-td'); $.each(tdList, function (j, td) { if (j <= frozenColIndex) { return; } var cssLeft = $(td).css('left'); var left = cssLeft.substr(0, cssLeft.length - 2); left = parseFloat(left); $(td).css('left', left + (e.pageX - _drag.dragState.preX)); }); }); } return { h: false, v: false };//不移动. }, onMoveEnd: function (_drag, e) { if (_obj.selector[0].isLoading) { return; } //alert(e.pageY - _drag.dragState.startY); var trList = _dg.selector.find('.kui-tr'); var trLast = $(trList[trList.length - 1]); var trFrozen; if (frozenRowIndex >= 0) { trFrozen = $(trList[frozenRowIndex]); } var trFirst; if (trList.length > frozenRowIndex + 1) { trFirst = $(trList[frozenRowIndex + 1]); } //alert(haveDragDistance); //最低要大于或等于30,因为如果dragMode是hOrv需要拖动达到30才能确定拖动方向. //要兼顾有垂直滚动条和没有垂直滚动条两种情况. //if (_dg.pageResult.pageNo < _dg.pageResult.pageTotal&&_drag.dragState.vOffsetDirection == 'up' && trLast.position().top + trLast.outerHeight() < _dg.selector.parent().height() - dragPagerDistance && e.pageY - _drag.dragState.startY < 0-dragPagerDistance) { if (_dg.pageResult.pageNo < _dg.pageResult.pageTotal && _drag.dragState.vOffsetDirection == 'up' && haveDragDistance < 0 - dragPagerDistance) { //加载下一页 //haveDragDistance = 0; pagerBottom.find('i')[0].outerHTML = '
'; _obj.selector[0].isLoading = true; _dg.loadData({ queryParams: { pageNo: _dg.pageResult.pageNo + 1 } }); return; } //if (_dg.pageResult.pageNo > 1&&_drag.dragState.vOffsetDirection == 'down' && trFirst.position().top>trFrozen.position().top+trFrozen.outerHeight()+dragPagerDistance&&e.pageY - _drag.dragState.startY > dragPagerDistance) { if (_dg.pageResult.pageNo > 1 && _drag.dragState.vOffsetDirection == 'down' && haveDragDistance > dragPagerDistance) { //加载上一页数据 //haveDragDistance = 0; pagerTop.find('i')[0].outerHTML = '
'; _obj.selector[0].isLoading = true; _dg.loadData({ queryParams: { pageNo: _dg.pageResult.pageNo - 1 } }); return; } //haveDragDistance = 0; pagerTop.hide(); pagerBottom.hide(); //if (trFirst) { // trFirst.find('>.kui-td').css('border-top-width', 0); //} if (_dg.selector[0].sepContainer[0].isHidden) { _dg.selector[0].sepContainer[0].isHidden = undefined; _dg.selector[0].sepContainer.show(); var tdList = _dg.selector.find('>.kui-tr>.kui-td'); $.each(tdList, function (i, td) { $(td).css('border-left-color', 'transparent'); }); } //恢复为static定位. _dg.selector.attr('style', _dg.selector[0].static_position.style); $.each(_dg.selector.find('.kui-tr'), function (i, tr) { //alert(tr.static_position.style); $(tr).attr('style', tr.static_position.style); var tdList = $(tr).find('.kui-td'); $.each(tdList, function (j, td) { $(td).attr('style', td.static_position.style); }); }); //恢复代码结束 } }); if (config.onRender) { config.onRender(_dg); } } }); _obj.config = _config; } obj.prototype.draw = function (dataList) { var _obj = this; if (_obj.config.onPreRender) { _obj.config.onPreRender(_obj); } var columns = _obj.config.columns; $(_obj.selector).html(''); if (_obj.config.showHeader) { //绘制表头 var tr = $('
').appendTo(_obj.selector); tr[0].index = -1; $.each(columns, function (i, column) { var _title = column.title; var isCheckboxColumn = !column.title && column.checkbox == true; if (isCheckboxColumn) { _title = ''; } var td = $('
' + _title + '
').appendTo(tr); if (isCheckboxColumn) { td.addClass('kui-td-checkbox'); td.find('input').click(function () { _obj.checkAll($(this).is(':checked')); }); } }); tr.append('
'); } //绘制数据行 $.each(dataList, function (dataIndex, data) { var tr = $('
').appendTo(_obj.selector); tr[0].index = dataIndex; tr[0].data = data; if (dataIndex % 2 == 1) { tr.addClass('kui-odd'); } $.each(columns, function (colIndex, column) { var formatData = null; if (column.rowNo == true) { formatData = (_obj.pageResult.pageNo - 1) * _obj.pageResult.pageSize + dataIndex + 1;//行号 } else if (column.checkbox == true) { formatData = ''; } else if (column.formatter) { formatData = column.formatter(data[column.field],data,dataIndex) } else { formatData = data[column.field]; } if (formatData == null || formatData == undefined) { formatData = ''; } var td = $('
' + formatData + '
').appendTo(tr); if (column.rowNo == true) { td.addClass('kui-td-rowno'); } if (column.checkbox == true) { td.addClass('kui-td-checkbox'); td.find('input').click(function () { if ($(this).is(':checked')) { tr.addClass('kui-tr-selected'); } else { tr.removeClass('kui-tr-selected'); } }); } if (column.hidden == true) { td.css('display', 'none'); } }); tr.append('
'); }); var trHeader = _obj.selector.find('>.kui-tr:eq(0)'); $.each(columns, function (i, column) { var td = trHeader.find('>.kui-td:eq(' + i + ')'); td.attr('kui-width', column.width); //if (column.width && column.width != 'auto') { // td.width(column.width);//column.width可能只是个比例而已,直接设置width会导致高度发生变化. //} if (column.minWidth) { td.attr('min-width', column.min_width); } if (column.fixColWidth == true) { td.attr('fixColWidth', 1); } if (column.hidden == true) { td.css('display', 'none'); } if (column.line_height == false) { td.attr('line-height', 0); } }); if (!_obj.divTableObj) { _obj.divTableObj=$c(_obj.selector).table(_obj.config); } else { _obj.divTableObj.table(); } if (_obj.config.onRender) { _obj.config.onRender(_obj); } //if (_obj.config.onLoaded) { // _obj.config.onLoaded(_obj); //} } var _obj = new obj($(this.jo), config || {}); _obj.datagrid(); return _obj; }; }