vhb-table
Version:
一个基于 vue 的 PC 端表格组件,支持增删改查、虚拟列表、虚拟树、懒加载、快捷菜单、数据校验、树形结构、打印导出、表单渲染、数据分页、弹窗、自定义模板、渲染器、贼灵活的配置项、扩展接口等...
1 lines • 14 kB
JavaScript
"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _xeUtils=_interopRequireDefault(require("xe-utils")),_vHBTable=_interopRequireDefault(require("../../v-h-b-table")),_utils=_interopRequireWildcard(require("../../tools/utils")),_util=require("../../table/src/util"),_dom=_interopRequireWildcard(require("../../tools/dom")),_log=require("../../tools/log");function _getRequireWildcardCache(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,r=new WeakMap;return(_getRequireWildcardCache=function(e){return e?r:t})(e)}function _interopRequireWildcard(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!==_typeof(e)&&"function"!=typeof e)return{default:e};var r=_getRequireWildcardCache(t);if(r&&r.has(e))return r.get(e);var i={},n=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var l=n?Object.getOwnPropertyDescriptor(e,o):null;l&&(l.get||l.set)?Object.defineProperty(i,o,l):i[o]=e[o]}return i.default=e,r&&r.set(e,i),i}function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function ownKeys(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);e&&(i=i.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,i)}return r}function _objectSpread(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?ownKeys(Object(r),!0).forEach(function(e){_defineProperty(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _iterableToArray(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,i=new Array(t);r<t;r++)i[r]=e[r];return i}function insertTreeRow(c,e,t){var d=c.tableFullTreeData,f=c.afterFullData,h=c.fullDataRowIdData,p=c.fullAllDataRowIdData,r=c.treeOpts,v=r.rowField,m=r.parentField,_=r.children,w=r.mapChildren,g=t?"push":"unshift";e.forEach(function(e){var t=e[m],r=(0,_util.getRowid)(c,e),i=t?_xeUtils.default.findTree(d,function(e){return t===e[v]},{children:w}):null;if(i){var n=i.item,o=p[(0,_util.getRowid)(c,n)],l=o?o.level:0,a=n[_];_xeUtils.default.isArray(a)||(a=n[_]=[]),a[g](e);var s={row:e,rowid:r,seq:-1,index:-1,_index:-1,$index:-1,items:a,parent:parent,level:l+1};h[r]=s,p[r]=s}else{"development"===process.env.NODE_ENV&&t&&(0,_log.warnLog)("vhb.error.unableInsert"),f[g](e),d[g](e);var u={row:e,rowid:r,seq:-1,index:-1,_index:-1,$index:-1,items:d,parent:null,level:0};h[r]=u,p[r]=u}})}var _default={methods:{_insert:function(e){return this.insertAt(e)},_insertAt:function(e,t){var r,n=this,i=this.tableFullTreeData,o=this.mergeList,l=this.afterFullData,a=this.editStore,s=this.tableFullData,u=this.treeConfig,c=this.fullDataRowIdData,d=this.fullAllDataRowIdData,f=this.treeOpts,h=f.transform,p=f.rowField,v=f.mapChildren;_xeUtils.default.isArray(e)||(e=[e]);var m=e.map(function(e){return n.defineField(Object.assign({},e))});if(t)if(-1===t)u&&h?insertTreeRow(this,m,!0):(l.push.apply(l,_toConsumableArray(m)),s.push.apply(s,_toConsumableArray(m)),o.forEach(function(e){var t=e.row,r=e.rowspan;t+r>l.length&&(e.rowspan=r+m.length)}));else if(u&&h){var _=_xeUtils.default.findTree(i,function(e){return t[p]===e[p]},{children:v});if(_){var w=_.parent,g=_.items,b=d[(0,_util.getRowid)(this,w)],y=b?b.level:0;m.forEach(function(e,t){var r=(0,_util.getRowid)(n,e);"development"===process.env.NODE_ENV&&e[f.parentField]&&w&&e[f.parentField]!==w[p]&&(0,_log.errLog)("vhb.error.errProp",["".concat(f.parentField,"=").concat(e[f.parentField]),"".concat(f.parentField,"=").concat(w[p])]),w&&(e[f.parentField]=w[p]),g.splice(_.index+t,0,e);var i={row:e,rowid:r,seq:-1,index:-1,_index:-1,$index:-1,items:g,parent:w,level:y+1};c[r]=i,d[r]=i})}else"development"===process.env.NODE_ENV&&(0,_log.warnLog)("vhb.error.unableInsert"),insertTreeRow(this,m,!0)}else{if(u)throw new Error(_utils.default.getLog("vhb.error.noTree",["insert"]));var C=-1;if(_xeUtils.default.isNumber(t)?t<l.length&&(C=t):C=l.indexOf(t),-1===C)throw new Error((0,_log.errLog)("vhb.error.unableInsert"));l.splice.apply(l,[C,0].concat(_toConsumableArray(m))),s.splice.apply(s,[s.indexOf(t),0].concat(_toConsumableArray(m))),o.forEach(function(e){var t=e.row,r=e.rowspan;C<t?e.row=t+m.length:C<t+r&&(e.rowspan=r+m.length)})}else u&&h?insertTreeRow(this,m,!1):(l.unshift.apply(l,_toConsumableArray(m)),s.unshift.apply(s,_toConsumableArray(m)),o.forEach(function(e){var t=e.row;0<t&&(e.row=t+m.length)}));return(r=a.insertList).unshift.apply(r,_toConsumableArray(m)),this.handleTableData(u&&h),u&&h||this.updateAfterDataIndex(),this.updateFooter(),this.cacheRowMap(),this.checkSelectionStatus(),this.scrollYLoad&&this.updateScrollYSpace(),this.$nextTick().then(function(){return n.updateCellAreas(),n.recalculate()}).then(function(){return{row:m.length?m[m.length-1]:null,rows:m}})},_remove:function(e){var o=this,l=this.afterFullData,n=this.tableFullData,a=this.tableFullTreeData,t=this.treeConfig,s=this.mergeList,r=this.editStore,i=this.checkboxOpts,u=this.selection,c=this.isInsertByRow,d=this.treeOpts,f=d.transform,h=r.actived,p=r.removeList,v=r.insertList,m=i.checkField,_=[];return e?_xeUtils.default.isArray(e)||(e=[e]):e=n,e.forEach(function(e){c(e)||p.push(e)}),m||e.forEach(function(e){var t=u.indexOf(e);-1<t&&u.splice(t,1)}),n===e?(e=_=n.slice(0),this.tableFullData=[],this.afterFullData=[],this.clearMergeCells()):t&&f?e.forEach(function(e){var t=(0,_util.getRowid)(o,e),r=_xeUtils.default.findTree(a,function(e){return t===(0,_util.getRowid)(o,e)},d);if(r){var i=r.items.splice(r.index,1);_.push(i[0])}var n=l.indexOf(e);-1<n&&l.splice(n,1)}):e.forEach(function(e){var t=n.indexOf(e);if(-1<t){var r=n.splice(t,1);_.push(r[0])}var i=l.indexOf(e);-1<i&&(s.forEach(function(e){var t=e.row,r=e.rowspan;i<t?e.row=t-1:i<t+r&&(e.rowspan=r-1)}),l.splice(i,1))}),h.row&&-1<e.indexOf(h.row)&&this.clearActived(),e.forEach(function(e){var t=v.indexOf(e);-1<t&&v.splice(t,1)}),this.handleTableData(t&&f),t&&f||this.updateAfterDataIndex(),this.updateFooter(),this.cacheRowMap(),this.checkSelectionStatus(),this.scrollYLoad&&this.updateScrollYSpace(),this.$nextTick().then(function(){return o.updateCellAreas(),o.recalculate()}).then(function(){return{row:_.length?_[_.length-1]:null,rows:_}})},_removeCheckboxRow:function(){var t=this;return this.remove(this.getCheckboxRecords()).then(function(e){return t.clearCheckboxRow(),e})},_removeRadioRow:function(){var t=this,e=this.getRadioRecord();return this.remove(e||[]).then(function(e){return t.clearRadioRow(),e})},_removeCurrentRow:function(){var t=this,e=this.getCurrentRecord();return this.remove(e||[]).then(function(e){return t.clearCurrentRow(),e})},_getRecordset:function(){return{insertRecords:this.getInsertRecords(),removeRecords:this.getRemoveRecords(),updateRecords:this.getUpdateRecords()}},_getInsertRecords:function(){var r=this,e=this.treeConfig,i=this.tableFullTreeData,t=this.tableFullData,n=this.treeOpts,o=this.editStore.insertList,l=[];return o.length&&(e&&n.transform?o.forEach(function(e){var t=(0,_util.getRowid)(r,e);_xeUtils.default.findTree(i,function(e){return t===(0,_util.getRowid)(r,e)},n)&&l.push(e)}):o.forEach(function(e){-1<t.indexOf(e)&&l.push(e)})),l},_getRemoveRecords:function(){return this.editStore.removeList},_getUpdateRecords:function(){var e=this.keepSource,t=this.tableFullData,r=this.isUpdateByRow,i=this.treeConfig,n=this.treeOpts,o=this.editStore;if(e){var l=o.actived,a=l.row,s=l.column;return(a||s)&&this._syncActivedCell(),i?_xeUtils.default.filterTree(t,function(e){return r(e)},n):t.filter(function(e){return r(e)})}return[]},handleActived:function(e,t){var r=this,i=this.editStore,n=this.editOpts,o=this.tableColumn,l=this.editConfig,a=this.mouseConfig,s=n.mode,u=n.activeMethod,c=i.actived,d=e.row,f=e.column,h=f.editRender,p=e.cell=e.cell||this.getCell(d,f);if((0,_utils.isEnableConf)(l)&&(0,_utils.isEnableConf)(h)&&p){if(c.row!==d||"cell"===s&&c.column!==f){var v="edit-disabled";u&&!u(_objectSpread(_objectSpread({},e),{},{$table:this}))||(a&&(this.clearSelected(t),this.clearCellAreas(t),this.clearCopyCellArea(t)),this.closeTooltip(),c.column&&this.clearActived(t),v="edit-actived",f.renderHeight=p.offsetHeight,c.args=e,c.row=d,c.column=f,"row"===s?o.forEach(function(e){return r._getColumnModel(d,e)}):this._getColumnModel(d,f),this.$nextTick(function(){r.handleFocus(e,t)})),this.emitEvent(v,{row:d,rowIndex:this.getRowIndex(d),$rowIndex:this.getVMRowIndex(d),column:f,columnIndex:this.getColumnIndex(f),$columnIndex:this.getVMColumnIndex(f)},t)}else{var m=c.column;if(a&&(this.clearSelected(t),this.clearCellAreas(t),this.clearCopyCellArea(t)),m!==f){var _=m.model;_.update&&_utils.default.setCellValue(d,m,_.value),this.clearValidate()}f.renderHeight=p.offsetHeight,c.args=e,c.column=f,setTimeout(function(){r.handleFocus(e,t)})}this.focus()}return this.$nextTick()},_getColumnModel:function(e,t){var r=t.model;t.editRender&&(r.value=_utils.default.getCellValue(e,t),r.update=!1)},_setColumnModel:function(e,t){var r=t.model;t.editRender&&r.update&&(_utils.default.setCellValue(e,t,r.value),r.update=!1,r.value=null)},_syncActivedCell:function(){var t=this,e=this.tableColumn,r=this.editStore,i=this.editOpts,n=r.actived,o=n.row,l=n.column;(o||l)&&("row"===i.mode?e.forEach(function(e){return t._setColumnModel(o,e)}):this._setColumnModel(o,l))},_clearActived:function(e){var t=this.editStore.actived,r=t.row,i=t.column;return(r||i)&&(this._syncActivedCell(),t.args=null,t.row=null,t.column=null,this.updateFooter(),this.emitEvent("edit-closed",{row:r,rowIndex:this.getRowIndex(r),$rowIndex:this.getVMRowIndex(r),column:i,columnIndex:this.getColumnIndex(i),$columnIndex:this.getVMColumnIndex(i)},e)),_vHBTable.default._valid?this.clearValidate():this.$nextTick()},_getActiveRecord:function(){var e=this.$el,t=this.editStore,r=this.afterFullData,i=t.actived,n=i.args,o=i.row;return n&&-1<r.indexOf(o)&&e.querySelectorAll(".vhb-body--column.col--actived").length?Object.assign({},n):null},_isActiveByRow:function(e){return this.editStore.actived.row===e},handleFocus:function(e){var t=e.row,r=e.column,i=e.cell,n=r.editRender;if((0,_utils.isEnableConf)(n)){var o,l=_vHBTable.default.renderer.get(n.name),a=n.autofocus,s=n.autoselect;if(a&&(o=i.querySelector(a)),!o&&l&&l.autofocus&&(o=i.querySelector(l.autofocus)),o){if(o.focus(),s)o.select();else if(_dom.browse.msie){var u=o.createTextRange();u.collapse(!1),u.select()}}else this.scrollToRow(t,r)}},_setActiveRow:function(e){return this.setActiveCell(e,_xeUtils.default.find(this.visibleColumn,function(e){return(0,_utils.isEnableConf)(e.editRender)}))},_setActiveCell:function(t,e){var r=this,i=this.editConfig,n=_xeUtils.default.isString(e)?this.getColumnByField(e):e;return t&&n&&(0,_utils.isEnableConf)(i)&&(0,_utils.isEnableConf)(n.editRender)?this.scrollToRow(t,!0).then(function(){var e=r.getCell(t,n);e&&(r.handleActived({row:t,rowIndex:r.getRowIndex(t),column:n,columnIndex:r.getColumnIndex(n),cell:e,$table:r}),r.lastCallTime=Date.now())}):this.$nextTick()},_setSelectCell:function(e,t){var r=this.tableData,i=this.editOpts,n=this.visibleColumn,o=_xeUtils.default.isString(t)?this.getColumnByField(t):t;if(e&&o&&"manual"!==i.trigger){var l=r.indexOf(e);if(-1<l){var a=this.getCell(e,o),s={row:e,rowIndex:l,column:o,columnIndex:n.indexOf(o),cell:a};this.handleSelected(s,{})}}return this.$nextTick()},handleSelected:function(e,t){var r=this,i=this.mouseConfig,n=this.mouseOpts,o=this.editOpts,l=this.editStore,a=l.actived,s=l.selected,u=e.row,c=e.column,d=i&&n.selected;return!d||s.row===u&&s.column===c||(a.row!==u||"cell"===o.mode&&a.column!==c)&&(r.clearActived(t),r.clearSelected(t),r.clearCellAreas(t),r.clearCopyCellArea(t),s.args=e,s.row=u,s.column=c,d&&r.addColSdCls(),r.focus(),t&&r.emitEvent("cell-selected",e,t)),r.$nextTick()},_getSelectedCell:function(){var e=this.editStore.selected,t=e.args,r=e.column;return t&&r?Object.assign({},t):null},_clearSelected:function(){var e=this.editStore.selected;return e.row=null,e.column=null,this.reColTitleSdCls(),this.reColSdCls(),this.$nextTick()},reColTitleSdCls:function(){var e=this.elemStore["main-header-list"];e&&_xeUtils.default.arrayEach(e.querySelectorAll(".col--title-selected"),function(e){return _dom.default.removeClass(e,"col--title-selected")})},reColSdCls:function(){var e=this.$el.querySelector(".col--selected");e&&_dom.default.removeClass(e,"col--selected")},addColSdCls:function(){var e=this.editStore.selected,t=e.row,r=e.column;if(this.reColSdCls(),t&&r){var i=this.getCell(t,r);i&&_dom.default.addClass(i,"col--selected")}}}};exports.default=_default;