vue.js實現checkbox的全選和反選

來源:互聯網
上載者:User

標籤:body   add   長度   href   sel   javascrip   後台   status   eth   

摘要:

    1、監聽全選的值的變化,來改變checbox的數組值

    2、監聽選中checkbox的數組值的變化,當某一項checkbox有變化,判斷是否checkbox的數值長度是否和列表資料的長度一致,來改變全選的值。

 

下面來一起看看是怎麼實現的吧:

一、HTML

<template>

    <table class="table table-bordered table-hover text-center" id="tabletList">

            <thead>

                        <tr>

                            <th class="bgColor" width="25px"></th>

                            <th class="bgColor">獲獎時間</th>

                            <th class="bgColor">使用者ID</th>

                            <th class="bgColor">使用者電話號碼</th>

                            <th class="bgColor">獎品</th>

                            <th class="bgColor">使用者地址</th>

                            <th class="bgColor">使用者姓名</th>

                            <th class="bgColor">狀態</th>

                            <th class="bgColor">編輯</th>

                        </tr>

             </thead>

            <tbody>

                        <tr v-for="(x, index) in list">

                            <td><input type="checkbox" class="checkBox" v-model="x.checked" @change="selectSingle(index, x.userId)"></td>

                            <td v-text="x.createTime"></td>

                            <td v-text="x.userId"></td>

                            <td class="uerPhone" v-text="x.phone"></td>

                            <td class="awardStatus">

                                <img :src="x.goodsImg">

                                <p v-text="x.goodsName"></p>

                            </td>

                            <td class="address" v-text="x.address"></td>

                            <td class="userName" v-text="x.name"></td>

                            <td class="userAward" v-text="x.awardStatusStr"></td>

                            <td><a href="javascript:;" @click="modify(index)" class="curEdit">修改</a></td>

                        </tr>

                    </tbody>

</table>


二、js

 

export default {

        data() {

                checked: [],

                allChecked:null,

                checkedCount:0,

                list: [],//後台傳過來的json資料

                total:{

                    num: 0,

                    page: 0

                },

        },

 

 

methods: {

            selectSingle(index, id) {//單選個input點擊事件

                console.log(index)

                console.log(this.list[index].checked)

                // this.list[index].checked ^= 1

                this.list.forEach((item, i) => {

                    if (item.userId === id && i !== index) {

                        item.checked = this.list[index].checked

                        this.$set(this.list, i, item)

                    }

                })

                if (this.list[index].checked === true) {

                    this.checkedCount ++

                }

            },

            selectAll(event) {//全選

                this.list.forEach((item, i)=> {

                   if(item.checked === true){

                        item.checked = i.checked

                   }

                    item.checked ^= 1

                    this.$set(this.list, i, item)

                })

                if (this.allChecked == true) {

                    this.checkedCount = this.total.num

                }else{

                    this.checkedCount = 0

                }

            },

}

}

 

That‘s all. 是不是覺得超級簡單。

vue.js實現checkbox的全選和反選

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.