elementUI中表格分页勾选以及整体的全选功能结合。

这篇博客展示了如何在Vue.js中实现表格数据展示、分页和全选功能。利用`el-table`组件,结合`el-pagination`进行数据分页,并通过`el-checkbox`实现全选和全不选的效果,同时处理了选中项的状态变化。
摘要由CSDN通过智能技术生成
<template>
<div>
    <div class="table taskcontent">
        <el-table :data="tabledata" style="width:100%" @selection-change='selRow' ref="multipleTable" :row-key="getRowKeys">
            <el-table-column type="selection" min-width="55" align="center" :reserve-selection="true"></el-table-column>
            <el-table-column prop="num" label="编号" min-width="120" align="center"></el-table-column>
            <el-table-column prop="name" label="任务名称" min-width="300" align="center"></el-table-column>
            <el-table-column prop="time" label="创建时间" min-width="300" align="center"></el-table-column>
        </el-table>
    </div>
    <el-checkbox label="全选" :indeterminate="isIndeterminate" v-model="checkAll" @change="selAll()" ></el-checkbox>
    <el-pagination
		@size-change="handlePageSize"
		@current-change="handleCurrentPage"
		:current-page="currentPage"
		:page-sizes="[3,5]"
		:page-size="pageSize"
		layout="total, sizes, prev, pager, next, jumper"
		:total="total">
	</el-pagination>
</div>
</template> 
<script>
export default {
    data() {
        return {
            selectedAssetsList:[],
            assetsUidList:[],
            currentPage:1,
            pageSize:3,
            total:8,
            isIndeterminate:false,//对el-checkbox控制不完整的全选状态
            checkAll:false,//对el-checkbox控制全选状态
            tabledata:[
            ],
            newTabledata: [ //el-table的数据内容
                {
                    id:1,
                    num: "201903123345",
                    name: "等级评价",
                    time: "2019-10-10"
                },
                {   id:2,
                    num: "201903123346",
                    name: "供应商推荐",
                    time: "2019-10-14"
                },
                {
                    id:3,
                    num: "201903123348",
                    name: "供应商推荐",
                    time: "2019-10-15"
                },
                {
                    id:4,
                    num: "201903123350",
                    name: "我的报告",
                    time: "2019-10-16"
                },
                {
                    id:5,
                    num: "201903123345",
                    name: "等级评价",
                    time: "2019-10-10"
                },
                {   id:6,
                    num: "201903123346",
                    name: "供应商推荐",
                    time: "2019-10-14"
                },
                {
                    id:7,
                    num: "201903123348",
                    name: "供应商推荐",
                    time: "2019-10-15"
                },
                {
                    id:8,
                    num: "201903123350",
                    name: "我的报告",
                    time: "2019-10-16"
                }
            ]
        };
    },
    mounted(){
        this.search();
    },
    methods:{
        // 获取row的key值
        getRowKeys(row) {
            return row.id;
        },
        handlePageSize(pageSize){
            this.pageSize = pageSize;
            this.search();
        },
        handleCurrentPage(currentPage){
            this.currentPage = currentPage;
            this.search();
        },
        search(){
            this.tabledata = this.newTabledata.slice((this.currentPage - 1)*this.pageSize,this.currentPage * this.pageSize);
        },
        //全选按键功能实现
        selAll() {
            if(this.$refs.multipleTable.selection.length < this.newTabledata.length){
                this.checkAll=true;
            }else{
                this.checkAll=false;
            }
            if(this.checkAll){
                this.newTabledata.forEach(row=>{
                    this.$refs.multipleTable.toggleRowSelection(row,true);
                });
            }else{
                this.$refs.multipleTable.clearSelection();
                this.selectedAssetsList = [];
                this.assetsUidList = [];
            }
        },
        stateChange(){
            var vm = this;
            if(vm.assetsUidList.length < this.newTabledata.length && vm.assetsUidList.length > 0){
                this.isIndeterminate = true;
            } else if(vm.assetsUidList.length == this.newTabledata.length){
                this.isIndeterminate = false;
                this.checkAll = true;
            } else if(vm.assetsUidList.length == 0){
                this.isIndeterminate = false;
                this.checkAll = false;
            }
        },
        //表格内checkbox触发的全选按钮状态变化
        selRow(val){
            const vm = this;
            // 选择的行的完整数据。
            vm.selectedAssetsList = Array.from(new Set([...vm.selectedAssetsList,...val]));
            const currentArr = val.map(item => item.id);
            const arr1 = [...vm.assetsUidList, ...currentArr];
            vm.assetsUidList = Array.from(new Set(arr1));
            const tableData = vm.tabledata.map(item => item.id);
            const difference = tableData.filter(v => !currentArr.includes(v));
            difference.forEach(item => {
                if (vm.assetsUidList.indexOf(item) !== -1) {
                    vm.assetsUidList.splice(vm.assetsUidList.indexOf(item), 1);
                }
            });
            // 把选中的行数据进行去重。
            var list = vm.selectedAssetsList.filter(item=>vm.assetsUidList.indexOf(item.id)!=-1);
            for(var i =0;i<list.length;i++){
                for(var j=i+1;j<list.length;j++){
                if(list[i].id==list[j].id){
                    list.splice(j,1);
                    j--;
                }
                }
            }
            setTimeout(()=>{
                this.stateChange();
            },0)
            
        },
    }
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值