123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- /**
- * jqGrid extension for SubGrid Data
- * Copyright (c) 2008-2014, Tony Tomov, tony@trirand.com
- * Copyright (c) 2014-2018, Oleg Kiriljuk, oleg.kiriljuk@ok-soft-gmbh.com
- * Dual licensed under the MIT and GPL licenses:
- * http://www.opensource.org/licenses/mit-license.php
- * http://www.gnu.org/licenses/gpl-2.0.html
- **/
- /*jshint eqeqeq:false */
- /*global jQuery, define, exports, module, require */
- /*jslint eqeq: true, nomen: true, plusplus: true, unparam: true, white: true */
- (function (factory) {
- "use strict";
- if (typeof define === "function" && define.amd) {
- // AMD. Register as an anonymous module.
- define([
- "jquery",
- "./grid.base"
- ], factory);
- } else if (typeof module === "object" && module.exports) {
- // Node/CommonJS
- module.exports = function (root, $) {
- if (!root) {
- root = window;
- }
- if ($ === undefined) {
- // require("jquery") returns a factory that requires window to
- // build a jQuery instance, we normalize how we use modules
- // that require this pattern but the window provided is a noop
- // if it's defined (how jquery works)
- $ = typeof window !== "undefined" ?
- require("jquery") :
- require("jquery")(root);
- }
- require("./grid.base");
- factory($);
- return $;
- };
- } else {
- // Browser globals
- factory(jQuery);
- }
- }(function ($) {
- "use strict";
- var jgrid = $.jgrid, jqID = jgrid.jqID, base = $.fn.jqGrid;
- // begin module grid.subgrid
- var subGridFeedback = function () {
- var args = $.makeArray(arguments);
- args[0] = "subGrid" + args[0].charAt(0).toUpperCase() + args[0].substring(1);
- args.unshift("");
- args.unshift("");
- args.unshift(this.p);
- return jgrid.feedback.apply(this, args);
- },
- collapseOrExpand = function (rowid, className) {
- return this.each(function () {
- if (this.grid && rowid != null && this.p.subGrid === true) {
- var tr = $(this).jqGrid("getInd", rowid, true);
- $(tr).find(">td." + className).trigger("click");
- }
- });
- };
- jgrid.extend({
- setSubGrid: function () {
- return this.each(function () {
- var p = this.p, $self = $(this), cm = p.subGridModel[0], i,
- getIcon = function (path) {
- return $self.jqGrid("getIconRes", path);
- };
- p.subGridOptions = $.extend({
- commonIconClass: getIcon("subgrid.common"),
- plusicon: getIcon("subgrid.plus"),
- minusicon: getIcon("subgrid.minus"),
- openicon: (p.direction === "rtl" ? getIcon("subgrid.openRtl") : getIcon("subgrid.openLtr")),
- expandOnLoad: false,
- delayOnLoad: 50,
- selectOnExpand: false,
- selectOnCollapse: false,
- reloadOnExpand: true
- }, p.subGridOptions || {});
- p.colNames.unshift("");
- p.colModel.unshift({
- name: "subgrid",
- width: jgrid.cell_width ? p.subGridWidth + p.cellLayout : p.subGridWidth,
- labelClasses: "jqgh_subgrid",
- sortable: false,
- resizable: false,
- hidedlg: true,
- search: false,
- fixed: true,
- frozen: true
- });
- if (cm) {
- cm.align = $.extend([], cm.align || []);
- for (i = 0; i < cm.name.length; i++) {
- cm.align[i] = cm.align[i] || "left";
- }
- }
- });
- },
- addSubGridCell: function (pos, iRow, rowid, item) {
- var self = this[0], subGridOptions = self.p.subGridOptions,
- hasSubgrid = $.isFunction(subGridOptions.hasSubgrid) ?
- subGridOptions.hasSubgrid.call(self, { rowid: rowid, iRow: iRow, iCol: pos, data: item }) :
- true;
- return self.p == null ? "" :
- "<td role='gridcell' class='" + base.getGuiStyles.call(this, "subgrid.tdStart", hasSubgrid ? "ui-sgcollapsed sgcollapsed" : "") + "' " +
- self.formatCol(pos, iRow) + ">" +
- (hasSubgrid ? "<div class='" + base.getGuiStyles.call(this, "subgrid.buttonDiv", "sgbutton-div") +
- "'><a role='button' class='" + base.getGuiStyles.call(this, "subgrid.button", "sgbutton") +
- "'><span class='" + jgrid.mergeCssClasses(subGridOptions.commonIconClass, subGridOptions.plusicon) + "'></span></a></div>" : " ") +
- "</td>";
- },
- addSubGrid: function (pos, sind) {
- return this.each(function () {
- var ts = this, p = ts.p, cm = p.subGridModel[0],
- getSubgridStyle = function (name, calsses) {
- return base.getGuiStyles.call(ts, "subgrid." + name, calsses || "");
- },
- thSubgridClasses = getSubgridStyle("thSubgrid", "ui-th-subgrid ui-th-column ui-th-" + p.direction),
- rowSubTableClasses = getSubgridStyle("rowSubTable", "ui-subtblcell"),
- rowClasses = getSubgridStyle("row", "ui-subgrid ui-row-" + p.direction),
- tdWithIconClasses = getSubgridStyle("tdWithIcon", "subgrid-cell"),
- tdDataClasses = getSubgridStyle("tdData", "subgrid-data"),
- subGridCell = function ($tr, cell, pos1) {
- var align = cm.align[pos1],
- $td = $("<td" +
- (align ? " style='text-align:" + align + ";'" : "") +
- "></td>").html(cell);
- $tr.append($td);
- },
- fillXmlBody = function (data, $tbody) {
- var sgmap = p.xmlReader.subgrid;
- $(sgmap.root + " " + sgmap.row, data).each(function () {
- var f, i, $tr = $("<tr class='" + rowSubTableClasses + "'></tr>");
- if (sgmap.repeatitems === true) {
- $(sgmap.cell, this).each(function (j) {
- subGridCell($tr, $(this).text() || " ", j);
- });
- } else {
- f = cm.mapping || cm.name;
- if (f) {
- for (i = 0; i < f.length; i++) {
- subGridCell($tr, jgrid.getXmlData(this, f[i]) || " ", i);
- }
- }
- }
- $tbody.append($tr);
- });
- },
- fillJsonBody = function (data, $tbody) {
- var $tr, i, j, f, cur, sgmap = p.jsonReader.subgrid,
- result = jgrid.getAccessor(data, sgmap.root);
- if (result != null) {
- for (i = 0; i < result.length; i++) {
- cur = result[i];
- $tr = $("<tr class='" + rowSubTableClasses + "'></tr>");
- if (sgmap.repeatitems === true) {
- if (sgmap.cell) {
- cur = cur[sgmap.cell];
- }
- for (j = 0; j < cur.length; j++) {
- subGridCell($tr, cur[j] || " ", j);
- }
- } else {
- f = cm.mapping || cm.name;
- if (f.length) {
- for (j = 0; j < f.length; j++) {
- subGridCell($tr, jgrid.getAccessor(cur, f[j]) || " ", j);
- }
- }
- }
- $tbody.append($tr);
- }
- }
- },
- subGridXmlOrJson = function (sjxml, sbid, fullBody) {
- var $th, i, subgridTableClasses = getSubgridStyle("legacyTable", "ui-jqgrid-legacy-subgrid" +
- (p.altRows === true && $(ts).jqGrid("isBootstrapGuiStyle") ? " table-striped" : "")),
- $table = $("<table" +
- (subgridTableClasses ? " style='width:1px' role='presentation' class='" + subgridTableClasses + "'" : "") +
- "><thead></thead><tbody></tbody></table>"),
- $tr = $("<tr></tr>");
- ts.grid.endReq.call(ts);
- for (i = 0; i < cm.name.length; i++) {
- $th = $("<th class='" + thSubgridClasses + "'></th>")
- .html(cm.name[i])
- .width(cm.width[i]);
- $tr.append($th);
- }
- $tr.appendTo($table[0].tHead);
- fullBody(sjxml, $($table[0].tBodies[0]));
- $("#" + jqID(p.id + "_" + sbid)).append($table);
- return false;
- },
- populatesubgrid = function (rd) {
- var sid = $(rd).attr("id"), dp = { nd_: (new Date().getTime()) }, iCol, j;
- dp[p.prmNames.subgridid] = sid;
- if (!cm) {
- return false;
- }
- if (cm.params) {
- for (j = 0; j < cm.params.length; j++) {
- iCol = p.iColByName[cm.params[j]];
- if (iCol !== undefined) {
- dp[p.colModel[iCol].name] = $(rd.cells[iCol]).text().replace(/\ \;/ig, "");
- }
- }
- }
- if (!ts.grid.hDiv.loading) {
- ts.grid.beginReq.call(ts);
- if (!p.subgridtype) {
- p.subgridtype = p.datatype;
- }
- if ($.isFunction(p.subgridtype)) {
- p.subgridtype.call(ts, dp);
- } else {
- p.subgridtype = p.subgridtype.toLowerCase();
- }
- switch (p.subgridtype) {
- case "xml":
- case "json":
- $.ajax($.extend({
- type: p.mtype,
- url: $.isFunction(p.subGridUrl) ? p.subGridUrl.call(ts, dp) : p.subGridUrl,
- dataType: p.subgridtype,
- context: sid,
- data: jgrid.serializeFeedback.call(ts, p.serializeSubGridData, "jqGridSerializeSubGridData", dp),
- success: function (data) {
- subGridXmlOrJson(
- data,
- this,
- p.subgridtype === "xml" ? fillXmlBody : fillJsonBody
- );
- },
- error: function (jqXHR, textStatus, errorThrown) {
- var loadError = p.loadSubgridError === undefined ?
- p.loadError :
- p.loadSubgridError;
- ts.grid.endReq.call(ts);
- if ($.isFunction(loadError)) {
- loadError.call(ts, jqXHR, textStatus, errorThrown);
- }
- // for compatibility only
- if (!p.subGridOptions.noEmptySubgridOnError) {
- subGridXmlOrJson(
- null,
- this,
- p.subgridtype === "xml" ? fillXmlBody : fillJsonBody
- );
- }
- }
- }, jgrid.ajaxOptions, p.ajaxSubgridOptions || {}));
- break;
- }
- }
- return false;
- },
- onClick = function () {
- var tr = $(this).parent("tr")[0], r = tr.nextSibling, rowid = tr.id, subgridDivId = p.id + "_" + rowid, atd,
- iconClass = function (iconName) {
- return jgrid.mergeCssClasses(p.subGridOptions.commonIconClass, p.subGridOptions[iconName]);
- },
- nhc = 1;
- $.each(p.colModel, function () {
- if (this.hidden === true || this.name === "rn" || this.name === "cb") {
- // ??? probably one should don't calculate hidden columns of subgrid?
- // (remove this.hidden === true part from the if) ???
- nhc++;
- }
- });
- if ($(this).hasClass("sgcollapsed")) {
- if (p.subGridOptions.reloadOnExpand === true || (p.subGridOptions.reloadOnExpand === false && !$(r).hasClass("ui-subgrid"))) {
- atd = pos >= 1 ? "<td colspan='" + pos + "'> </td>" : "";
- if (!subGridFeedback.call(ts, "beforeExpand", subgridDivId, rowid)) {
- return;
- }
- $(tr).after("<tr role='row' class='" + rowClasses + "'>" + atd + "<td class='" + tdWithIconClasses +
- "'><span class='" + iconClass("openicon") + "'></span></td><td colspan='" + parseInt(p.colNames.length - nhc, 10) +
- "' class='" + tdDataClasses + "'><div id='" + subgridDivId + "' class='tablediv'></div></td></tr>");
- $(ts).triggerHandler("jqGridSubGridRowExpanded", [subgridDivId, rowid]);
- if ($.isFunction(p.subGridRowExpanded)) {
- p.subGridRowExpanded.call(ts, subgridDivId, rowid);
- } else {
- populatesubgrid(tr);
- }
- } else {
- $(r).show();
- }
- $(this).html(
- "<div class='" + base.getGuiStyles.call(ts, "subgrid.buttonDiv", "sgbutton-div") +
- "'><a role='button' class='" + base.getGuiStyles.call(ts, "subgrid.button", "sgbutton") +
- "'><span class='" + iconClass("minusicon") + "'></span></a></div>"
- ).removeClass("sgcollapsed").addClass("sgexpanded");
- if (p.subGridOptions.selectOnExpand) {
- $(ts).jqGrid("setSelection", rowid);
- }
- } else if ($(this).hasClass("sgexpanded")) {
- if (!subGridFeedback.call(ts, "beforeCollapse", subgridDivId, rowid)) {
- return;
- }
- if (p.subGridOptions.reloadOnExpand === true) {
- $(r).remove(".ui-subgrid");
- } else if ($(r).hasClass("ui-subgrid")) { // incase of dynamic deleting
- $(r).hide();
- }
- $(this).html(
- "<div class='" + base.getGuiStyles.call(ts, "subgrid.buttonDiv", "sgbutton-div") +
- "'><a role='button' class='" + base.getGuiStyles.call(ts, "subgrid.button", "sgbutton") +
- "'><span class='" + iconClass("plusicon") + "'></span></a></div>"
- ).removeClass("sgexpanded").addClass("sgcollapsed");
- if (p.subGridOptions.selectOnCollapse) {
- $(ts).jqGrid("setSelection", rowid);
- }
- }
- return false;
- },
- len,
- tr1,
- $td1,
- iRow = 1;
- if (!ts.grid) {
- return;
- }
- len = ts.rows.length;
- if (sind !== undefined && sind > 0) {
- iRow = sind;
- len = sind + 1;
- }
- while (iRow < len) {
- tr1 = ts.rows[iRow];
- if ($(tr1).hasClass("jqgrow")) {
- $td1 = $(tr1.cells[pos]);
- if ($td1.hasClass("ui-sgcollapsed")) {
- if (p.scroll) {
- $td1.off("click");
- }
- $td1.on("click", onClick);
- }
- }
- iRow++;
- }
- if (p.subGridOptions.expandOnLoad === true) {
- var iColSubgrid = p.iColByName.subgrid;
- $(ts.rows).filter(".jqgrow").each(function (index, row) {
- $(row.cells[iColSubgrid])
- .filter(".sgcollapsed")
- .children(".sgbutton-div")
- .children(".sgbutton")
- .click();
- });
- }
- ts.subGridXml = function (xml, sid) {
- return subGridXmlOrJson(xml, sid, fillXmlBody);
- };
- ts.subGridJson = function (json, sid) {
- return subGridXmlOrJson(json, sid, fillJsonBody);
- };
- });
- },
- expandSubGridRow: function (rowid) {
- return collapseOrExpand.call(this, rowid, "sgcollapsed");
- },
- collapseSubGridRow: function (rowid) {
- return collapseOrExpand.call(this, rowid, "sgexpanded");
- },
- toggleSubGridRow: function (rowid) {
- return collapseOrExpand.call(this, rowid, "ui-sgcollapsed");
- }
- });
- // end module grid.subgrid
- }));
|