Vxe-table使用技巧总结

最近项目中需要用到多层树table+checkbox勾选功能,选了一些组件,最终决定使用vxe-table。

Vxe-table功能丰富、api也多。官方例子很多比较粗浅,我花了很多精力才弄通了一些功能点及细节。在此汇总给大伙铺铺路。官网:vxe-table v4,安装配置和一般组件库差不多,就不细说了。由于项目用的是vue2,所以vxe-table用的是v3版本。

1、树形结构配置:

官方配置的树是一个数组,需要在tree-config对象里设置transform为true,这样才会自动把数组转换为需要的树结构。

 

如果已经有一个具有树形结构的表数据,则应该设置transform为false。如下图所示:

 

2、获取全局table对象:

this.$refs[vxe-table标签上的ref属性值]

官方文档中的table方法api都可以通过该对象调用,如

this.$refs['Tree' + this.activeName][0].setCheckboxRow(row,row.checkFlag)

this.$refs['Tree' + this.activeName][0].loadData(this.middleValue)

this.$refs['Tree' + this.activeName][0].remove(firstLoop)

3、使用多选框注意事项:

在checkbox-config的checkField可以绑定选中属性字段,勾选状态为true,未勾选和半选状态为false。组件会自动渲染勾选和未勾选状态,但不会自动渲染半选状态,除非设置checkRowKeys,但这种方式只会在初始化时被触发一次,动态刷新时没有相应api触发。期间还试了很多方式都无法完美解决该问题,最后是采用slot模板插槽搞定的,我将在第七点详细介绍。

4、编辑行或单元格注意事项:

设置edit-config的mode为cell时是开启单元格编辑模式,为row时是开启行编辑模式;设置edit-config的activeMethod来决定该单元格是否允许编辑(该方法的返回值为true或false),如下图所示,它的意思是只要表格行有childes属性且该属性不为空就不能点击;edit-closed只对 edit-config 配置时有效,单元格编辑状态下被关闭时会触发该事件,可以通过该事件实现实时保存编辑功能。

  

想要编辑的列插槽主要有三个属性配置:1、field:列字段名,这里需要的是直接可以取到的字段;2、edit-render:可编辑渲染器配置项,具体用法可参考官网vxe-table v3:只对 edit-render 启用时有效,自定义可编辑组件模板,可传多个参数,如传row的话,template里的标签可以获取行数据。需要注意的是:field值和模板里的v-model值应该为同一个,才能实现双向绑定,建议提前把想要编辑的字段放到可以直接取到的层级和地方。

5、表尾数据合并问题:

配置show-footer为true可以开启表尾,再通过footer-method方法获取表尾的数据,该数据是一个二维数组。这里需要注意一个问题:表尾数据在触发方法获取后只能通过编辑单元格动态改变,通过该方法改变二维数组后是不会更新变化后的值。

6、仅显示已勾选功能讲解:

这个功能就是通过loadData方法加载去掉未勾选行列表数据和原始表数据之间切换。全局table对象下有两个字段:data和tableData,前者是动态改变了数据的整个初始化表数据,可以用来还原仅显示已勾选功能,后者是现在加载着的表数据,经过处理后可以用来激活仅显示已勾选功能。这里需要注意的是:vxe-table加载列表数据后会默认自动生成自定义行数据唯一主键字段_X_ROW_KEY,我们可以在数据处理时用上该字段,如下图所示:

7、解决初始数据半选中状态失效问题:

我采用插槽方式自定义了checkbox,它的不确定状态(半选)字段indeterminate我绑定了判断是否半选方法:只要节点是未选中状态且有选中状态的儿子节点就是true半选状态,否则为false未选中状态。

由于将checkbox自定义了,vxe-table的checkbox-change失效,所以要重新在vxe-checkbox里面绑定change事件。最简单的方式就是直接通过setCheckboxRow方法把勾选行设置勾选状态,它的好处是会把勾选节点的所有子节点一并设置是否勾选状态。

element ui表格抖动的原因是因为无论是显示列还是隐藏列,vue都需要重新计算单元格的高度和宽度,然后再重新渲染到页面上。因为这个过程被直接反馈到了页面上,所以导致了table发生闪烁。解决方法是使用element中的dolayout函数。具体实现方法如下: ```javascript <template> <el-table :data="tableData" style="width: 100%" :key="tableKey" ref="table" @selection-change="handleSelectionChange" @sort-change="handleSortChange" @filter-change="handleFilterChange" @row-click="handleRowClick" @row-dblclick="handleRowDblclick" @row-contextmenu="handleRowContextmenu" @header-click="handleHeaderClick" @header-contextmenu="handleHeaderContextmenu" @current-change="handleCurrentChange" @header-dragend="handleHeaderDragend" @expand-change="handleExpandChange" @select="handleSelect" @select-all="handleSelectAll" @cell-mouse-enter="handleCellMouseEnter" @cell-mouse-leave="handleCellMouseLeave" @cell-click="handleCellClick" @cell-dblclick="handleCellDblclick" @cell-contextmenu="handleCellContextmenu" @edit-disabled="handleEditDisabled" @edit-closed="handleEditClosed" @edit-actived="handleEditActived" @edit-canceled="handleEditCanceled" @edit-submit="handleEditSubmit" @edit-command="handleEditCommand" @edit-queue-full="handleEditQueueFull" @edit-clear="handleEditClear" @edit-validate="handleEditValidate" @edit-invalid="handleEditInvalid" @edit-valid="handleEditValid" @edit-error="handleEditError" @edit-reset="handleEditReset" @edit-render="handleEditRender" @edit-parse-value="handleEditParseValue" @edit-format-value="handleEditFormatValue" @edit-select-all="handleEditSelectAll" @edit-tab="handleEditTab" @edit-toggle-dropdown="handleEditToggleDropdown" @edit-click="handleEditClick" @edit-focus="handleEditFocus" @edit-blur="handleEditBlur" @edit-change="handleEditChange" @edit-input="handleEditInput" @edit-composition-start="handleEditCompositionStart" @edit-composition-update="handleEditCompositionUpdate" @edit-composition-end="handleEditCompositionEnd" @edit-custom-command="handleEditCustomCommand" @edit-custom-button-click="handleEditCustomButtonClick" @edit-custom-cell-click="handleEditCustomCellClick" @edit-custom-cell-mouseenter="handleEditCustomCellMouseenter" @edit-custom-cell-mouseleave="handleEditCustomCellMouseleave" @edit-custom-cell-contextmenu="handleEditCustomCellContextmenu" @edit-custom-cell-dblclick="handleEditCustomCellDblclick" @edit-custom-cell-focus="handleEditCustomCellFocus" @edit-custom-cell-blur="handleEditCustomCellBlur" @edit-custom-cell-keydown="handleEditCustomCellKeydown" @edit-custom-cell-keyup="handleEditCustomCellKeyup" @edit-custom-cell-keypress="handleEditCustomCellKeypress" @edit-custom-cell-edit-disabled="handleEditCustomCellEditDisabled" @edit-custom-cell-edit-closed="handleEditCustomCellEditClosed" @edit-custom-cell-edit-actived="handleEditCustomCellEditActived" @edit-custom-cell-edit-canceled="handleEditCustomCellEditCanceled" @edit-custom-cell-edit-submit="handleEditCustomCellEditSubmit" @edit-custom-cell-edit-command="handleEditCustomCellEditCommand" @edit-custom-cell-edit-queue-full="handleEditCustomCellEditQueueFull" @edit-custom-cell-edit-clear="handleEditCustomCellEditClear" @edit-custom-cell-edit-validate="handleEditCustomCellEditValidate" @edit-custom-cell-edit-invalid="handleEditCustomCellEditInvalid" @edit-custom-cell-edit-valid="handleEditCustomCellEditValid" @edit-custom-cell-edit-error="handleEditCustomCellEditError" @edit-custom-cell-edit-reset="handleEditCustomCellEditReset" @edit-custom-cell-edit-render="handleEditCustomCellEditRender" @edit-custom-cell-edit-parse-value="handleEditCustomCellEditParseValue" @edit-custom-cell-edit-format-value="handleEditCustomCellEditFormatValue" @edit-custom-cell-edit-select-all="handleEditCustomCellEditSelectAll" @edit-custom-cell-edit-tab="handleEditCustomCellEditTab" @edit-custom-cell-edit-toggle-dropdown="handleEditCustomCellEditToggleDropdown" @edit-custom-cell-edit-click="handleEditCustomCellEditClick" @edit-custom-cell-edit-focus="handleEditCustomCellEditFocus" @edit-custom-cell-edit-blur="handleEditCustomCellEditBlur" @edit-custom-cell-edit-change="handleEditCustomCellEditChange" @edit-custom-cell-edit-input="handleEditCustomCellEditInput" @edit-custom-cell-edit-composition-start="handleEditCustomCellEditCompositionStart" @edit-custom-cell-edit-composition-update="handleEditCustomCellEditCompositionUpdate" @edit-custom-cell-edit-composition-end="handleEditCustomCellEditCompositionEnd" @edit-custom-cell-edit-custom-command="handleEditCustomCellEditCustomCommand" @edit-custom-cell-edit-custom-button-click="handleEditCustomCellEditCustomButtonClick" @edit-custom-cell-edit-custom-cell-click="handleEditCustomCellEditCustomCellClick" @edit-custom-cell-edit-custom-cell-mouseenter="handleEditCustomCellEditCustomCellMouseenter" @edit-custom-cell-edit-custom-cell-mouseleave="handleEditCustomCellEditCustomCellMouseleave" @edit-custom-cell-edit-custom-cell-contextmenu="handleEditCustomCellEditCustomCellContextmenu" @edit-custom-cell-edit-custom-cell-dblclick="handleEditCustomCellEditCustomCellDblclick" @edit-custom-cell-edit-custom-cell-focus="handleEditCustomCellEditCustomCellFocus" @edit-custom-cell-edit-custom-cell-blur="handleEditCustomCellEditCustomCellBlur" @edit-custom-cell-edit-custom-cell-keydown="handleEditCustomCellEditCustomCellKeydown" @edit-custom-cell-edit-custom-cell-keyup="handleEditCustomCellEditCustomCellKeyup" @edit-custom-cell-edit-custom-cell-keypress="handleEditCustomCellEditCustomCellKeypress" @edit-custom-cell-edit-custom-cell-edit-disabled="handleEditCustomCellEditCustomCellEditDisabled" @edit-custom-cell-edit-custom-cell-edit-closed="handleEditCustomCellEditCustomCellEditClosed" @edit-custom-cell-edit-custom-cell-edit-actived="handleEditCustomCellEditCustomCellEditActived" @edit-custom-cell-edit-custom-cell-edit-canceled="handleEditCustomCellEditCustomCellEditCanceled" @edit-custom-cell-edit-custom-cell-edit-submit="handleEditCustomCellEditCustomCellEditSubmit" @edit-custom-cell-edit-custom-cell-edit-command="handleEditCustomCellEditCustomCellEditCommand" @edit-custom-cell-edit-custom-cell-edit-queue-full="handleEditCustomCellEditCustomCellEditQueueFull" @edit-custom-cell-edit-custom-cell-edit-clear="handleEditCustomCellEditCustomCellEditClear" @edit-custom-cell-edit-custom-cell-edit-validate="handleEditCustomCellEditCustomCellEditValidate" @edit-custom-cell-edit-custom-cell-edit-invalid="handleEditCustomCellEditCustomCellEditInvalid" @edit-custom-cell-edit-custom-cell-edit-valid="handleEditCustomCellEditCustomCellEditValid" @edit-custom-cell-edit-custom-cell-edit-error="handleEditCustomCellEditCustomCellEditError" @edit-custom-cell-edit-custom-cell-edit-reset="handleEditCustomCellEditCustomCellEditReset" @edit-custom-cell-edit-custom-cell-edit-render="handleEditCustomCellEditCustomCellEditRender" @edit-custom-cell-edit-custom-cell-edit-parse-value="handleEditCustomCellEditCustomCellEditParseValue" @edit-custom-cell-edit-custom-cell-edit-format-value="handleEditCustomCellEditCustomCellEditFormatValue" @edit-custom-cell-edit-custom-cell-edit-select-all="handleEditCustomCellEditCustomCellEditSelectAll" @edit-custom-cell-edit-custom-cell-edit-tab="handleEditCustomCellEditCustomCellEditTab" @edit-custom-cell-edit-custom-cell-edit-toggle-dropdown="handleEditCustomCellEditCustomCellEditToggleDropdown" @edit-custom-cell-edit-custom-cell-edit-click="handleEditCustomCellEditCustomCellEditClick" @edit-custom-cell-edit-custom-cell-edit-focus="handleEditCustomCellEditCustomCellEditFocus" @edit-custom-cell-edit-custom-cell-edit-blur="handleEditCustomCellEditCustomCellEditBlur" @edit-custom-cell-edit-custom-cell-edit-change="handleEditCustomCellEditCustomCellEditChange" @edit-custom-cell-edit-custom-cell-edit-input="handleEditCustomCellEditCustomCellEditInput" @edit-custom-cell-edit-custom-cell-edit-composition-start="handleEditCustomCellEditCustomCellEditCompositionStart" @edit-custom-cell-edit-custom-cell-edit-composition-update="handleEditCustomCellEditCustomCellEditCompositionUpdate" @edit-custom-cell-edit-custom-cell-edit-composition-end="handleEditCustomCellEditCustomCellEditCompositionEnd" @edit-custom-cell-edit-custom-cell-edit-custom-command="handleEditCustomCellEditCustomCellEditCustomCommand" @edit-custom-cell-edit-custom-cell-edit-custom-button-click="handleEditCustomCellEditCustomCellEditCustomButtonClick" @edit-custom-cell-edit-custom-cell-edit-custom-cell-click="handleEditCustomCellEditCustomCellEditCustomCellClick" @edit-custom-cell-edit-custom-cell-edit-custom-cell-mouseenter="handleEditCustomCellEditCustomCellEditCustomCellMouseenter" @edit-custom-cell-edit-custom-cell-edit-custom-cell-mouseleave="handleEditCustomCellEditCustomCellEditCustomCellMouseleave" @edit-custom-cell-edit-custom-cell-edit-custom-cell-contextmenu="handleEditCustomCellEditCustomCellEditCustomCellContextmenu" @edit-custom-cell-edit-custom-cell-edit-custom-cell-dblclick="handleEditCustomCellEditCustomCellEditCustomCellDblclick" @edit-custom-cell-edit-custom-cell-edit-custom-cell-focus="handleEditCustomCellEditCustomCellEditCustomCellFocus" @edit-custom-cell-edit-custom-cell-edit-custom-cell-blur="handleEditCustomCellEditCustomCellEditCustomCellBlur" @edit-custom-cell-edit-custom-cell-edit-custom-cell-keydown="handleEditCustomCellEditCustomCellEditCustomCellKeydown" @edit-custom-cell-edit-custom-cell-edit-custom-cell-keyup="handleEditCustomCellEditCustomCellEditCustomCellKeyup" @edit-custom-cell-edit-custom-cell-edit-custom-cell-keypress="handleEditCustomCellEditCustomCellEditCustomCellKeypress" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-disabled="handleEditCustomCellEditCustomCellEditCustomCellEditDisabled" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-closed="handleEditCustomCellEditCustomCellEditCustomCellEditClosed" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-actived="handleEditCustomCellEditCustomCellEditCustomCellEditActived" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-canceled="handleEditCustomCellEditCustomCellEditCustomCellEditCanceled" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-submit="handleEditCustomCellEditCustomCellEditCustomCellEditSubmit" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-command="handleEditCustomCellEditCustomCellEditCustomCellEditCommand" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-queue-full="handleEditCustomCellEditCustomCellEditCustomCellEditQueueFull" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-clear="handleEditCustomCellEditCustomCellEditCustomCellEditClear" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-validate="handleEditCustomCellEditCustomCellEditCustomCellEditValidate" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-invalid="handleEditCustomCellEditCustomCellEditCustomCellEditInvalid" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-valid="handleEditCustomCellEditCustomCellEditCustomCellEditValid" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-error="handleEditCustomCellEditCustomCellEditCustomCellEditError" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-reset="handleEditCustomCellEditCustomCellEditCustomCellEditReset" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-render="handleEditCustomCellEditCustomCellEditCustomCellEditRender" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-parse-value="handleEditCustomCellEditCustomCellEditCustomCellEditParseValue" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-format-value="handleEditCustomCellEditCustomCellEditCustomCellEditFormatValue" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-select-all="handleEditCustomCellEditCustomCellEditCustomCellEditSelectAll" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-tab="handleEditCustomCellEditCustomCellEditCustomCellEditTab" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-toggle-dropdown="handleEditCustomCellEditCustomCellEditCustomCellEditToggleDropdown" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-click="handleEditCustomCellEditCustomCellEditCustomCellEditClick" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-focus="handleEditCustomCellEditCustomCellEditCustomCellEditFocus" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-blur="handleEditCustomCellEditCustomCellEditCustomCellEditBlur" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-change="handleEditCustomCellEditCustomCellEditCustomCellEditChange" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-input="handleEditCustomCellEditCustomCellEditCustomCellEditInput" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-composition-start="handleEditCustomCellEditCustomCellEditCustomCellEditCompositionStart" @edit-custom-cell-edit-custom-cell-edit-custom-cell-edit-composition
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值