1.0.5 • Published 4 years ago

@cgxqd/c-table v1.0.5

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

c-table

基于 Element-UI Table 组件封装的高阶表格组件,可无缝支持 element 的 table 组件。

安装

首先组件依赖 Element Table,先要安装 Element:

npm i element-ui

安装 c-table:

#npm
npm i @cgxqd/c-table
#yarn
yarn add @cgxqd/c-table

引入 Element、c-table

在 main.js 中写入以下内容:

<script>
    import Vue from 'vue';
    import ElementUI from 'element-ui';
    import CTable from '@cgxqd/c-table'
    import 'element-ui/lib/theme-chalk/index.css';
    import App from './App.vue';
Vue.use(ElementUI);
Vue.use(CTable)

new Vue({
    el: '#app',
    render: h => h(App)
});

使用

elementUI自带的属性方法,请参考elementUi文档

基础的表格

<template>
    <c-table :data="tableData" :config="tableConfig" :isLoading="false" @selection-change="handleSelectionChange"></c-table>
</template>
>### 含有分页的表格
```html
<template>
    <c-table :data="tableData" :config="tableConfig" has-pag :pagination="form" :init="init" ></c-table>
</template>

<script>
    export default {
        data() {
            return {
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄'
                }],
                tableConfig: [
                    {label: '日期',prop:'date'},
                    {label: '姓名',prop:'name'},
                    {label: '地址',prop:'address'},
                ],
                form:{
                    page:1,     
                    pageSize:20,     
                    total:0,     
                }
            }
        },  
        methods:{
            init(){
                 //触发请求函数 
            },
        },
    }
</script>

多级表头

<template>
    <div>
        <c-table :data='tableData' :config='tableConfig'></c-table>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [{
                date: '2016-05-03',
                name: '王小虎',
                province: '上海',
                city: '普陀区',
                address: '上海市普陀区金沙江路 1518 弄',
                zip: 200333
            }, {
                date: '2016-05-02',
                name: '王小虎',
                province: '上海',
                city: '普陀区',
                address: '上海市普陀区金沙江路 1518 弄',
                zip: 200333
            }, {
                date: '2016-05-04',
                name: '王小虎',
                province: '上海',
                city: '普陀区',
                address: '上海市普陀区金沙江路 1518 弄',
                zip: 200333
            }, {
                date: '2016-05-01',
                name: '王小虎',
                province: '上海',
                city: '普陀区',
                address: '上海市普陀区金沙江路 1518 弄',
                zip: 200333
            }, {
                date: '2016-05-08',
                name: '王小虎',
                province: '上海',
                city: '普陀区',
                address: '上海市普陀区金沙江路 1518 弄',
                zip: 200333
            }, {
                date: '2016-05-06',
                name: '王小虎',
                province: '上海',
                city: '普陀区',
                address: '上海市普陀区金沙江路 1518 弄',
                zip: 200333
            }, {
                date: '2016-05-07',
                name: '王小虎',
                province: '上海',
                city: '普陀区',
                address: '上海市普陀区金沙江路 1518 弄',
                zip: 200333
            }],
            tableConfig: [
                {label: '日期', prop: 'date'},
                {
                    label: '配送信息', children: [
                        {label:'姓名',prop:'name'},
                        {label:'省份',prop:'province'},
                        {label:'市区',prop:'city'},
                        {label:'地址',prop:'address'},
                        {label:'邮编',prop:'zip'},
                    ]
                }
            ],
        };
    },
};

render渲染

    <script>
        export default {
            data(){
                return {
                    config:[
                        {label:'时间',prop:'time',render:this.filterTime},
                        {label:'金额',prop:'price',render:price=>`¥${price}`}
                        {label:'操作',render:<el-link type='primary'>编辑</el-link>},
                    ]
                }
            },
            methods:{
                filterTime(time,{row},h){
                    console.log(time);// 打印时间
                    console.log(row.time);// 打印时间
                    /**时间过滤 */
                    // return h('span',time);
                    return <span>{time}</span>
                }
            }       
        }   
    </script>

slot插槽

    <template>
        <div>
            <c-table :data='tableData' :config='tableConfig'>
                <template slot="filterTime" slot-scope="{row}">{{row.time}}</template>
            </c-table>
        </div>
    </template>
    <script>
        export default {
            data(){
                return {
                    tableData:[],
                    config:[
                        {label:'时间',slot:'filterTime'},
                    ]
                }
            },   
        }   
    </script>

isShow是否显示当前列

    <template>
        <div>
            <c-table :data='tableData' :config='tableConfig'></c-table>
        </div>
    </template>
    <script>
        export default {
            data(){
                return {
                    tableData: [{
                        date: '2016-05-02',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1518 弄'
                    }, {
                        date: '2016-05-04',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1517 弄'
                    }, {
                        date: '2016-05-01',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1519 弄'
                    }, {
                        date: '2016-05-03',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1516 弄'
                    }],
                    tableConfig: [
                        {label: '日期',prop:'date'},
                        {label: '姓名',prop:'name'},
                        {label: '地址',prop:'address'},
                        {label: '地址',isShow:false,render:<el-link>删除</el-link>},
                    ],
                }
            },   
        }   
    </script>

c-table Methods

详细使用请参考 Element Table Methods

    <c-table ref='testRef' />
    // 调用方法 this.$refs.testRef.clearSelection()

c-table Attributes && c-table Events

以下属性为新增属性,其他具体的属性设置可以参考 Element Table AttributesTable Events文档

参数说明类型可选值默认值
isLoading表格组件是否显示loadingBoolean-false
align配置表格全局table-column的对齐方式Stringleft/center/rightcenter
tableClassName给el-table 设置 classNameString/Object/Array--
tableStyle给el-table 设置 styleString/Object/Array--
has-pag是否启动分页Boolean-false
init触发分页的请求函数Function--
layout组件布局 子组件用逗号隔开String-total,sizes,prev,next,jumper
pageSizes每页希纳是个数选择器的选项配置Array-20, 50, 100, 150
pagination分页的配置信息Object--
config配置表格的el-table-columnArray-[]

pagination

具体的属性设置可以参考 Element Pagination 文档

参数说明类型可选值默认值
page当前页数Number-1
page-size每页多少条个数Number-20
total总条目数Number--

config

config 中每项的配置项其实就是 Element Table-column 上的 props 属性,具体的属性设置可以参考 Element Table-column 文档,以下为新增属性。

参数说明类型可选值默认值
render渲染属性function(cellValue,{row,column,$index},h),HTMLElement,String,*--
slot插槽名String--
isShow当前列是否显示Boolean-true
1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago