Vue+ElementUI实现Table表格添加自定义头CheckBox多选框

  • Post author:
  • Post category:vue


<template>
    <div id="app">
        <img src="./assets/logo.png">
        <el-table :data="tableData" style="width: 100%">
           <el-table-column
               label="姓名"
               prop="name">
           </el-table-column>
           <el-table-column
               label="性别"
               prop="sex">
           </el-table-column>
           <el-table-column align="right">
               <template slot="header" slot-scope="scope">
                   <el-checkbox :indeterminate="isIndeterminate"        
                    v-model="checkAll"@change="handleCheckAllChange">隐藏</el-checkbox>
               </template>
               <template slot-scope="scope">
                   <el-checkbox v-model="scope.row.checked"                   
                   @change="handleCheckOneChange">隐藏</el-checkbox>
               </template>
           </el-table-column>
        </el-table>
    </div>
</template>
<script>

export default {
    name: 'App',
    data () {
      return {
        tableData: [{
           name: '李时珍',
           sex: '男',
           checked: false
        }, {
           name: '花木兰',
           sex: '女',
           checked: true
       }],
        checkAll: false,
        isIndeterminate: true
     }
   },

   methods: {
       handleCheckAllChange (val) {
           console.info('check all change is ', val)
           this.isIndeterminate = false
           this.tableData.forEach(item => {
               item.checked = val
           })
       },

       handleCheckOneChange (val) {
           console.info('check one change is ', val)
           let totalCount = this.tableData.length
           let someStatusCount = 0
           this.tableData.forEach(item => {
               if (item.checked === val) {
                    someStatusCount++
               }
           })
           this.checkAll = totalCount === someStatusCount ? val : !val
           this.isIndeterminate = someStatusCount > 0 && someStatusCount < totalCount
       }
   }
}
</script> 



版权声明:本文为weixin_43871837原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。