📄 zpgrid-query.js
字号:
// Return compiled statement return this.compileStatement(oValue.statement); } // Nothing applicable this.setError('Invalid statement value'); return null;};/** * Compiles operator. * * @private * @param {function or null} fLeftValue Left value * @param {function or null} fRightValue Right value * @param {string} strOperator Any javascript binary or unary operator * @return Rreference to a function that accepts 1 or 2 values and returns * result of operator evaluation on those values * @type function or null */Zapatec.GridQuery.prototype.compileOperator = function(fLeftValue, fRightValue, strOperator) { // Compile operator try { if (fLeftValue && fRightValue) { // Binary operator return new Function('l', 'r', 'return l ' + strOperator + ' r'); } if (fRightValue) { // Left unary operator return new Function('v', 'return ' + strOperator + ' v'); } if (fLeftValue) { // Right unary operator return new Function('v', 'return v ' + strOperator); } } catch(oException) { this.setError('Invalid operator: ' + strOperator); }; return null;};/** * Sets error. * * @private * @param {string} strError Human readable error description * @return Always false * @type boolean */Zapatec.GridQuery.prototype.setError = function(strError) { this.error = true; this.errorDescription = strError; return false;};/** * Refreshes grid after change. * @private */Zapatec.GridQuery.prototype.refreshGrid = function() { if (!this.noRefresh && this.grid && this.grid.show) { this.grid.modify(); }};/** * Insert query. * * @constructor * @extends Zapatec.GridQuery * @param {object} oArgs Query initialization object (see * {@link Zapatec.GridQuery} for details) */Zapatec.GridQueryInsert = function(oArgs) { // Call constructor of superclass Zapatec.GridQueryInsert.SUPERconstructor.call(this, oArgs);};// Inherit GridQueryZapatec.inherit(Zapatec.GridQueryInsert, Zapatec.GridQuery);/** * Initializes object. * * @private * @param {object} oArgs Query initialization object (see * {@link Zapatec.GridQuery} for details) * @return Success * @type boolean */Zapatec.GridQueryInsert.prototype.init = function(oArgs) { // Call parent return Zapatec.GridQueryInsert.SUPERclass.init.call(this, oArgs);};/** * Executes query. * * <pre> * Query data object format: * * { * rows: [object] array of rows to add (see {@link Zapatec.Grid#splice} for * details), * debug: [boolean, optional] if true, actual change to the grid will not be * performed * } * </pre> * * @param {object} oArgs Query data object * @return Array of added rows. Number of added rows can be accessed through the * length property of this array. If error occured during query, returns * undefined * @type object */Zapatec.GridQueryInsert.prototype.execute = function(oArgs) { if (!this.grid || this.error) { // Error return; } // Check arguments if (!oArgs || !oArgs.rows) { return; } // Insert rows if (!oArgs.debug) { this.grid.splice({ rows: oArgs.rows, noRefresh: this.noRefresh }); } // Success return oArgs.rows;};/** * Update query. * * @constructor * @extends Zapatec.GridQuery * @param {object} oArgs Query initialization object (see * {@link Zapatec.GridQuery} for details) */Zapatec.GridQueryUpdate = function(oArgs) { // Call constructor of superclass Zapatec.GridQueryUpdate.SUPERconstructor.call(this, oArgs);};// Inherit GridQueryZapatec.inherit(Zapatec.GridQueryUpdate, Zapatec.GridQuery);/** * Initializes object. * * @private * @param {object} oArgs Query initialization object (see * {@link Zapatec.GridQuery} for details) * @return Success * @type boolean */Zapatec.GridQueryUpdate.prototype.init = function(oArgs) { // Call parent return Zapatec.GridQueryUpdate.SUPERclass.init.call(this, oArgs);};/** * Executes query. * * <pre> * Query data object format: * * { * cells: [ * { * v: [any] cell value, * style: [string, optional] table cell style attribute * }, * ... * ], * style: [string, optional] table row style attribute, * debug: [boolean, optional] if true, actual change to the grid will not be * performed * } * * If only some of cells should be changed, specify only those cells. E.g. * * { * cells: [ * null, * null, * { v: value1 }, * null, * { v: value2 } * ] * } * * will change only values in 3-rd and 5-th columns. * </pre> * * @param {object} oArgs Query data object * @return Array of modified rows. Number of modified rows can be accessed * through the length property of this array. Each element of this array * contains array with copy of original row object and reference to modified * row object: [oOriginalRow, oModifiedRow]. If error occured during query, * returns undefined * @type object */Zapatec.GridQueryUpdate.prototype.execute = function(oArgs) { if (!this.grid || this.error) { // Error return; } // Check arguments if (!oArgs || !oArgs.cells) { return; } // Fire event this.grid.fireEvent('gridPrepareModify'); // Will contain modified rows var aModified = []; // Iterate over rows for (var iRow = 0; iRow < this.grid.rows.length; iRow++) { // Get row if (!(this.grid.rows[iRow] instanceof Object)) { this.grid.rows[iRow] = {}; } var oRow = this.grid.rows[iRow]; if (!(oRow.cells instanceof Array)) { oRow.cells = []; } // Update row if (this.condition(iRow)) { // Save a copy of original row and a reference to modified row aModified.push([Zapatec.Utils.clone(oRow), oRow]); // Update row if (!oArgs.debug) { for (var iCol = 0; iCol < this.grid.fields.length; iCol++) { // Get new value var oArgsCell = oArgs.cells[iCol]; if (!oArgsCell) { continue; } // Get cell if (!(oRow.cells[iCol] instanceof Object)) { oRow.cells[iCol] = {}; } var oCell = oRow.cells[iCol]; // Set new cell value oCell = this.grid.setCellValue(oCell, this.grid.getCellValue(oArgsCell)); // Set cell style oCell = this.grid.setCellStyle(oCell, oArgsCell.style); // Update cell oRow.cells[iCol] = oCell; } // Set row style oRow = this.grid.setRowStyle(oRow, oArgs.style); } } } // Rebuild primary key if (!oArgs.debug && aModified.length) { this.grid.rebuildPrimaryKey(); } // Refresh grid this.refreshGrid(); // Success return aModified;};/** * Delete query. * * @constructor * @extends Zapatec.GridQuery * @param {object} oArgs Query initialization object (see * {@link Zapatec.GridQuery} for details) */Zapatec.GridQueryDelete = function(oArgs) { // Call constructor of superclass Zapatec.GridQueryDelete.SUPERconstructor.call(this, oArgs);};// Inherit GridQueryZapatec.inherit(Zapatec.GridQueryDelete, Zapatec.GridQuery);/** * Initializes object. * * @private * @param {object} oArgs Query initialization object (see * {@link Zapatec.GridQuery} for details) * @return Success * @type boolean */Zapatec.GridQueryDelete.prototype.init = function(oArgs) { // Call parent return Zapatec.GridQueryDelete.SUPERclass.init.call(this, oArgs);};/** * Executes query. * * <pre> * Query data object format: * * { * debug: [boolean, optional] if true, actual change to the grid will not be * performed * } * </pre> * * @param {object} oArgs Query data object * @return Array of removed rows. Number of removed rows can be accessed through * the length property of this array. If error occured during query, returns * undefined * @type object */Zapatec.GridQueryDelete.prototype.execute = function(oArgs) { if (!this.grid || this.error) { // Error return; } // Fire event this.grid.fireEvent('gridPrepareModify'); // Will contain removed rows var aRemoved = []; // Iterate over rows for (var iRow = this.grid.rows.length - 1; iRow >= 0; iRow--) { // Get row var oRow = this.grid.rows[iRow]; if (!(oRow instanceof Object) || !(oRow.cells instanceof Array)) { continue; } // Remove row if (this.condition(iRow)) { if (oArgs && oArgs.debug) { // Only get copy of row without removing aRemoved.push(oRow); } else { // Remove row aRemoved.push(this.grid.removeRow(iRow)); } } } // Rows were removed in reverse order aRemoved.reverse(); // Rebuild primary key if (!(oArgs && oArgs.debug) && aRemoved.length) { this.grid.rebuildPrimaryKey(); } // Refresh grid this.refreshGrid(); // Success return aRemoved;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -