vue+elementUI实现全选功能


需要实现这样的功能,全选和判断是否是全选状态.因为样式的需要所以结构和elementUI案例结构有点区别

<div>
  <el-checkbox 
  :indeterminate="isIndeterminate" 
  v-model="checkAll" 
  @change="handleCheckAllChange"
  >全选</el-checkbox>
  <el-checkbox-group v-model="check" @change="handleCheckedCitiesChange">
    <el-row>
      <el-col :span="12" v-for="item in checkedList" :key="item.id" style="padding:30px">
        <el-checkbox :label="item.id">
          <span style="margin:0 120px 0 40px">{{item.name}}</span>
          <span>{{item.phone}}</span>
        </el-checkbox>
      </el-col>
    </el-row>
  </el-checkbox-group>
</div>

这个地方是模拟的数据

export default {
  data() {
    return {
      isIndeterminate: false,
      checkAll: false,
      check: [],
      checkedGameId: [],
      checkedList: [
        {
          id: 1,
          name: "张三",
          phone: "13554006475"
        },
        {
          id: 2,
          name: "李四",
          phone: "13514002475"
        },
        {
          id: 3,
          name: "王五",
          phone: "13558992475"
        }
      ]
    };
  },

这个地方是定义的方法

methods: {
    init() {
      for (let i = 0; i < this.checkedList.length; i++) {
        this.checkedGameId.push(this.checkedList[i].id);
      }
    },
    handleCheckAllChange(val) {
      this.check = val ? this.checkedGameId : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.checkedList.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.checkedList.length;
    }
  },
  created() {
    this.init();
  }

有帮助的可以在下方留言

以下是一个使用 Vue 2 和 Element UI 实现的多选全选开源项目的示例代码: ```html <template> <div> <el-checkbox v-model="allSelected" @change="selectAll">{{selectAllText}}</el-checkbox> <br /> <el-checkbox-group v-model="selectedItems"> <el-checkbox v-for="item in items" :key="item.id" :label="item">{{item.label}}</el-checkbox> </el-checkbox-group> </div> </template> <script> export default { data() { return { items: [ { id: 1, label: 'Item 1' }, { id: 2, label: 'Item 2' }, { id: 3, label: 'Item 3' }, { id: 4, label: 'Item 4' } ], selectedItems: [], allSelected: false } }, computed: { selectAllText() { return this.allSelected ? 'Deselect All' : 'Select All' } }, methods: { selectAll() { if (this.allSelected) { this.selectedItems = [...this.items] } else { this.selectedItems = [] } } } } </script> ``` 在该示例中,我们首先渲染一个“全选”复选框,并将其绑定到 `allSelected` 数据属性上。当用户更改复选框的选中状态时,我们会调用 `selectAll` 方法,该方法将根据 `allSelected` 数据属性将所有项目添加到或从 `selectedItems` 数组中删除。 接下来,我们使用 `el-checkbox-group` 组件渲染每个项目,并将其绑定到 `selectedItems` 数据属性上。最后,我们使用 `computed` 属性计算“全选”复选框的文本,该文本根据 `allSelected` 数据属性而变化。 这是一个基本示例,您可以根据需要进行扩展和定制。希望这可以帮助到您!
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值