element-ui表格实现全选和多选功能,先看效果图:

代码也是相对简单,根据官网提供的API来实现,具体代码奉上:

<template>
    <div>
        <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column prop="date" label="日期"></el-table-column>
            <el-table-column prop="name" label="姓名"></el-table-column>
            <el-table-column prop="address" label="地址"></el-table-column>
            <el-table-column label="选择">
                <template slot-scope="scope">
                    <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
                        <el-checkbox :label="scope.row.gameId">选中</el-checkbox>
                    </el-checkbox-group>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            checkAll: false,
            isIndeterminate: false,
            checkedCities: [],
            tableData: [],
            multipleSelection: [],
            checkedGameId: []
        }
    },
    methods: {
        handleCheckAllChange(val) {
            this.checkedCities = val ? this.checkedGameId : []
            this.isIndeterminate = false
        },
        handleCheckedCitiesChange(value) {
            let checkedCount = value.length
            this.checkAll = checkedCount === this.checkedGameId.length
            this.isIndeterminate = checkedCount > 0 && checkedCount < this.checkedGameId.length
        }
    },
    created() {
        let res = {
            'data': {
                'list': [
                    {
                        gameId: '11111',
                        date: '2016-05-02',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1518 弄'
                    }, {
                        gameId: '22222',
                        date: '2016-05-04',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1517 弄'
                    }, {
                        gameId: '33333',
                        date: '2016-05-01',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1519 弄'
                    }, {
                        gameId: '44444',
                        date: '2016-05-03',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1516 弄'
                    }
                ]
            }
        }
        this.tableData = res.data.list
        for (let i = 0; i < this.tableData.length; i++) {
            this.checkedGameId.push(this.tableData[i].gameId)
            this.multipleSelection = this.checkedGameId
        }
    }
}
</script>

 

GitHub 加速计划 / eleme / element
10
1
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:4 个月前 )
c345bb45 8 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 8 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐