vue 表格里面的复选框,全选,单选

效果图:
在这里插入图片描述

main.js:

<template>
  <div>
    <div class="table">
      <table border="1" cellspacing="0">
        <thead>
          <tr>
            <th>
              <el-checkbox
                :value="isAllChecked()"
                @change="setAllSelect()"
              ></el-checkbox>
            </th>
            <th>序号</th>
            <th>名称</th>
            <th>所占比例</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in items" :key="index">
            <td>
              <el-checkbox
                :value="isChecked(item)"
                @change="setSelect(item)"
              ></el-checkbox>
            </td>

            <td>{{ index + 1 }}</td>
            <td v-text="item.name"></td>
            <td v-text="item.row"></td>
          </tr>
        </tbody>
      </table>
    </div>
    <pre class="">选中的数据:{{selectedItem}}</pre>
  </div>
</template>

<script>
import selectTableData from "../../service/selectTableData.js";
export default {
  mixins: [selectTableData],
  data() {
    return {
      items: [
        { id: 1, name: "数据1", row: "3" },
        { id: 2, name: "数据2", row: "3" },
        { id: 3, name: "数据3", row: "3" },
        { id: 4, name: "数据4", row: "3" },
        { id: 5, name: "数据5", row: "3" },
        { id: 6, name: "数据6", row: "3" },
      ],
      selectedItem: [],
    };
  },
  methods:{
  // 数组里面的字符串拼接
   spliceStr(data,id,str){
      return  data.map(item=>item[id]).join(str)
    }
  }
};
</script>
<style lang="less" scoped>
.table {
  width: 100%;
  table {
    width: 100%;
    border: 1px solid #ccc;
    tr {
      td,
      th {
        border: 1px solid #ccc;
        border-width: 0px 1px 1px 0px;
        width: auto;
        line-height: 40px;
        text-align: center;
      }
      th {
        background: #f7f8fa;
      }
    }
  }
}
</style>

selectTableData.js


export default {
    name: 'selectTableData',
    data() {
        return {
            tableUID: "id",
            items: [], // 列表数据
            selectedItem: [], // 选中列表数据
        };
    },
    methods: {
    	// 判断是否全部选中
        isAllChecked() {
            return (this.items.length == this.selectedItem.length && this.selectedItem.length > 0) ? true : false
        },
        // 设置全部选中
        setAllSelect() {
            this.isAllChecked() ? this.selectedItem = [] : this.selectedItem = [...this.items]
        },
        // 设置单选
        setSelect(_item) {
            if (this.isChecked(_item)) {
            	//  若是有改数据,则删除该数据,否则添加该数据
                this.selectedItem.splice( this.selectedItem.findIndex( (item) => item[this.tableUID] ==_item[this.tableUID] ), 1 );
            } else {
                this.selectedItem.push(_item);
            }
        },
        // 判断是否选中
        isChecked(item) {
            const flag = this.selectedItem.some((_item) => {
                return _item[this.tableUID] === item[this.tableUID];
            });
            return flag;
        },
    },
};

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值