介绍
fish-select 是基于Element UI 的 Table 表格 封装的组件,简化了column的配置,大大提升了开发效率,并保持原有的表格的参数及事件。
用法
基础用法

<template><fish-table :tableData='tableData' :columns='columns' :showPage='false' sum-text='总计' ></fish-table></template><script>export default {data () {return {columns: [{prop: 'id',label: 'ID'},{prop: 'name',label: '姓名'},{prop: 'amount1',label: '数值1',sortable: true},{prop: 'amount2',label: '数值2',sortable: true,isSummary:true,},{prop: 'amount3',label: '数值3',sortable: true,isSummary:true,unit:'元',isCurrency:true}],tableData: [{id: '12987122',name: '王小虎',amount1: '234',amount2: '3',amount3: 10},{id: '12987123',name: '王小虎',amount1: '165',amount2: '4',amount3: 12},{id: '12987124',name: '王小虎',amount1: '324',amount2: '1',amount3: 9},{id: '12987125',name: '王小虎',amount1: '621',amount2: '2',amount3: 17},{id: '12987126',name: '王小虎',amount1: '539',amount2: '4',amount3: 15}]}}}</script>
多级表头、排序、多选

<template><div><fish-table:table-data="tableData"ref="mtable":columns="columns":showPage="false"selection@selection-change="selectChange"height="400":showNum="false"><template slot="date" slot-scope="scope">{{scope.row.date}}</template></fish-table><br><el-button @click="toggleSelection([tableData[1], tableData[2]])">切换第二、第三行的选中状态</el-button><el-button @click="toggleSelection()">取消选择</el-button></div></template><script>export default {data() {return {tableData: [],columns: [{prop: "date",fixed: true,sortable: true,width: "120",renderHeader: (h, scope) => {return (<span><i class="el-icon-time" />日期</span>);}},{label: "配送信息",children: [{prop: "name",width: "120",render: (h, scope) => {return <el-tag>{scope.row.name}</el-tag>;}},{label: "地址",children: [{prop: "province",label: "省份",width: "120"},{prop: "city",label: "市区",width: "120"},{prop: "address",label: "地址",sortable: true,width: "300px",renderHeader: (h, scope) => {return (<span><i class="el-icon-location-outline" />地址</span>);}}]}]}]};},created() {for (let i = 0; i < 10; i++) {this.tableData.push({date: `2016-05-${i + 1}`,name: `王小虎${i + 1}`,province: "上海",city: "普陀区",address: `上海市普陀区金沙江路 ${i + 1} 弄`,zip: 200333});}},methods: {selectChange(arr) {console.log(arr);},toggleSelection(rows) {if (rows) {this.$refs.mtable.toggleRowSelection(rows);} else {this.$refs.mtable.clearSelection();}}}};</script>
展开行

<template><fish-table :columns="tableData.column":table-data="tableData.data" expand :showNum='false' :border='false'><template slot="expand" slot-scope="{row}"><fish-form :model='row' :columns='columns' class="demo-table-expand"></fish-form></template></fish-table></template><script>export default {data () {return {columns:[{label:'商品名称',prop:'name'},{label:'所属店铺',prop:'shop'},{label:'商品 ID',prop:'id'},{label:'店铺 ID',prop:'shopId'},{label:'商品分类',prop:'category'},{label:'店铺地址',prop:'address'},{label:'商品描述',prop:'desc',span:22}],tableData: {column: [{prop: 'id',label: 'ID'},{prop: 'name',label: '商品名称'},{prop: 'desc',label: '描述'}],data: [{id: '12987122',name: '好滋好味鸡蛋仔',category: '江浙小吃、小吃零食',desc: '荷兰优质淡奶,奶香浓而不腻',address: '上海市普陀区真北路',shop: '王小虎夫妻店',shopId: '10333'},{id: '12987123',name: '好滋好味鸡蛋仔',category: '江浙小吃、小吃零食',desc: '荷兰优质淡奶,奶香浓而不腻',address: '上海市普陀区真北路',shop: '王小虎夫妻店',shopId: '10333'},{id: '12987125',name: '好滋好味鸡蛋仔',category: '江浙小吃、小吃零食',desc: '荷兰优质淡奶,奶香浓而不腻',address: '上海市普陀区真北路',shop: '王小虎夫妻店',shopId: '10333'},{id: '12987126',name: '好滋好味鸡蛋仔',category: '江浙小吃、小吃零食',desc: '荷兰优质淡奶,奶香浓而不腻',address: '上海市普陀区真北路',shop: '王小虎夫妻店',shopId: '10333'}]}}}}</script><style>.demo-table-expand label {color: #99a9bf;}</style>
合计行
自动合计只需在需要合并的列sSummary为true,unit 为单位,如果想要手动合并summaryMethod
<template><fish-table :tableData='tableData' :columns='columns' :showPage='false' sum-text='总计' ></fish-table></template><script>export default {data () {return {columns: [{prop: 'id',label: 'ID'},{prop: 'name',label: '姓名'},{prop: 'amount1',label: '数值1',sortable: true},{prop: 'amount2',label: '数值2',sortable: true,isSummary:true,},{prop: 'amount3',label: '数值3',sortable: true,isSummary:true,unit:'元',isCurrency:true}],tableData: [{id: '12987122',name: '王小虎',amount1: '234',amount2: '3',amount3: 10},{id: '12987123',name: '王小虎',amount1: '165',amount2: '4',amount3: 12},{id: '12987124',name: '王小虎',amount1: '324',amount2: '1',amount3: 9},{id: '12987125',name: '王小虎',amount1: '621',amount2: '2',amount3: 17},{id: '12987126',name: '王小虎',amount1: '539',amount2: '4',amount3: 15}]}}}</script>
合并单元格
自动合并单元格传入需要合并的行和列的字段,列的合并是根据值相同合并,行的合并是根据当前属性后的几个字段为undefined 如果要手动合并传入span-method方法
<template><fish-table :tableData='tableData' :columns='columns' :showPage='false' :showNum='false':merge-row='mergeRow':merge-column='mergeColumn'></fish-table></template><script>export default {data () {return {mergeColumn:['amount1'],mergeRow:['name'],columns: [{prop: 'id',label: 'ID'},{prop: 'name',label: '姓名'},{prop: 'amount1',label: '数值1',sortable: true},{prop: 'amount2',label: '数值2',sortable: true},{prop: 'amount3',label: '数值3',sortable: true}],tableData: [{id: '12987122',name: '王小虎',amount1: '234',amount2: '3',amount3: 10},{id: '12987123',name: '王小虎',amount1: '165',amount3: 12},{id: '12987124',name: '王小虎',amount1: '324',amount3: 9},{id: '12987125',name: '王小虎',amount1: '621',amount2: '2',amount3: 17},{id: '12987126',name: '王小虎',amount1: '539',amount2: '4',amount3: 15}]}}}</script>
导出excel
<template><div><el-button type="primary" @click="exportExcel">导出excel</el-button><fish-table :tableData='tableData' :columns='columns' ref="mtable" :showPage='false' :showNum='false'></fish-table></div></template><script>export default {data () {return {columns: [{prop: 'id',label: 'ID'},{prop: 'name',label: '姓名',},{prop: 'amount1',label: '数值1',sortable: true,},{prop: 'amount2',label: '数值2',sortable: true,},{prop: 'amount3',label: '数值3',sortable: true}],tableData: [{id: '12987122',name: '王小虎',amount1: '234',amount2: '3',amount3: 10},{id: '12987123',name: '王小虎',amount1: '165',amount2: '3',amount3: 12},{id: '12987124',name: '王小虎',amount2: '3',amount1: '324',amount3: 9},{id: '12987125',name: '王小虎',amount1: '621',amount2: '2',amount3: 17},{id: '12987126',name: '王小虎',amount1: '539',amount2: '4',amount3: 15}]}},methods: {exportExcel(){this.$refs.mtable.exportCsv({filename:'导出excel'})}}}</script>
拖拽行
需要指定row-key, drop为true,drop-change事件会返回顺序变化后的数据
<template><fish-table :tableData='tableData' :columns='columns' :showPage='false' row-key='id' drop @drop-change='dropChange' ></fish-table></template><script>export default {data () {return {columns: [{prop: 'id',label: 'ID'},{prop: 'name',label: '姓名'},{prop: 'amount1',label: '数值1',},{prop: 'amount2',label: '数值2',},{prop: 'amount3',label: '数值3',}],tableData: [{id: '12987122',name: '王小虎1',amount1: '234',amount2: '3',amount3: 10},{id: '12987123',name: '王小虎2',amount1: '165',amount2: '4',amount3: 12},{id: '12987124',name: '王小虎3',amount1: '324',amount2: '1',amount3: 9},{id: '12987125',name: '王小虎4',amount1: '621',amount2: '2',amount3: 17},{id: '12987126',name: '王小虎5',amount1: '539',amount2: '4',amount3: 15}]}},methods:{dropChange(tableData){this.tableData = tableData}}}</script>
配置项
封装的配置项及说明,其他配置和事件请参照 Table 表格
Props
| 字段 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| border | 是否带有纵向边框 | boolean | true |
| tableData | 表格数据 | Array | - |
| isTree | 是否为树形表格 | boolean | false |
| layout | 分页器组件布局 | string | ‘total,sizes,prev, pager, next, jumper’ |
| showNum | 是否显示序号列 | boolean | true |
| numTitle | 序号列名称 | string | ‘序号’ |
| selection | 表格是否展示多选列 | boolean | false |
| expand | 表格是否可以展开 | boolean | false |
| columns | 表格描述列信息 | Array | - |
| page | 分页信息 | Object | |
| pageSizes | 可选择每页多少条数据 | Array | [15, 30, 45, 60] |
| mergeRow | 可合并列字段 | Array | - |
| mergeColumn | 可合并行字段 | Array |
