vex-table是一款适用于pc端的UI组件,使用的于业务场景为较复杂表格;配合Vue3使用更佳;
开始及使用
安装依赖
1
| npm install xe-utils vxe-table@next
|
引入声明文件(使用的是vue3)
1 2 3 4 5 6 7 8 9
| import { App, createApp } = 'vue' import 'xe-utils' import VXETable from 'vxe-table' import 'vxe-table/lib/style.css'
function useTable (app: App) { app.use(VXETable) } createApp(App).use(useTable).mount('#app')
|
在项目文建立一个放置公共组件的文件夹并命名为components,接下来再创立一个子文件vxeFliter文件夹;文件夹下再建立一个index.tsx及filterContent.vue文件index.ts文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import VXETable from 'vxe-table'; import {createVNode } from "vue" import filterContent from './filterContent.vue';
VXETable.renderer.add('filterContent', { showFilterFooter: false, renderFilter(renderOpts, params) { return [createVNode(filterContent, { renderOpts, params })]; }, filterResetMethod(params) { const { options } = params; options.forEach((option) => { option.data = { vals: [], sVal: '' }; }); }, filterMethod(params) { const { option, row, column } = params; const { vals } = option.data; const cellValue = row[column.property]; return vals.includes(cellValue); }, });
|
filterContent文件
Ps:在index.tsx文件中 声明挂载自定义的filter的时候;之前renderFilter中的返回值必须是带有dom的标签;示例demo的写法有误差,会有ts的语法错误;导出的组件必须用createVNode的包裹一层,使用方正常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
renderFilter(renderOpts, params) { return [createVNode(filterContent, { renderOpts, params })]; },
renderFilter(renderOpts, params) { return [<filterContent params={params}} />]; },
|
最后将components/vxeFilter/index.tsx 文件引入main.ts文件
filterContent的使用
1 2 3 4 5 6 7 8 9 10 11 12
| <vxe-grid ref='xGrid' v-bind="gridOptions" :column="column"></vxe-grid>
const column= ref([ { field: 'product_code', minwidth: 100, title: 'Product', filters: [{ data: { vals: [] } }], filterRender: { name: 'filterContent' } }, ])
|