📄 zpgrid-core.js
字号:
* Only one of "atKey", "atRowId", "atRow", "afterKey", "afterRowId" and * "afterRow" properties should be defined. If none of them is defined, new rows * will be added to the end of grid. * * Several input objects can be passed in an array. This is useful when several * changes must be done simultaneously. * * If several objects are passed in an array, "noRefresh" property is ignored * for all objects except the last. * </pre> * * @param {object} aData Array of objects or single object in above shown format * @return Array with replaced or removed row objects. Number of replaced or * removed rows can be accessed through the length property of this array. If * error occured, returns undefined. * @type object */Zapatec.Grid.prototype.splice = function(aData) { // Check arguments if (!aData) { return; } if (!(aData instanceof Array)) { aData = [aData]; } // Fire event this.fireEvent('gridPrepareModify'); // Will hold removed rows var aRemoved = []; // Process arguments var iDataLen = aData.length; var oData; for (var iData = 0; iData < iDataLen; iData++) { // Get arguments oData = aData[iData]; // Get insert position var iInsertPos = null; // Transform primary key into id if (typeof oData.atKey != 'undefined') { var iRowId = this.getRowIdByPrimaryKey(oData.atKey); if (typeof iRowId != 'undefined') { oData.atRowId = iRowId; } } // Find row number by id if ((typeof oData.atRowId == 'string' && oData.atRowId.length) || typeof oData.atRowId == 'number') { iInsertPos = this.getRowIndexById(oData.atRowId); } if (typeof iInsertPos != 'number') { if ((typeof oData.atRow == 'string' && oData.atRow.length) || typeof oData.atRow == 'number') { var iRowNum = oData.atRow * 1; if (typeof this.rows[iRowNum] != 'undefined') { // Before specified row iInsertPos = iRowNum; } } } if (typeof iInsertPos != 'number') { // Transform primary key into id if (typeof oData.afterKey != 'undefined') { var iRowId = this.getRowIdByPrimaryKey(oData.afterKey); if (typeof iRowId != 'undefined') { oData.afterRowId = iRowId; } } // Find row number by id if ((typeof oData.afterRowId == 'string' && oData.afterRowId.length) || typeof oData.afterRowId == 'number') { iInsertPos = this.getRowIndexById(oData.afterRowId); if (typeof iInsertPos == 'number') { iInsertPos++; } } } if (typeof iInsertPos != 'number') { if ((typeof oData.afterRow == 'string' && oData.afterRow.length) || typeof oData.afterRow == 'number') { var iRowNum = oData.afterRow * 1; if (typeof this.rows[iRowNum] != 'undefined') { // After specified row iInsertPos = iRowNum + 1; } } } // Default is end of the grid if (typeof iInsertPos != 'number') { iInsertPos = this.rows.length; } // Correct rows argument if (!(oData.rows instanceof Array)) { oData.rows = []; } // Correct howMany argument var iHowManyToRemove = parseInt(oData.howMany); if (isNaN(iHowManyToRemove)) { iHowManyToRemove = 0; } // Prevent rebuilding of primary key after each remove var oPrimaryKey = this.primaryKey; this.primaryKey = null; // Indicates that primay key must be rebuilt var bRebuildPrimaryKey = false; // Update rows var iRow = 0; var iRemoved = 0; while (iRemoved < iHowManyToRemove && iRow < oData.rows.length) { var oGridRow = this.rows[iInsertPos]; if (typeof oGridRow == 'undefined') { // Trying to remove more rows than there are in the grid break; } // Save old row object aRemoved.push(Zapatec.Utils.clone(oGridRow)); // Replace row var oRow = this.prepareRow(oData.rows[iRow]); // Replace row cells for (var iCol = 0; iCol < oGridRow.cells.length; iCol++) { // Get cell var oCell = oRow.cells[iCol]; if (!oCell) { continue; } var oGridCell = oGridRow.cells[iCol]; // Check if primary key value has changed if (this.primaryKeyColumn == iCol && oGridCell.c != oCell.c) { bRebuildPrimaryKey = true; } // Replace cell values oGridCell.v = oCell.v; oGridCell.c = oCell.c; oGridCell.o = oCell.o; // Replace cell style oGridCell.style = oCell.style; } // Replace row style oGridRow.style = oRow.style; // Next row iInsertPos++; iRow++; iRemoved++; } // Delete rows for (; iRemoved < iHowManyToRemove; iRemoved++) { if (typeof this.rows[iInsertPos] == 'undefined') { // Trying to remove more rows than there are in the grid break; } var oRow = this.removeRow(iInsertPos); if (oRow) { aRemoved.push(oRow); } // Will need to rebuild primary key bRebuildPrimaryKey = true; } // Insert rows for (; iRow < oData.rows.length; iRow++) { var oRow = oData.rows[iRow]; oRow.i = this.rowsIndex.length; oRow = this.prepareRow(oRow); // Insert row this.rows.splice(iInsertPos++, 0, oRow); this.rowsIndex.push(oRow); // Will need to rebuild primary key bRebuildPrimaryKey = true; } // Rebuild or restore primary key if (bRebuildPrimaryKey) { oPrimaryKey = null; // Rebuild this.buildPrimaryKey(); } else { // Restore this.primaryKey = oPrimaryKey; oPrimaryKey = null; } } // Show updates if (!oData.noRefresh) { this.modify(); } // Return removed rows return aRemoved;};/** * Removes specified row from the grid. * * @private * @param {number} iRow Row index in rows array * @return Removed row * @type object */Zapatec.Grid.prototype.removeRow = function(iRow) { var oRow = this.rows[iRow]; if (!oRow) { return; } var undef; this.rowsIndex[oRow.i] = undef; var aRows = this.rows.splice(iRow, 1); this.rebuildPrimaryKey(); return aRows[0];};/** * Changes the content of the grid, replacing, adding or removing columns. * * <pre> * Input object format: * { * atColumnId: [number, optional] id of column at which to start changing * the grid, * afterColumnId: [number, optional] id of column after which to start * changing the grid, * howMany: [number, optional] number of columns to replace or remove * (default is 0), * fields: * [ * { * i: [number] zero-based column number, <b>dataPrepared</b> specific, * title: [string, optional] column title, * dataType: [string, optional] defines which standard or custom data type * to use for cell conversion (if not specified, no conversion is done), * columnWidth: [string, optional] column width, e.g. "10px", "10em", * style: [string, optional] header table cell style attribute, * ... * }, * ... * ], * rows: * [ * { * cells: * [ * { * v: [any] cell value to display, * c: [any, optional] cell value to compare, * o: [any, optional] original cell value, * style: [string, optional] table cell style attribute, * ... * }, * ... * ] * }, * ... * ], * noRefresh: [boolean, optional] indicates that grid should not be refreshed * after changing (default is false) (useful when several changes go one by * one) * } * * Only one of "atColumnId" and "afterColumnId" properties should be defined. If * none of them is defined, new columns will be added to the end of the grid. * * Several input objects can be passed in an array. This is useful when several * changes must be done simultaneously. * * If several objects are passed in an array, "noRefresh" property is ignored * for all objects except the last. * </pre> * * @param {object} aData Array of objects or single object in above shown format * @return Number of deleted columns. * @type number */Zapatec.Grid.prototype.spliceColumns = function(aData) { // Check arguments if (!aData) { return 0; } // Fire event this.fireEvent('gridPrepareModify'); // Process arguments var aFields = this.fields; var iFields = aFields.length; var aRows = this.rows; var iRows = aRows.length; var iPrimaryKey = this.primaryKeyColumn; var bRebuildPrimaryKey = false; if (!(aData instanceof Array)) { aData = [aData]; } var iRemoved = 0; var iDataLen = aData.length; var iData, oData, iInsertPos, iHowManyToRemove, aSpliceArgs, aColumns, iColumns, aRemoved, aUpdates, iRow, aCells, oUpdates, aNewCells; for (iData = 0; iData < iDataLen; iData++) { // Get arguments oData = aData[iData]; // Get insert position iInsertPos = parseInt(oData.atColumnId); if (isNaN(iInsertPos)) { iInsertPos = parseInt(oData.afterColumnId); if (!isNaN(iInsertPos)) { iInsertPos++; } else { // Default is after last column iInsertPos = iFields; } } // Correct howMany argument iHowManyToRemove = parseInt(oData.howMany); if (isNaN(iHowManyToRemove)) { iHowManyToRemove = 0; } // Arguments for splice method aSpliceArgs = [iInsertPos, iHowManyToRemove]; // Replace fields aColumns = oData.fields; if (!(aColumns instanceof Array)) { aColumns = [aColumns]; } iColumns = aColumns.length; aRemoved = aFields.splice.apply(aFields, aSpliceArgs.concat(aColumns)); if (aRemoved && aRemoved.length) { iRemoved += aRemoved.length; } // Change rows aUpdates = oData.rows; if (!(aUpdates instanceof Array)) { aUpdates = []; } for (iRow = 0; iRow < iRows; iRow++) { // Get cells array aCells = aRows[iRow].cells; // Get updates and check correctness of passed data oUpdates = aUpdates[iRow]; if (!oUpdates) { oUpdates = {}; } aNewCells = oUpdates.cells; if (!(aNewCells instanceof Array)) { aNewCells = new Array(iColumns); } else if (aNewCells.length < iColumns) { aNewCells = aNewCells.concat(new Array(iColumns - aNewCells.length)); } else if (aNewCells.length > iColumns) { aNewCells.splice(iColumns, aNewCells.length - iColumns); } // Replace cells aCells.splice.apply(aCells, aSpliceArgs.concat(aNewCells)); } // Check if primary key needs to be rebuilt if (typeof iPrimaryKey == 'number' && iPrimaryKey >= iInsertPos) { if (iPrimaryKey < iInsertPos + iHowManyToRemove) { this.primaryKeyColumn = null; this.primaryKey = null; bRebuildPrimaryKey = false; } else { this.primaryKeyColumn += iColumns - iHowManyToRemove; if (this.primaryKeyColumn < 0) { this.primaryKeyColumn = null; this.primaryKey = null; bRebuildPrimaryKey = false; } else { bRebuildPrimaryKey = true; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -