📄 zpgrid-xml.js
字号:
/** * @fileoverview Plugin for Zapatec Grid to input grid data from XML source. * * <pre> * Copyright (c) 2004-2006 by Zapatec, Inc. * http://www.zapatec.com * 1700 MLK Way, Berkeley, California, * 94709, U.S.A. * All rights reserved. * </pre> *//* $Id: zpgrid-xml.js 7649 2007-08-03 14:15:03Z alex $ *//** * Loads data from XML source. * * @private * @param {object} oDoc Input XMLDocument object. */Zapatec.Grid.prototype.loadDataXml = function(oDoc) { // Check arguments if (!oDoc) { return; } var oDocEl = oDoc.documentElement; if (!oDocEl) { return; } var oUtils = Zapatec.Utils; var fFC = oUtils.getFirstChild; var oTable = fFC(oDocEl, 'table'); if (!oTable) { return; } var oFields = fFC(oTable, 'fields'); if (!oFields) { return; } // Remove old data var oData = this.data = this.newDataXml(oTable); var aFields = this.fields = oData.fields; this.rows = oData.rows; this.rowsIndex = []; // Set primary key column this.primaryKeyColumn = oData.primaryKey; // Set page if (typeof oData.currentPage != 'undefined') { this.setCurrentPage(oData.currentPage); } else { this.setCurrentPage(0); } // Get fields var oCell = fFC(oFields, 'field'); var fNext = oUtils.getNextSibling; while (oCell) { // Add field aFields.push(this.newFieldXml(oCell)); // Next field oCell = fNext(oCell, 'field'); } // Get rows var oRows = fFC(oTable, 'rows'); if (oRows) { // Append rows oDocEl.setAttribute('norefresh', 'true'); this.spliceXml(oDoc); } // Show grid this.show();};/** * Creates new data object from XML source. * * @private * @param {object} oTable Source object * @return Data object * @type object */Zapatec.Grid.prototype.newDataXml = function(oTable) { // Create data object var oData = { fields: [], rows: [] }; // Get attributes var sStyle = oTable.getAttribute('style'); if (sStyle) { oData.style = sStyle; } var sHeaderStyle = oTable.getAttribute('headerstyle'); if (!sHeaderStyle) { // For backward compatibility sHeaderStyle = oTable.getAttribute('headerStyle'); } if (sHeaderStyle) { oData.headerStyle = sHeaderStyle; } var sPrimaryKey = oTable.getAttribute('primarykey'); if (sPrimaryKey) { oData.primaryKey = sPrimaryKey; } var sTotalRows = oTable.getAttribute('totalrows'); if (!sTotalRows) { // For backward compatibility sTotalRows = oTable.getAttribute('totalRows'); } if (sTotalRows) { // Convert to number oData.totalRows = sTotalRows * 1; } var sDisplayedRows = oTable.getAttribute('displayedrows'); if (!sDisplayedRows) { // For backward compatibility sDisplayedRows = oTable.getAttribute('displayedRows'); } if (sDisplayedRows) { // Convert to number oData.displayedRows = sDisplayedRows * 1; } var sCurrentPage = oTable.getAttribute('currentpage'); if (!sCurrentPage) { // For backward compatibility sCurrentPage = oTable.getAttribute('currentPage'); } if (sCurrentPage) { // Convert to number oData.currentPage = sCurrentPage * 1; } return oData;};/** * Returns child text of the first child with a specified tag of the given * parent element. * * @private * @param {object} oParent Parent element * @param {string} sTag Tag of child element * @return Child text of child element or undefined if not found * @type string */Zapatec.Grid.getFirstChildText = function(oParent, sTag) { var oChild = Zapatec.Utils.getFirstChild(oParent, sTag); if (oChild) { var sText = Zapatec.Utils.getChildText(oChild); if (typeof sText == 'string' && sText.length) { return sText; } }};/** * Creates new field object from XML source. * * @private * @param {object} oCell Source object * @return Field object * @type object */Zapatec.Grid.prototype.newFieldXml = function(oCell) { var oUtils = Zapatec.Utils; var fFC = oUtils.getFirstChild; var fCT = oUtils.getChildText; var fFCT = Zapatec.Grid.getFirstChildText; // title var oEl = fFC(oCell, 'title'); // Create field object var oFld = { i: this.fields.length, title: fCT(oEl) }; // dataType var val = fFCT(oCell, 'datatype'); if (val) { oFld.dataType = val; } // columnWidth val = oCell.getAttribute('width'); if (val) { // Convert to string val += ''; if (val.length) { oFld.columnWidth = val; } } // style val = oCell.getAttribute('style'); if (val) { oFld.style = val; } // span val = oCell.getAttribute('span'); if (val) { oFld.span = val * 1; } // spanTitle val = oCell.getAttribute('spantitle'); if (!val) { // For backward compatibility val = oCell.getAttribute('spanTitle'); } if (val) { oFld.spanTitle = val; } // spanStyle val = oCell.getAttribute('spanstyle'); if (!val) { // For backward compatibility val = oCell.getAttribute('spanStyle'); } if (val) { oFld.spanStyle = val; } // hidden val = (oCell.getAttribute('hidden') == 'true'); if (val) { oFld.hidden = val; } // nosort val = (oCell.getAttribute('nosort') == 'true'); if (val) { oFld.nosort = val; } // sortByColumn val = oCell.getAttribute('sortbycolumn') + ''; if (val.length && val != 'null' && val != 'undefined') { oFld.sortByColumn = val * 1; } // hiddenValues oEl = fFC(oCell, 'hiddenvalues'); if (oEl) { oFld.hiddenValues = []; var aVals = oEl.getElementsByTagName('hiddenval'); var iVals = aVals.length; for (var iVal = 0; iVals--; iVal++) { oFld.hiddenValues.push(fCT(aVals[iVal])); } } // minValue val = fFCT(oCell, 'minlimit'); if (val) { oFld.minValue = val; } // maxValue val = fFCT(oCell, 'maxlimit'); if (val) { oFld.maxValue = val; } // regexpFilter val = fFCT(oCell, 'regexpfilter'); if (val) { oFld.regexpFilter = val; } // textFilter val = fFCT(oCell, 'textfilter'); if (val) { oFld.textFilter = val; } // columnRange oEl = fFC(oCell, 'columnrange'); if (oEl) { var oCR = oFld.columnRange = {}; var oVal = this.convertCellByField(oFld, { v: fFCT(oEl, 'minvalue') }); oCR.min = this.getCellValueCompare(oVal); oCR.minValue = this.getCellValue(oVal); oCR.minOrig = this.getCellValueOriginal(oVal); oVal = this.convertCellByField(oFld, { v: fFCT(oEl, 'maxvalue') }); oCR.max = this.getCellValueCompare(oVal); oCR.maxValue = this.getCellValue(oVal); oCR.maxOrig = this.getCellValueOriginal(oVal); oCR.values = []; var aVals = oEl.getElementsByTagName('uniqueval'); var iVals = aVals.length; var aCRVals = oCR.values; for (var iVal = 0; iVals--; iVal++) { aCRVals.push(this.convertCellByField(oFld, { v: fCT(aVals[iVal]) })); } } return oFld;};/** * Changes content of the grid, replacing, adding or removing rows. * * <pre> * Input XML format: * * <xmp> * <?xml version="1.0"?> * <grid norefresh="true"> * <table> * <atkey>Primary key value</atkey> * <atid>0</atid> * <atrow>0</atrow> * <afterkey>Primary key value</afterkey> * <afterid>0</afterid> * <afterrow>0</atrow> * <howmany>0</howmany> * <rows> * <row style="background: #eee"> * <cell style="color: #f00">Value</cell> * ... * </row> * ... * </rows> * </table> * ... * </grid> * </xmp> * * Where: * * "norefresh" attribute [string, optional] indicates that grid should not be * refreshed after changing (useful when several changes go one by one). * * "atkey" tag [string, optional] defines primary key value at which to start * changing the grid. * * "atid" tag [number, optional] defines id of row at which to start changing * the grid. * * "atrow" tag [number, optional, private] defines index of row in rows array at * which to start changing the grid. * * "afterkey" tag [string, optional] defines primary key value after which to * start changing the grid. * * "afterid" tag [number, optional] defines id of row after which to start * changing the grid. * * "afterrow" tag [number, optional, private] defines index of row in rows array * after which to start changing the grid. * * "howmany" tag [number, optional] defines number of rows to replace or remove * (default is 0). * * Only one of "atkey", "atid", "atrow", "afterkey", "afterid" and "afterrow" * tags should be defined. If none of them is defined, new rows will be added to * the end of grid. * * There can be several table tags in the input XML. This is useful when several * changes must be done simultaneously. * * XMLDocument object can be obtained from XML fragment using * Zapatec.Transport.parseXml function.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -