Vue中手写checkbox自定义复选框、单选全选

Vue中手写checkbox自定义复选框、单选框
可以自定义各种样式的单选复选框,比一些组件用起来方便。
案例展示,样式就不多阐述了,自己根据情况写。

<div>
  <div v-for="(item, index) in shareList" :key="index" class="item-box"@click="btnCheck(item)">
    <img class="checkbox-img" v-show="item.isCheck" src="../icon/checked.png"/>
	<img class="checkbox-img" v-show="!item.isCheck" src="../icon/uncheck.png"/>
    <p class="title">{{ item.title }}</p>
 </div>
//全选
 <div class="fixed-container">
    <div class="flex-s">
      <div class="check-icon-box flex-s" v-if="!isAllCheck" @click="chooseAll">
        <img src="../icon/all-uncheck.png" class="icon" />
        <span>全选</span>
      </div>
      <div class="check-icon-box flex-s" v-else @click="chooseAll">
        <img src="../icon/checked.png" class="icon" />
        <span>全选</span>
      </div>
    </div>
 </div>
</div>
<script>
export default {
  data() {
    return {
      shareList: [],
      result: [],
      isEdit: false,
      isAllCheck: false, // 全选
    };
  },
  created() {
    if (!localStorage.token) {
      this.$router.push({
        path: "/login",
      });
    } else {
      this.init();
    }
  },
  methods: {
    init() {
      return new Promise((resolve) => {
        let param = {
          url: `/wfcm-api/lingdu/share/me/page`,
          data: {
            shareId: this.$route.query.id,
          },
        };
        util.getData(param).then((res) => {
          if (res.errCode == 0) {
            let course = {};
            let list = [];
            // 给接口返回的数据处理,添加未选择isCheck状态
            res.data.list.forEach((item) => {
              course = {
                ...item,
                isCheck: false, //重点:给数组的每一项添加自定义isCheck,之后根据这个来区分选择状态
              };
              list.push(course);
            });
            this.shareList = list;
          } else {
            util.checkLogin(this, res, this.$route.query.inviteId);
          }
          resolve();
        });
      });
    },
    // 单选
    btnCheck(item) {
      item.isCheck = !item.isCheck;
      this.result = this.shareList.filter((item) => {
        return item.isCheck;
      });
      let flag = this.shareList.every((item) => {
        return item.isCheck == true;
      });
      this.isAllCheck = flag;
    },
    // 全选
    chooseAll() {
      this.isAllCheck = !this.isAllCheck;
      this.shareList.forEach((item, index) => {
        item.isCheck = this.isAllCheck;
      });
      this.result = this.shareList.filter((item) => {
        return item.isCheck;
      });
    },
  },
};
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue ,可以通过 v-model 指令绑定复选框的选状态。实现全选、反选、单选功能可以通过以下方式: 1. 全选:绑定一个全选复选框,并将其选状态绑定到一个 Boolean 类型的变量上,然后将所有子复选框的选状态绑定到这个变量上。当全选复选框的选状态改变时,通过 v-model 指令将变量的值同步到所有子复选框的选状态,实现全选功能。 ```html <template> <div> <input type="checkbox" v-model="allSelected"> 全选 <div v-for="(item, index) in items" :key="index"> <input type="checkbox" :value="item" v-model="selectedItems"> {{ item }} </div> </div> </template> <script> export default { data() { return { items: ['A', 'B', 'C'], selectedItems: [], allSelected: false } }, watch: { allSelected(val) { this.selectedItems = val ? this.items : [] } } } </script> ``` 2. 反选:绑定一个反选的按钮,并在点击时通过计算属性将所有子复选框的选状态取反。 ```html <template> <div> <button @click="toggleSelection">反选</button> <div v-for="(item, index) in items" :key="index"> <input type="checkbox" :value="item" v-model="selectedItems"> {{ item }} </div> </div> </template> <script> export default { data() { return { items: ['A', 'B', 'C'], selectedItems: [] } }, computed: { allSelected: { get() { return this.selectedItems.length === this.items.length }, set(val) { this.selectedItems = val ? this.items : [] } } }, methods: { toggleSelection() { this.selectedItems = this.items.filter(item => !this.selectedItems.includes(item)) } } } </script> ``` 3. 单选:绑定一个单选复选框,将其选状态绑定到一个 String 类型的变量上,然后将所有子复选框的选状态绑定到这个变量上。当子复选框的选状态改变时,通过 v-model 指令将变量的值同步到单选复选框的选状态,实现单选功能。 ```html <template> <div> <input type="checkbox" :value="null" v-model="selectedItem"> 无 <div v-for="(item, index) in items" :key="index"> <input type="checkbox" :value="item" v-model="selectedItem"> {{ item }} </div> </div> </template> <script> export default { data() { return { items: ['A', 'B', 'C'], selectedItem: null } }, computed: { allSelected: { get() { return this.selectedItems.length === this.items.length }, set(val) { this.selectedItems = val ? this.items : [] } } }, watch: { selectedItem(val) { this.selectedItems = val ? [val] : [] } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值