在公司開發大型專案中,使用主流表格庫 vxe-table 和 element-ui 元件庫,可以在可編輯單元格中渲染非常簡單的使用更多的元件
官網:https://vxetable.cn
安裝
npm install vxe-pc-ui@3.3.9 vxe-table@3.11.10 @vxe-ui/plugin-render-element@3.0.0
// ...
import VxeUI from 'vxe-pc-ui'
import 'vxe-pc-ui/lib/style.css'
import VxeUITable from 'vxe-table'
import 'vxe-table/lib/style.css'
import VxeUIPluginRenderElement from '@vxe-ui/plugin-render-element'
import '@vxe-ui/plugin-render-element/dist/style.css'
// ...
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
VxeUI.use(VxeUIPluginRenderElement)
Vue.use(VxeUI)
Vue.use(VxeUITable)
Vue.use(Element)
使用輸入框 el-input
<template>
<div>
<vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script>
export default {
data () {
const gridOptions = {
border: true,
showOverflow: true,
keepSource: true,
editConfig: {
trigger: 'click',
mode: 'row'
},
columns: [
{ type: 'checkbox', width: 60 },
{ type: 'seq', title: 'Number', width: 80 },
{ field: 'name', title: 'Name', minWidth: 140, editRender: { name: 'ElInput' } },
{ field: 'nickname', title: '輸入框', width: 200, editRender: { name: 'ElInput' } }
],
data: [
{ id: 10001, name: 'Test1', nickname: 'Nickname11' },
{ id: 10002, name: 'Test2', nickname: '' }
]
}
return {
gridOptions
}
}
}
</script>
使用下拉框 el-select
<template>
<div>
<vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script>
export default {
data () {
const sexEditRender = {
name: 'ElSelect',
options: [
{ label: '男', value: '1' },
{ label: '女', value: '0' }
]
}
const sexListEditRender = {
name: 'ElSelect',
props: {
multiple: true
},
options: [
{ label: '男', value: '1' },
{ label: '女', value: '0' }
]
}
const gridOptions = {
border: true,
showOverflow: true,
keepSource: true,
editConfig: {
trigger: 'click',
mode: 'row'
},
columns: [
{ type: 'checkbox', width: 60 },
{ type: 'seq', title: 'Number', width: 80 },
{ field: 'name', title: 'Name', minWidth: 140, editRender: { name: 'ElInput' } },
{ field: 'sex', title: '下拉框', width: 200, editRender: sexEditRender },
{ field: 'sexList', title: '下拉框多選', width: 200, editRender: sexListEditRender }
],
data: [
{ id: 10001, name: 'Test1', sex: '1', sexList: [] },
{ id: 10002, name: 'Test2', sex: '', sexList: ['0', '1'] }
]
}
return {
gridOptions
}
}
}
</script>
使用日期選擇 el-date-picker
<template>
<div>
<vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script>
export default {
data () {
const gridOptions = {
border: true,
showOverflow: true,
keepSource: true,
editConfig: {
trigger: 'click',
mode: 'row'
},
columns: [
{ type: 'checkbox', width: 60 },
{ type: 'seq', title: 'Number', width: 80 },
{ field: 'name', title: 'Name', minWidth: 140, editRender: { name: 'ElInput' } },
{ field: 'date1', title: '日期', width: 200, editRender: { name: 'ElDatePicker', props: { type: 'date', valueFormat: 'yyyy-MM-dd' } } },
{ field: 'date2', title: '日期帶時間', width: 220, editRender: { name: 'ElDatePicker', props: { type: 'datetime', valueFormat: 'yyyy-MM-dd HH:mm:ss' } } }
],
data: [
{ id: 10001, name: 'Test1', date1: '', date2: '' },
{ id: 10002, name: 'Test2', date1: '2018-01-01', date2: '2018-01-01 10:10:30' }
]
}
return {
gridOptions
}
}
}
</script>
使用級聯選擇 el-cascader
<template>
<div>
<vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script>
export default {
data () {
const regionList = [
{
label: '北京',
value: 1,
children: [
{ value: 3, label: '東城區' },
{ value: 4, label: '西城區' }
]
},
{
label: '上海',
value: 21,
children: [
{ value: 23, label: '黃浦區' },
{ value: 24, label: '盧灣區' }
]
},
{
label: '廣東',
value: 42,
children: [
{ value: 43, label: '廣州市' },
{ value: 67, label: '深圳市' }
]
}
]
const regionEditRender = {
name: 'ElCascader',
props: {
options: regionList
}
}
const gridOptions = {
border: true,
showOverflow: true,
keepSource: true,
editConfig: {
trigger: 'click',
mode: 'row'
},
columns: [
{ type: 'checkbox', width: 60 },
{ type: 'seq', title: 'Number', width: 80 },
{ field: 'name', title: 'Name', minWidth: 140, editRender: { name: 'ElInput' } },
{ field: 'region', title: '級聯選擇', width: 200, editRender: regionEditRender }
],
data: [
{ id: 10001, name: 'Test1', region: [] },
{ id: 10002, name: 'Test2', region: [21, 24] }
]
}
return {
gridOptions,
regionList,
regionEditRender
}
}
}
</script>
還有非常的多,具體需要去看官網文件了
https://gitee.com/xuliangzhan/vxe-table