首页 > 其他 > 详细

vue+element ui 的表格列使用组件

时间:2018-08-25 13:06:37      阅读:548      评论:0      收藏:0      [点我收藏+]

前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件。转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9530781.html

网站地址:我的个人vue+element ui demo网站 

github地址:yuleGH github

技术分享图片

代码如下:

  这里有两种方式让表格使用组件

    <el-table :data="tableData" style="width: 100%">

        <!--下拉框列的组件-->
        <my-select-column :select-options="selectOptions" key-field="selectOption"></my-select-column>

        <!--输入框的组件-->
        <el-table-column label="备注" width="180">
            <template slot-scope="scope">
                <my-input :key-field.sync="scope.row.remark"></my-input>
            </template>
        </el-table-column>     

    </el-table>

  js

<!--主 js-->
<script type="text/javascript">
    window.onload = function(){
        new Vue({
            el: "#app",
            data: {
                printStr: "",

                selectOptions : [
                    {value : "1", label : "选择一"},
                    {value : "2", label : "选择二"},
                    {value : "3", label : "选择三"},
                ],

                tableData: [{
                    date: 2000-10-27,
                    name: 余小乐,
                    address: 北京,
                    isRich: false,
                    remark : "我是备注",
                    selectOption : "2",
                    sex : "0"
                }, {
                    date: 2016-05-04,
                    name: 王小虎,
                    address: 上海市普陀区金沙江路 1517 弄,
                    isRich: true,
                    remark : "",
                    selectOption : "",
                    sex : "1"
                }, {
                    date: 2016-05-01,
                    name: 小花,
                    address: 重庆市璧山区,
                    isRich: true,
                    remark : "",
                    selectOption : "",
                    sex : "0"
                }, {
                    date: 1998-05-03,
                    name: 二哈,
                    address: 成都,
                    isRich: false,
                    remark : "",
                    selectOption : "",
                    sex : "1"
                }]
            },
            components : {
                my-select-column : myComponents.mySelectColumn,
                my-input : window.myComponents.myInput
            },
            methods: {
                handleEdit(index, row) {
                    this.printStr = "点击编辑;index=" + index + ";row=" + JSON.stringify(row);
                },
                handleDelete(index, row) {
                    this.printStr = "点击删除;index=" + index + ";row=" + JSON.stringify(row);
                },
                getTabelData(){
                    this.printStr = "表格数据:" + JSON.stringify(this.tableData);
                }
            }
        });
    };
</script>

  下拉框列的组件

<!--表格列组件-->
<div id="mySelectColumn">
    <el-table-column label="选择栏" width="200">
        <template slot-scope="scope">
            <el-select clearable placeholder="请选择" v-model="scope.row[keyField]">
                <el-option
                        v-for="item in selectOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
        </template>
    </el-table-column>
</div>
<script type="text/javascript">
    var getSelectColumn = function () {
        var component = {
            template: document.querySelector("#mySelectColumn").innerHTML,
            data: function () {
                return {}
            },
            // 声明 props
            props: {
                selectOptions: {
                    type: Array,
                    required: true
                },
                keyField : {
                    type : String,
                    required: true
                }
            },
            watch: {},
            methods: {}
        };

        return component;
    };

    window.myComponents = {
        mySelectColumn: getSelectColumn()
    };
</script>

  输入框的组件

<!--输入框组件-->
<div id="myInput">
    <el-input v-model="curKeyField" placeholder="请输入备注"></el-input>
</div>
<script type="text/javascript">
    var getInputComponent = function () {
        var component = {
            template: document.querySelector("#myInput").innerHTML,
            data: function () {
                return {
                    curKeyField : this.keyField
                }
            },
            // 声明 props
            props: {
                keyField : {
                    type : String,
                    required: true
                }
            },
            watch: {
                keyField : function(newVal, oldVal){
                    this.curKeyField = newVal;
                },
                curKeyField : function(newVal, oldVal){
                    this.$emit("update:keyField", newVal);
                }
            },
            methods: {}
        };

        return component;
    };

    window.myComponents.myInput = getInputComponent();
</script>

完。整体代码见 GitHub。

表格头使用自定义:https://www.jb51.net/article/137320.htm

 

转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9530781.html

 

vue+element ui 的表格列使用组件

原文:https://www.cnblogs.com/yuxiaole/p/9530781.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!