');
}
return titleHtml.join("");
}
, _initHead: function () {
var that = this;
var opts = this.opts;
var $head = this.$head;
if (opts.cols) {
var theadHtmls = [''];
//获取标题深度
var titleDeep = that._titleDeep(opts.cols);
for (var deep = 1; deep <= titleDeep; deep++) {
var cols = that._colsWithTitleDeep(opts.cols, deep);
theadHtmls.push('
');
for (var colIndex = 0; colIndex < cols.length; colIndex++) {
var col = cols[colIndex];
theadHtmls.push(this._titleHtml(col, titleDeep - deep + 1));
}
theadHtmls.push('
');
}
theadHtmls.push('');
$head.html(theadHtmls.join(''));
}
var $ths = this._expandThs();
var expandCols = this._expandCols(opts.cols);
$.each($ths, function (index) {
if (!expandCols[index].width) {
expandCols[index].width = 100;
}
$.data(this, 'col-width', expandCols[index].width);
$.data(this, 'col', expandCols[index]);
});
var $mmGrid = this.$mmGrid;
var $headWrapper = this.$headWrapper;
var $bodyWrapper = this.$bodyWrapper;
if (opts.height !== 'auto') {
$bodyWrapper.height($mmGrid.height() - $headWrapper.outerHeight(true));
}
//初始化排序状态
if (opts.sortName) {
for (var colIndex = 0; colIndex < expandCols.length; colIndex++) {
var col = expandCols[colIndex];
if (col.sortName === opts.sortName || col.name === opts.sortName) {
var $th = $ths.eq(colIndex);
$.data($th.find('.mmg-title')[0], 'sortStatus', opts.sortStatus);
$th.find('.mmg-sort').addClass('mmg-' + opts.sortStatus);
}
}
}
}
, _initOptions: function () {
var opts = this.opts;
var $mmGrid = this.$mmGrid;
var $headWrapper = this.$headWrapper;
var $backboard = this.$backboard;
$mmGrid.find('a.mmg-btnBackboardDn').css({
'top': $headWrapper.outerHeight(true)
}).slideUp('fast');
var cols = this._leafCols();
if (cols) {
var bbHtml = ['
显示列
'];
for (var colIndex = 0; colIndex < cols.length; colIndex++) {
bbHtml.push('');
}
$backboard.append($(bbHtml.join('')));
}
}
, _initEvents: function () {
var that = this;
var opts = this.opts;
var $mmGrid = this.$mmGrid;
var $headWrapper = this.$headWrapper;
var $head = this.$head;
var $bodyWrapper = this.$bodyWrapper;
var $body = this.$body;
var $backboard = this.$backboard;
var $ths = this._expandThs();
var expandCols = this._expandCols(opts.cols);
var leafCols = this._leafCols();
//调整浏览器
if (opts.width === 'auto' || opts.height === 'auto' || (typeof opts.width === 'string' && opts.width.indexOf('%') === opts.width.length - 1) ||
typeof opts.height === 'string' && opts.height.indexOf('%') === opts.height.length - 1) {
$(window).on('resize', function () {
that.resize();
});
}
//滚动条事件
$bodyWrapper.on('scroll', function () {
$head.css('left', - $(this).scrollLeft());
});
//向下按钮
var $btnBackboardDn = $mmGrid.find('a.mmg-btnBackboardDn').on('click', function () {
var backboardHeight = $mmGrid.height() - $headWrapper.outerHeight(true);
if (opts.height === 'auto' && opts.backboardMinHeight !== 'auto' && backboardHeight < opts.backboardMinHeight) {
backboardHeight = opts.backboardMinHeight;
}
$backboard.height(backboardHeight);
if (opts.height === 'auto') {
$mmGrid.height($headWrapper.outerHeight(true) + $backboard.outerHeight(true));
}
$backboard.slideDown();
$btnBackboardDn.slideUp('fast');
that._hideMessage();
});
$body.on('mouseenter', function () {
$btnBackboardDn.slideUp('fast');
});
$mmGrid.on('mouseleave', function () {
$btnBackboardDn.slideUp('fast');
});
$headWrapper.on('mouseenter', function () {
if ($backboard.is(':hidden') && opts.showBackboard) {
$btnBackboardDn.slideDown('fast');
}
});
//向上按钮
$mmGrid.find('a.mmg-btnBackboardUp').on('click', function () {
$backboard.slideUp().queue(function (next) {
if (!that.rowsLength() || (that.rowsLength() === 1 && $body.find('tr.emptyRow').length === 1)) {
that._showNoData();
}
if (opts.height === 'auto') {
$mmGrid.height('auto');
}
next();
});
});
//隐藏列
$backboard.on('click', ':checkbox', function () {
var index = $backboard.find('label').index($(this).parent());
//最后一个不隐藏
var last = 1;
if (opts.checkCol) {
last = last + 1;
}
if (opts.indexCol) {
last = last + 1;
}
if ($backboard.find('label :checked').length < last) {
this.checked = true;
return;
}
var col = leafCols[index];
if (this.checked) {
col.hidden = false;
} else {
col.hidden = true;
}
var $ths = $head.find('th');
for (var colIndex = $ths.length - 1; colIndex >= 0; colIndex--) {
var $th = $ths.eq(colIndex);
var iCol = $th.data('col');
if (iCol.cols) {
var hidden = true;
var colspan = 0;
$.each(iCol.cols, function (index, item) {
if (!item.hidden) {
hidden = false;
colspan++;
}
});
//IE bug
if (colspan !== 0) {
$th.prop('colspan', colspan);
}
iCol.hidden = hidden;
}
}
that._setColsWidth();
$backboard.height($mmGrid.height() - $headWrapper.outerHeight(true));
if (opts.height !== 'auto') {
$bodyWrapper.height($mmGrid.height() - $headWrapper.outerHeight(true));
}
$mmGrid.find('a.mmg-btnBackboardDn').css({
'top': $headWrapper.outerHeight(true)
})
});
//排序事件
$head.on('click', '.mmg-title', function () {
var $this = $(this);
var $titles = $ths.find('.mmg-title');
//当前列不允许排序
var col = $this.parent().parent().data('col');
if (!col.sortable) {
return;
}
//取得当前列下一个排序状态
var sortStatus = $.data(this, 'sortStatus') === 'asc' ? 'desc' : 'asc';
//清除排序状态
$.each($titles, function () {
$.removeData(this, 'sortStatus');
});
$ths.find('.mmg-sort').removeClass('mmg-asc').removeClass('mmg-desc');
//设置当前列排序状态
$.data(this, 'sortStatus', sortStatus);
$this.siblings('.mmg-sort').addClass('mmg-' + sortStatus);
if (opts.url && opts.remoteSort) {
that.load()
} else {
that._nativeSorter($.inArray(col, leafCols), sortStatus);
that._setStyle();
}
}).on('mousedown', '.mmg-colResize', function (e) {
//调整列宽
var $resize = $(this);
var start = e.pageX;
var $colResizePointer = $mmGrid.find('.mmg-colResizePointer')
.css('left', e.pageX - $headWrapper.offset().left).show();
var scrollLeft = $head.position().left;
var $colResizePointerBefore = $mmGrid.find('.mmg-colResizePointer-before')
.css('left', $resize.parent().parent().position().left + scrollLeft).show();
//取消文字选择
document.selection && document.selection.empty && (document.selection.empty(), 1)
|| window.getSelection && window.getSelection().removeAllRanges();
document.body.onselectstart = function () {
return false;
};
$headWrapper.css('-moz-user-select', 'none');
$mmGrid.on('mousemove', function (e) {
$colResizePointer.css('left', e.pageX - $headWrapper.offset().left);
}).on('mouseup', function (e) {
//改变宽度
var $th = $resize.parent().parent();
var width = $th.width() + e.pageX - start;
$.data($th[0], 'col-width', width);
that._setColsWidth();
$headWrapper.mouseleave();
}).on('mouseleave', function () {
$mmGrid.off('mouseup').off('mouseleave').off('mousemove');
$colResizePointer.hide();
$colResizePointerBefore.hide();
document.body.onselectstart = function () {
return true;//开启文字选择
};
$headWrapper.css('-moz-user-select', 'text');
});
});
//选中事件
var $body = this.$body;
$body.on('click', 'td', function (e) {
var $this = $(this);
if ($this.find("input,select,textarea").length > 0) {
return false;
}
var event = jQuery.Event("cellSelected");
event.target = e.target;
that.$body.triggerHandler(event, [$.data($this.parent()[0], 'item'), $this.parent().index(), $this.index()]);
if (event.isPropagationStopped()) {
return;
}
if (!$this.parent().hasClass('selected')) {
that.select($this.parent().index());
} else {
that.deselect($this.parent().index());
}
});
//行双击
$body.on('dblclick', 'tr', function (e) {
var $this = $(this);
var event = jQuery.Event("rowDblclick");
event.target = e.target;
that.$body.triggerHandler(event, [$.data($this[0], 'item'), $this.index()]);
if (event.isPropagationStopped()) {
return;
}
// if(!$this.parent().hasClass('selected')){
// that.select($this.parent().index());
// }else{
// that.deselect($this.parent().index());
// }
});
$body.on('click', 'tr > td .mmg-check', function (e) {
e.stopPropagation();
var $this = $(this);
if (this.checked) {
that.select($($this.parents('tr')[0]).index());
} else {
that.deselect($($this.parents('tr')[0]).index());
}
});
//checkbox列
if (opts.checkCol) {
$head.find('th .checkAll').on('click', function () {
if (this.checked) {
that.select('all');
} else {
that.deselect('all');
}
});
}
//IE6不支持hover
if (browser.isIE6) {
$body.on('mouseenter', 'tr', function () {
$(this).toggleClass('hover');
}).on('mouseleave', 'tr', function () {
$(this).toggleClass('hover');
});
};
}
, _rowHtml: function (item, rowIndex) {
var opts = this.opts;
var expandCols = this._expandCols(opts.cols);
var leafCols = this._leafCols();
if ($.isPlainObject(item)) {
var trHtml = [];
trHtml.push('
');
for (var colIndex = 0; colIndex < leafCols.length; colIndex++) {
var col = leafCols[colIndex];
trHtml.push('