Vue全选

<el-checkbox :indeterminate="pYIsIndeterminate" v-model="pYcheckAll" @change="pYhandleCheckAllChange">全选</el-checkbox>

              <el-checkbox-group class="check_group" v-model="pYcheckedCities" @change="planTypeChecked">

                <el-checkbox v-for="item in planTypeOptions" :label="item.planningTypeName" :key="item.planningTypeName">{{item.planningTypeName}}</el-checkbox>

              </el-checkbox-group>

 

/**

   * 全选

   */

  // 全选框状态

  private pYcheckAll = false

  // 类型选择之后的数据

  private pYcheckedCities = []

  // 半选框状态

  private pYIsIndeterminate = false

  // 选择包含的计划类型

private planTypeOptions = [

    {

      planningTypeName: '小明',

      id: '1'

    },

    {

      planningTypeName: '小红',

      id: '2'

    }

  ]

  pYhandleCheckAllChange(val: any) {

    this.planTypeOptions.forEach(item => {

      this.pYcheckedCities.push((item.planningTypeName))

    }),

    (this.pYcheckedCities as any) = val ? this.pYcheckedCities : [];

    this.pYIsIndeterminate = false;

  }

  // 选择后包含的计划类型

  // 选择包含的计划类型

  private planTypeChecked(value: any) {

    let checkedCount = value.length;

    this.pYcheckAll = checkedCount === this.planTypeOptions.length;

    this.pYIsIndeterminate = checkedCount > 0 && checkedCount < this.planTypeOptions.length;

    console.log('value', value)

  }

Vue 中,进行全选与分页的兼容问题主要是在处理分页切换时全选状态的保持。当用户选择全选时,如果切换到下一页或其他页码,需要保持之前选中的项仍然处于选中状态。以下是一个示例: ```vue <template> <div> <input type="checkbox" v-model="selectAll" @change="selectAllItems"> <label for="selectAll">全选</label> <ul> <li v-for="item in displayedItems" :key="item.id"> <input type="checkbox" v-model="selectedItems" :value="item.id"> <label>{{ item.name }}</label> </li> </ul> <button @click="previousPage" :disabled="currentPage === 1">上一页</button> <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button> </div> </template> <script> export default { data() { return { items: [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }, // ...more items ], selectedItems: [], selectAll: false, currentPage: 1, itemsPerPage: 3 }; }, computed: { displayedItems() { const startIndex = (this.currentPage - 1) * this.itemsPerPage; const endIndex = startIndex + this.itemsPerPage; return this.items.slice(startIndex, endIndex); }, totalPages() { return Math.ceil(this.items.length / this.itemsPerPage); } }, methods: { selectAllItems() { if (this.selectAll) { this.selectedItems = this.items.map(item => item.id); } else { this.selectedItems = []; } }, previousPage() { if (this.currentPage > 1) { this.currentPage--; } }, nextPage() { if (this.currentPage < this.totalPages) { this.currentPage++; } } } }; </script> ``` 在上述示例中,`items` 数组包含所有的选项,`selectedItems` 数组用于存储选中的项的 ID。通过 `v-model` 双向绑定,可以实现全选和单个项的选择。 当用户选择全选时,`selectAllItems` 方法会将 `selectedItems` 数组设置为所有项的 ID。当用户取消全选时,会清空 `selectedItems` 数组。 在分页切换时,通过计算属性 `displayedItems` 获取当前页要显示的项。`previousPage` 和 `nextPage` 方法用于切换页码。 这样,无论用户如何切换分页,之前选中的项将保持选中状态。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值