(function ($) { $.jgrid.extend({ columnChooser2: function (opts) { var self = this; if (!self[0].p.groupHeader) { $(this).jqGrid("columnChooser", opts); return; } var groupHeaders; if ($("#colchooser_" + $.jgrid.jqID(self[0].p.id)).length) { return; } var selectorHtml = '
'; selectorHtml += '目前不支持调整顺序' groupHeaders = self[0].p.groupHeader.groupHeaders; var select = []; for (var i = 0; i < groupHeaders.length; i++) { selectorHtml += ''; select.push($("")); } selectorHtml += '
'; var selector = $(selectorHtml); //$('iframe', selector).each(function() { // this.contentWindow.document.write( // `${$(document.head).html()}
`); // select.push($(this.contentWindow.getElementsByTagName("select"))); //}); //var select = $('select', selector); function insert(perm, i, v) { if (i >= 0) { var a = perm.slice(); var b = a.splice(i, Math.max(perm.length - i, i)); if (i > perm.length) { i = perm.length; } a[i] = v; return a.concat(b); } } opts = $.extend({ "width": 420, "height": 420, "classname": null, "done": function (perm) { if (perm) { self.jqGrid("remapColumns", perm, true); } }, /* msel is either the name of a ui widget class that extends a multiselect, or a function that supports creating a multiselect object (with no argument, or when passed an object), and destroying it (when passed the string "destroy"). */ "msel": "multiselect", /* "msel_opts" : {}, */ /* dlog is either the name of a ui widget class that behaves in a dialog-like way, or a function, that supports creating a dialog (when passed dlog_opts) or destroying a dialog (when passed the string "destroy") */ "dlog": "dialog", "dialog_opts": { "minWidth": 470 }, /* dlog_opts is either an option object to be passed to "dlog", or (more likely) a function that creates the options object. The default produces a suitable options object for ui.dialog */ "dlog_opts": function (opts) { var buttons = {}; buttons[opts.bSubmit] = function () { opts.apply_perm(); opts.cleanup(false); }; buttons[opts.bCancel] = function () { opts.cleanup(true); }; return $.extend(true, { "buttons": buttons, "close": function () { opts.cleanup(true); }, "modal": opts.modal || false, "resizable": opts.resizable || true, "width": opts.width + 20 }, opts.dialog_opts || {}); }, /* Function to get the permutation array, and pass it to the "done" function */ "apply_perm": function () { var tempGroupHeader = self[0].p.groupHeader; self.jqGrid('destroyGroupHeader', true); self[0].p.groupHeader = null; $.each(select, function () { $('option', this).each(function () { if (this.selected) { self.jqGrid("showCol", colModel[this.value].name); } else { self.jqGrid("hideCol", colModel[this.value].name); } }); }); var perm = []; fixedCols.slice(0); $.each(select, function (i) { if ($('option:selected', this).length <= 0) { groupHeaders[i].notShow = true; } else { var startName = colModel[$('option:selected', this)[0].value].name; groupHeaders[i].startColumnName = startName; var sIndex = parseInt($('option:selected', this)[0].value); $("option:not([selected])", this).each(function () { var valueInt = parseInt(this.value); if (valueInt < sIndex) { sIndex = valueInt; groupHeaders[i].startColumnName = colModel[sIndex].name; } }); $('option:selected', this).each(function () { perm.push(parseInt(this.value, 10)); }); } }); $.each(perm, function () { delete colMap[colModel[parseInt(this, 10)].name]; }); //$.each(colMap, function () { // var ti = parseInt(this, 10); // perm = insert(perm, ti, ti); //}); if (opts.done) { opts.done.call(self, perm); } var tempGroupHeaders = groupHeaders.slice(0); for (var i = groupHeaders.length - 1; i >= 0; i--) { if (groupHeaders[i].notShow) { groupHeaders[i].notShow = false; groupHeaders.splice(i, 1); } } self.jqGrid('setGroupHeaders', tempGroupHeader); self[0].p.groupHeader.groupHeaders = tempGroupHeaders; }, /* Function to cleanup the dialog, and select. Also calls the done function with no permutation (to indicate that the columnChooser was aborted */ "cleanup": function (calldone) { call(opts.dlog, selector, 'destroy'); $.each(select, function () { call(opts.msel, this, 'destroy'); }); //call(opts.msel, select, 'destroy'); selector.remove(); if (calldone && opts.done) { opts.done.call(self); } }, "msel_opts": {} }, $.jgrid.col, opts || {}); if ($.ui) { if ($.ui.multiselect) { if (opts.msel === "multiselect") { if (!$.jgrid._multiselect) { // should be in language file alert("Multiselect plugin loaded after jqGrid. Please load the plugin before the jqGrid!"); return; } opts.msel_opts = $.extend($.ui.multiselect.defaults, opts.msel_opts); } } } if (opts.caption) { selector.attr("title", opts.caption); } if (opts.classname) { selector.addClass(opts.classname); $.each(select, function () { this.addClass(opts.classname); }); //select.addClass(opts.classname); } if (opts.width) { $(">iframe", selector).css({ "width": opts.width, "margin": "0 auto" }); $.each(select, function () { this.css("width", opts.width - 25); }); //select.css("width", opts.width); } if (opts.height) { $(">iframe", selector).css("height", (opts.height - 90) / select.length); $.each(select, function () { this.css("height", (opts.height - 90) / select.length - 26); }); //select.css("height", (opts.height - 75)/select.length); } var colModel = self.jqGrid("getGridParam", "colModel"); var colNames = self.jqGrid("getGridParam", "colNames"); var realColNames = new Array(); $.each(colNames, function (i) { var index = colNames[i].indexOf("
"); if (index >= 0) { realColNames.push(colNames[i].substring(0, index)); } else { realColNames.push(colNames[i]); } }); var colMap = {}, fixedCols = []; $.each(select, function () { this.empty(); }); //select.empty(); var startIndex; for (var j = 0; j < groupHeaders.length; j++) { startIndex = -1; for (var m = 0; m < colModel.length; m++) { if (colModel[m].name === groupHeaders[j].startColumnName) { startIndex = m; colModel[m].groupIndex = j; } else if (startIndex > -1 && m < startIndex + groupHeaders[j].numberOfColumns) { colModel[m].groupIndex = j; } } } $.each(colModel, function (i) { colMap[this.name] = i; if (this.hidedlg) { if (!this.hidden) { fixedCols.push(i); } return; } if (!colModel[i].optionHide) { $(select[this.groupIndex]).append(""); } }); function call(fn, obj) { if (!fn) { return; } if (typeof fn === 'string') { if ($.fn[fn]) { $.fn[fn].apply(obj, $.makeArray(arguments).slice(2)); } } else if ($.isFunction(fn)) { fn.apply(obj, $.makeArray(arguments).slice(2)); } } function call2(fn, obj, jQueryContext) { if (!fn) { return; } if (typeof fn === 'string') { if (jQueryContext.fn[fn]) { jQueryContext.fn[fn].apply(obj, jQueryContext.makeArray(arguments).slice(3)); } } else if ($.isFunction(fn)) { fn.apply(obj, jQueryContext.makeArray(arguments).slice(3)); } } var dopts = $.isFunction(opts.dlog_opts) ? opts.dlog_opts.call(self, opts) : opts.dlog_opts; call(opts.dlog, selector, dopts); $('iframe', selector).each(function (i) { this.contentWindow.document.write( '' + '' + '' + '' + '
'); this.contentWindow.document.close(); this.onload = function () { var that = this; var contentBody = that.contentWindow.document.body; $(contentBody).find("div").append(select[i]); var mopts = $.isFunction(opts.msel_opts) ? opts.msel_opts.call(self, opts) : opts.msel_opts; call2(opts.msel, select[i], that.contentWindow.$, mopts); }; }); var mopts = $.isFunction(opts.msel_opts) ? opts.msel_opts.call(self, opts) : opts.msel_opts; $.each(select, function () { call(opts.msel, this, mopts); }); }, showHideCol: function (colname, show) { return this.each(function () { var $t = this, fndh = false, brd = $.jgrid.cell_width ? 0 : $t.p.cellLayout, cw; if (!$t.grid) { return; } if (typeof colname === 'string') { colname = [colname]; } show = show !== "none" ? "" : "none"; var sw = show === "" ? true : false, gh = $t.p.groupHeader && (typeof $t.p.groupHeader === 'object' || $.isFunction($t.p.groupHeader)); if (gh) { $($t).jqGrid('destroyGroupHeader', false); } $(this.p.colModel).each(function (i) { if ($.inArray(this.name, colname) !== -1 && this.hidden === sw) { //if($t.p.frozenColumns === true && this.frozen === true) { // return true; //} $("tr[role=rowheader]", $t.grid.hDiv).each(function () { $(this.cells[i]).css("display", show); }); $($t.rows).each(function () { if (!$(this).hasClass("jqgroup")) { $(this.cells[i]).css("display", show); } }); if ($t.p.footerrow) { $("tr.footrow td:eq(" + i + ")", $t.grid.sDiv).css("display", show); } cw = parseInt(this.width, 10); if (show === "none") { $t.p.tblwidth -= cw + brd; } else { $t.p.tblwidth += cw + brd; } this.hidden = !sw; fndh = true; $($t).triggerHandler("jqGridShowHideCol", [sw, this.name, i]); } }); if (fndh === true) { if ($t.p.shrinkToFit === true && !isNaN($t.p.height)) { $t.p.tblwidth += parseInt($t.p.scrollOffset, 10); } $($t).jqGrid("setGridWidth", $t.p.shrinkToFit === true ? $t.p.tblwidth : $t.p.width); } if (gh) { $($t).jqGrid('setGroupHeaders', $t.p.groupHeader); } }); }, setFrozenColumns: function () { return this.each(function () { if (!this.grid) { return; } var $t = this, cm = $t.p.colModel, i = 0, len = cm.length, maxfrozen = -1, frozen = false; // TODO treeGrid and grouping Support if ($t.p.subGrid === true || $t.p.treeGrid === true || $t.p.cellEdit === true || $t.p.sortable || $t.p.scroll || $t.p.grouping) { return; } if ($t.p.rownumbers) { i++; } if ($t.p.multiselect) { i++; } // get the max index of frozen col while (i < len) { // from left, no breaking frozen if (cm[i].frozen === true) { frozen = true; maxfrozen = i; } else { break; } i++; } if (maxfrozen >= 0 && frozen) { var top = $t.p.caption ? $($t.grid.cDiv).outerHeight() : 0, hth = $(".ui-jqgrid-htable", "#gview_" + $.jgrid.jqID($t.p.id)).height(); //headers if ($t.p.toppager) { top = top + $($t.grid.topDiv).outerHeight(); } if ($t.p.toolbar[0] === true) { if ($t.p.toolbar[1] !== "bottom") { top = top + $($t.grid.uDiv).outerHeight(); } } $t.grid.fhDiv = $('
'); $t.grid.fbDiv = $('
'); $("#gview_" + $.jgrid.jqID($t.p.id)).append($t.grid.fhDiv); var htbl = $(".ui-jqgrid-htable", "#gview_" + $.jgrid.jqID($t.p.id)).clone(true); // groupheader support - only if useColSpanstyle is false if ($t.p.groupHeader) { $("tr.jqg-first-row-header, tr.jqg-third-row-header", htbl).each(function () { $("th:gt(" + maxfrozen + ")", this).remove(); }); var swapfroz = -1, fdel = -1, cs, rs; $("tr.jqg-second-row-header th", htbl).each(function () { cs = parseInt($(this).attr("colspan"), 10); rs = parseInt($(this).attr("rowspan"), 10); if (rs) { swapfroz++; fdel++; } if (cs) { swapfroz = swapfroz + cs; fdel++; } if (swapfroz === maxfrozen) { return false; } }); if (swapfroz !== maxfrozen) { fdel = maxfrozen; } $("tr.jqg-second-row-header", htbl).each(function () { $("th:gt(" + fdel + ")", this).remove(); }); } else { $("tr", htbl).each(function () { $("th:gt(" + maxfrozen + ")", this).remove(); }); } $(htbl).width(1); // resizing stuff $($t.grid.fhDiv).append(htbl) .mousemove(function (e) { if ($t.grid.resizing) { $t.grid.dragMove(e); return false; } }); if ($t.p.footerrow) { var hbd = $(".ui-jqgrid-bdiv", "#gview_" + $.jgrid.jqID($t.p.id)).height(); $t.grid.fsDiv = $('
'); $("#gview_" + $.jgrid.jqID($t.p.id)).append($t.grid.fsDiv); var ftbl = $(".ui-jqgrid-ftable", "#gview_" + $.jgrid.jqID($t.p.id)).clone(true); $("tr", ftbl).each(function () { $("td:gt(" + maxfrozen + ")", this).remove(); }); $(ftbl).width(1); $($t.grid.fsDiv).append(ftbl); } $($t).bind('jqGridResizeStop.setFrozenColumns', function (e, w, index) { var rhth = $(".ui-jqgrid-htable", $t.grid.fhDiv); $("th:eq(" + index + ")", rhth).width(w); var btd = $(".ui-jqgrid-btable", $t.grid.fbDiv); $("tr:first td:eq(" + index + ")", btd).width(w); if ($t.p.footerrow) { var ftd = $(".ui-jqgrid-ftable", $t.grid.fsDiv); $("tr:first td:eq(" + index + ")", ftd).width(w); } }); // sorting stuff $($t).bind('jqGridSortCol.setFrozenColumns', function (e, index, idxcol) { //根据idxcol无法准确定位选择排序的列,换成index定位 //var previousSelectedTh = $("tr.ui-jqgrid-labels:last th:eq(" + $t.p.lastsort + ")", $t.grid.fhDiv), newSelectedTh = $("tr.ui-jqgrid-labels:last th:eq(" + idxcol + ")", $t.grid.fhDiv); var previousSelectedTh = $("tr.ui-jqgrid-labels:last th:eq(" + $t.p.lastsort + ")", $t.grid.fhDiv), newSelectedTh = $("#listGrid_" + index); $("span.ui-grid-ico-sort", previousSelectedTh).addClass('ui-state-disabled'); $(previousSelectedTh).attr("aria-selected", "false"); $("span.ui-icon-" + $t.p.sortorder, newSelectedTh).removeClass('ui-state-disabled'); $(newSelectedTh).attr("aria-selected", "true"); if (!$t.p.viewsortcols[0]) { if ($t.p.lastsort !== idxcol) { $("span.s-ico", previousSelectedTh).hide(); $("span.s-ico", newSelectedTh).show(); } } }); // data stuff //TODO support for setRowData $("#gview_" + $.jgrid.jqID($t.p.id)).append($t.grid.fbDiv); $($t.grid.bDiv).scroll(function () { $($t.grid.fbDiv).scrollTop($(this).scrollTop()); }); if ($t.p.hoverrows === true) { $("#" + $.jgrid.jqID($t.p.id)).unbind('mouseover').unbind('mouseout'); } $($t).bind('jqGridAfterGridComplete.setFrozenColumns', function () { $("#" + $.jgrid.jqID($t.p.id) + "_frozen").remove(); $($t.grid.fbDiv).height($($t.grid.bDiv).height() - 16); var btbl = $("#" + $.jgrid.jqID($t.p.id)).clone(true); $("tr[role=row]", btbl).each(function () { $("td[role=gridcell]:gt(" + maxfrozen + ")", this).remove(); }); $(btbl).width(1).attr("id", $t.p.id + "_frozen"); $($t.grid.fbDiv).append(btbl); if ($t.p.hoverrows === true) { $("tr.jqgrow", btbl).hover( function () { $(this).addClass("ui-state-hover"); $("#" + $.jgrid.jqID(this.id), "#" + $.jgrid.jqID($t.p.id)).addClass("ui-state-hover"); }, function () { $(this).removeClass("ui-state-hover"); $("#" + $.jgrid.jqID(this.id), "#" + $.jgrid.jqID($t.p.id)).removeClass("ui-state-hover"); } ); $("tr.jqgrow", "#" + $.jgrid.jqID($t.p.id)).hover( function () { $(this).addClass("ui-state-hover"); $("#" + $.jgrid.jqID(this.id), "#" + $.jgrid.jqID($t.p.id) + "_frozen").addClass("ui-state-hover"); }, function () { $(this).removeClass("ui-state-hover"); $("#" + $.jgrid.jqID(this.id), "#" + $.jgrid.jqID($t.p.id) + "_frozen").removeClass("ui-state-hover"); } ); } btbl = null; if ($t.p.footerrow) { $($t.grid.fsDiv).remove(); $t.grid.fsDiv = null; var hbd = $(".ui-jqgrid-bdiv", "#gview_" + $.jgrid.jqID($t.p.id)).height(); $t.grid.fsDiv = $('
'); $("#gview_" + $.jgrid.jqID($t.p.id)).append($t.grid.fsDiv); var ftbl = $(".ui-jqgrid-ftable", "#gview_" + $.jgrid.jqID($t.p.id)).clone(true); $("tr", ftbl).each(function () { $("td:gt(" + maxfrozen + ")", this).remove(); }); $(ftbl).width(1); $($t.grid.fsDiv).append(ftbl); } }); if (!$t.grid.hDiv.loading) { $($t).triggerHandler("jqGridAfterGridComplete"); } $t.p.frozenColumns = true; } }); }, destroyFrozenColumns: function () { return this.each(function () { if (!this.grid) { return; } if (this.p.frozenColumns === true) { var $t = this; $($t.grid.fhDiv).remove(); $($t.grid.fbDiv).remove(); $t.grid.fhDiv = null; $t.grid.fbDiv = null; if ($t.p.footerrow) { $($t.grid.fsDiv).remove(); $t.grid.fsDiv = null; } $(this).unbind('.setFrozenColumns'); if ($t.p.hoverrows === true) { var ptr; $("#" + $.jgrid.jqID($t.p.id)).bind('mouseover', function (e) { ptr = $(e.target).closest("tr.jqgrow"); if ($(ptr).attr("class") !== "ui-subgrid") { $(ptr).addClass("ui-state-hover"); } }).bind('mouseout', function (e) { ptr = $(e.target).closest("tr.jqgrow"); $(ptr).removeClass("ui-state-hover"); }); } this.p.frozenColumns = false; } }); } }); })(jQuery);