📄 zpgrid-xml.js
字号:
* * Old input format (deprecated): * * <xmp> * <?xml version="1.0"?> * <grid> * <table> * <rows atRow="0" afterRow="0" howMany="0" noRefresh="noRefresh"> * <row style="background: #eee"> * <cell style="color: #f00">Value</cell> * ... * </row> * ... * </rows> * </table> * </grid> * </xmp> * </pre> * * @param {object} oDoc XMLDocument object that describes one or several rows to * add. * @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.spliceXml = function(oDoc) { // Check arguments if (!oDoc) { return; } var oDocEl = oDoc.documentElement; if (!oDocEl) { return; } // Get table elements var aTables = oDocEl.getElementsByTagName('table'); if (!aTables.length) { return; } // Fire event this.fireEvent('gridPrepareModify'); // Rowspan flags var aRF = []; // Will hold removed rows var aRemoved = []; // Process tables var oUtils = Zapatec.Utils; var fFC = oUtils.getFirstChild; var fFCT = Zapatec.Grid.getFirstChildText; var fClone = oUtils.clone; var fNext = oUtils.getNextSibling; var iTables = aTables.length; for (var iTable = 0; iTables--; iTable++) { // Get table var oTable = aTables[iTable]; // Get rows var oRows = fFC(oTable, 'rows'); // Get insert position var iInsertPos = null; // Try atkey tag var sAtKey = fFCT(oTable, 'atkey'); if (typeof sAtKey != 'undefined') { var iRowId = this.getRowIdByPrimaryKey(sAtKey); if (typeof iRowId != 'undefined') { iInsertPos = this.getRowIndexById(iRowId); } } // Try atid tag if (typeof iInsertPos != 'number') { var sAtId = fFCT(oTable, 'atid'); if (typeof sAtId != 'undefined') { iInsertPos = this.getRowIndexById(sAtId); } } // Try atrow tag if (typeof iInsertPos != 'number') { var sAtRow = fFCT(oTable, 'atrow'); if (typeof sAtRow != 'undefined') { var iRowNum = sAtRow * 1; if (typeof this.rows[iRowNum] != 'undefined') { iInsertPos = iRowNum; } } } // Try afterkey tag if (typeof iInsertPos != 'number') { var sAfterKey = fFCT(oTable, 'afterkey'); if (typeof sAfterKey != 'undefined') { var iRowId = this.getRowIdByPrimaryKey(sAfterKey); if (typeof iRowId != 'undefined') { iInsertPos = this.getRowIndexById(iRowId); if (typeof iInsertPos == 'number') { iInsertPos++; } } } } // Try afterid tag if (typeof iInsertPos != 'number') { var sAfterId = fFCT(oTable, 'afterid'); if (typeof sAfterId != 'undefined') { iInsertPos = this.getRowIndexById(sAfterId); if (typeof iInsertPos == 'number') { iInsertPos++; } } } // Try afterrow tag if (typeof iInsertPos != 'number') { var sAfterRow = fFCT(oTable, 'afterrow'); if (typeof sAfterRow != 'undefined') { var iRowNum = sAfterRow * 1; if (typeof this.rows[iRowNum] != 'undefined') { iInsertPos = iRowNum + 1; } } } // For backward compatibility if (typeof iInsertPos != 'number' && oRows) { var sAtRow = oRows.getAttribute('atRow'); if (typeof sAtRow == 'string' && sAtRow.length) { var iRowNum = sAtRow * 1; if (typeof this.rows[iRowNum] != 'undefined') { iInsertPos = iRowNum; } } } // For backward compatibility if (typeof iInsertPos != 'number' && oRows) { var sAfterRow = oRows.getAttribute('afterRow'); if (typeof sAfterRow == 'string' && sAfterRow.length) { var iRowNum = sAfterRow * 1; if (typeof this.rows[iRowNum] != 'undefined') { iInsertPos = iRowNum + 1; } } } // Default is end of the grid if (typeof iInsertPos != 'number') { iInsertPos = this.rows.length; } // Get howMany argument var iHowManyToRemove = 0; var sHowMany = fFCT(oTable, 'howmany'); if (typeof sHowMany != 'undefined') { iHowManyToRemove = sHowMany * 1; } else if (oRows) { // For backward compatibility iHowManyToRemove = oRows.getAttribute('howMany') * 1; } // 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 oCurRow = fFC(oRows, 'row'); var iRemoved = 0; while (iRemoved < iHowManyToRemove && oCurRow) { 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(fClone(oGridRow)); // Replace row var oRow = this.newRowXml(oCurRow, aRF); // Replace row cells var iCols = oGridRow.cells.length; for (var iCol = 0; iCols--; 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++; oCurRow = fNext(oCurRow, 'row'); 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 while (oCurRow) { // Create row var oRow = this.newRowXml(oCurRow, aRF); // Insert row this.rows.splice(iInsertPos++, 0, oRow); this.rowsIndex.push(oRow); // Will need to rebuild primary key bRebuildPrimaryKey = true; // Next row oCurRow = fNext(oCurRow, 'row'); } // Rebuild or restore primary key if (bRebuildPrimaryKey) { oPrimaryKey = null; // Rebuild this.buildPrimaryKey(); } else { // Restore this.primaryKey = oPrimaryKey; oPrimaryKey = null; } } // Prepare spans this.prepareSpans(this.rows); // Show updates. oRows is checked for backward compatibility. if (!oDocEl.getAttribute('norefresh') && !(oRows && oRows.getAttribute('noRefresh'))) { this.modify(); } // Return removed rows return aRemoved;};/** * Creates new row object from XML source. * * @private * @param {object} oCurRow Source object * @param {object} aRF Rowspan flags * @return Row object * @type object */Zapatec.Grid.prototype.newRowXml = function(oCurRow, aRF) { // Create new row object var oRow = { i: this.rowsIndex.length, cells: [] }; // Set style var sStyle = oCurRow.getAttribute('style'); if (sStyle) { oRow.style = sStyle; } // Get cells var oUtils = Zapatec.Utils; var oCurCell = oUtils.getFirstChild(oCurRow, 'cell'); // Variables used inside the loop var iCols = this.fields.length; var fNext = oUtils.getNextSibling; var aCells = oRow.cells; var iCol, oCell, iRowspan, iColspan; for (iCol = 0; iCol < iCols; iCol++) { if (aRF[iCol]) { aRF[iCol]--; continue; } // Add cell oCell = this.newCellXml(oCurCell, oRow.i, iCol); aCells[iCol] = oCell; // Consider rowspan iRowspan = oCell.rowspan; if (iRowspan > 1) { aRF[iCol] = iRowspan - 1; } // Consider colspan iColspan = oCell.colspan; if (iColspan > 1) { iCol += iColspan - 1; } // Next cell if (oCurCell) { oCurCell = fNext(oCurCell, 'cell'); } } return oRow;};/** * Creates new cell object from XML source. * * @private * @param {object} oCurCell Source object * @param {number} iRow Row id * @param {number} iCol Column id * @return Cell object * @type object */Zapatec.Grid.prototype.newCellXml = function(oCurCell, iRow, iCol) { // Create cell object var oCell = { i: iCol, r: iRow, v: '' }; if (oCurCell) { // Set value var aValue = []; var iCn = oCurCell.childNodes.length; var fSerialize = Zapatec.Transport.serializeXmlDoc; var oC; for (var iC = 0; iCn--; iC++) { oC = oCurCell.childNodes[iC]; if (oC.nodeType == 3) { // Text node aValue.push(oC.data); } else { // Element node aValue.push(fSerialize(oC)); } } oCell.v = aValue.join(''); // Set colspan var iColspan = oCurCell.getAttribute('colspan') * 1; if (iColspan > 1) { oCell.colspan = iColspan; } // Set rowspan var iRowspan = oCurCell.getAttribute('rowspan') * 1; if (iRowspan > 1) { oCell.rowspan = iRowspan; } // Set style var sStyle = oCurCell.getAttribute('style'); if (sStyle) { oCell.style = sStyle; } } // Convert cell value oCell = this.convertCell(oCell); return oCell;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -