0

0

0

修罗

站点介绍

只有了解事实才能获得真正的自由

vue slot开发表格组件

修罗 2020-09-15 1785 0条评论 vue

首页 / 正文

vue slot开发表格组件

slot用法回顾

子组件

1600180395235.png

父组件引入

用法1

1600180718443.png

用法2

1600180699094.png

表格组件开发

组件内容

1600181614460.png

组件使用

定义表头和表格数据

1600180912924.png

使用组件

1600181151047.png

本案例代码

表格组件(需要bootstrap样式支持)

<template>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th v-for="(item,index) in columns" :key="index">{{item.name}}</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="(item,index) in data" :key="index">
                <td v-for="(td,tdi) in columns" :key="tdi">
                    {{item[td.slot] || ''}}
                    <slot :name="td.slot" :item="item" :index="index"></slot>
                </td>
            </tr>
        </tbody>
    </table>
</template>

<script>
    export default {
        props:['columns','data']
    }
</script>

<style>
</style>

使用

<template>
    <div>
        <e-table :columns="columns" :data="data">
            <template #action="{item, index}">
                <button class="btn btn-primary btn-sm mr-2" @click="edit(item)">修改</button>
                <button class="btn btn-danger btn-sm" @click="del(index)">删除</button>
            </template>
        </e-table>
    </div>
</template>

<script>
    import eTable from "./e-table.vue"
    export default {
        data() {
            return {
                columns: [
                    { name: "姓名", slot: "name" },
                    { name: "性别", slot: "sex" },
                    { name: "年龄", slot: "age" },
                    { name: "操作", slot: "action" }
                ],
                data: [
                    { name: "小明", sex: "男", age: 22 },
                    { name: "小红", sex: "女", age: 20 },
                    { name: "小黄", sex: "男", age: 23 },
                ]
            }
        },
        components: {
            eTable
        },
        methods: {
            edit(item) {
                item.name = Math.floor(Math.random() * 100)
            },
            del(index) {
                this.data.splice(index, 1)
            }
        },
    }
</script>

<style>
</style>

评论(0)


最新评论

  • 1

    1

  • 1

    1

  • -1' OR 2+158-158-1=0+0+0+1 or 'TKCTZnRa'='

    1

  • 1

    1

  • 1

    1

  • 1

    1

  • 1

    1

  • @@5Qa2D

    1

  • 1

    1

  • 1

    1

日历

2025年09月

 123456
78910111213
14151617181920
21222324252627
282930    

文章目录

推荐关键字: Linux webpack js 算法 MongoDB laravel JAVA jquery javase redis