• 【Vue Element-ui el-table组件 实现跨分页全选 可全选中当前页 也可选中全量数据】


    思路一 Vue Element-ui el-table组件 实现跨分页全选 可全选中当前页 也可选中全量数据

    前端模拟数据示例,无需后台接口,复制粘贴即可看到效果。

    <template>
      <div>
        <div class="common-wrapper">
          
          <el-table :data="lists" ref="table" highlight-current-row v-loading="listLoading" style="width: 100%;"
            :row-key="getRowKeys" type="selection" @selection-change="handleSelectionChange">
            <el-table-column type="selection" :reserve-selection="true" :selectable="checkSelectable">el-table-column>
            <el-table-column prop="id" label="id"> el-table-column>
            <el-table-column prop="time" label="time"> el-table-column>
          el-table>
          
          <el-col :span="24" class="toolbar">
             
            <el-checkbox v-model="allCheck" @change="allCheckEvent">全选所有el-checkbox>
            <el-pagination :current-page="this.page" layout="total , prev, pager, next"
              @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
            el-pagination>
          el-col>
          <div class="clearfix">div>
        div>
      div>
    template>
     
    <script> 
    import axios from 'axios';
    export default {
      data () {
        return {
          lists: [
            { id: '1', time: '2019-09-09 12:12:12' },
            { id: '2', time: '2019-09-09 12:12:12' },
            { id: '3', time: '2019-09-09 12:12:12' },
            { id: '4', time: '2019-09-09 12:12:12' },
            { id: '5', time: '2019-09-09 12:12:12' },
            { id: '6', time: '2019-09-09 12:12:12' },
            { id: '7', time: '2019-09-09 12:12:12' },
            { id: '8', time: '2019-09-09 12:12:12' },
            { id: '9', time: '2019-09-09 12:12:12' },
            { id: '10', time: '2019-09-09 12:12:12' },
          ],
          total: 13,
          page: 1,
          listLoading: false,
          multipleSelectionAll: [],//所有选中的数据包含跨分页数据
          allCheck: false,
          getRowKeys (row) {
            return row.id;
          },
        };
      },
      methods: {
        // 分页全选-选中框改变事件
        handleSelectionChange (val) {
          // 数据去重
          this.multipleSelectionAll = this.reduceSame(val);
          // 选中所有选择框时勾选全选按钮
          if (this.multipleSelectionAll.length == this.total) {
            this.allCheck = true;
          }
          // 将row是对象数组数据转换为字符串
          this.multipleSelectionAll = this.multipleSelectionAll.map(function (val) {
            return val.id;
          }).toString();
          // 选中后的数据
          console.log(this.multipleSelectionAll)
        },
        // 分页全选-全选按钮change事件
        allCheckEvent () {
          let _this = this;
          if (_this.allCheck) {
            // 全选选中时当前页所有数据选中
            _this.lists.forEach(row => {
              if (row) {
                _this.$refs.table.toggleRowSelection(row, true);
              }
            });
          } else {
            _this.$refs.table.clearSelection();
          }
        },
        // 分页全选-全选时禁用选择框
        checkSelectable (row, index) {
          return !this.allCheck;
        },
        // 数组对象去重
        reduceSame: function (arr) {
          let obj = {};
          return arr.reduce(function (prev, item) {
            obj[item.id] ? "" : (obj[item.id] = 1 && prev.push(item))
            return prev
              ;
          }, []);
        },
        // 分页
        handleCurrentChange (val) {
          this.page = val;
          if (val == 1) {
            this.lists = [
              { id: '1', time: '2019-09-09 12:12:12' },
              { id: '2', time: '2019-09-09 12:12:12' },
              { id: '3', time: '2019-09-09 12:12:12' },
              { id: '4', time: '2019-09-09 12:12:12' },
              { id: '5', time: '2019-09-09 12:12:12' },
              { id: '6', time: '2019-09-09 12:12:12' },
              { id: '7', time: '2019-09-09 12:12:12' },
              { id: '8', time: '2019-09-09 12:12:12' },
              { id: '9', time: '2019-09-09 12:12:12' },
              { id: '10', time: '2019-09-09 12:12:12' },
            ]
          } else {
            this.lists = [
              { id: '11', time: '2019-09-09 12:12:12' },
              { id: '12', time: '2019-09-09 12:12:12' },
              { id: '13', time: '2019-09-09 12:12:12' },
            ]
          }
        },
      },
      mounted () {
     
      },
      watch: {
        // 分页全选-监听数据变化
        lists: {
          handler (value) {
            if (this.allCheck) {
              this.lists.forEach(row => {
                if (row) {
                  this.$refs.table.toggleRowSelection(row, true)
                }
              });
            }
          },
          deep: true
        },
      }
    }
     
    script>
     <template>
      <div>
        <div class="common-wrapper">
          
          <el-table :data="lists" ref="table" highlight-current-row v-loading="listLoading" style="width: 100%;"
            :row-key="getRowKeys" type="selection" @selection-change="handleSelectionChange">
            <el-table-column type="selection" :reserve-selection="true" :selectable="checkSelectable">el-table-column>
            <el-table-column prop="id" label="id"> el-table-column>
            <el-table-column prop="time" label="time"> el-table-column>
          el-table>
          
          <el-col :span="24" class="toolbar">
             
            <el-checkbox v-model="allCheck" @change="allCheckEvent">全选所有el-checkbox>
            <el-pagination :current-page="this.page" layout="total , prev, pager, next"
              @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
            el-pagination>
          el-col>
          <div class="clearfix">div>
        div>
      div>
    template>
     
    <script> 
    import axios from 'axios';
    export default {
      data () {
        return {
          lists: [
            { id: '1', time: '2019-09-09 12:12:12' },
            { id: '2', time: '2019-09-09 12:12:12' },
            { id: '3', time: '2019-09-09 12:12:12' },
            { id: '4', time: '2019-09-09 12:12:12' },
            { id: '5', time: '2019-09-09 12:12:12' },
            { id: '6', time: '2019-09-09 12:12:12' },
            { id: '7', time: '2019-09-09 12:12:12' },
            { id: '8', time: '2019-09-09 12:12:12' },
            { id: '9', time: '2019-09-09 12:12:12' },
            { id: '10', time: '2019-09-09 12:12:12' },
          ],
          total: 13,
          page: 1,
          listLoading: false,
          multipleSelectionAll: [],//所有选中的数据包含跨分页数据
          allCheck: false,
          getRowKeys (row) {
            return row.id;
          },
        };
      },
      methods: {
        // 分页全选-选中框改变事件
        handleSelectionChange (val) {
          // 数据去重
          this.multipleSelectionAll = this.reduceSame(val);
          // 选中所有选择框时勾选全选按钮
          if (this.multipleSelectionAll.length == this.total) {
            this.allCheck = true;
          }
          // 将row是对象数组数据转换为字符串
          this.multipleSelectionAll = this.multipleSelectionAll.map(function (val) {
            return val.id;
          }).toString();
          // 选中后的数据
          console.log(this.multipleSelectionAll)
        },
        // 分页全选-全选按钮change事件
        allCheckEvent () {
          let _this = this;
          if (_this.allCheck) {
            // 全选选中时当前页所有数据选中
            _this.lists.forEach(row => {
              if (row) {
                _this.$refs.table.toggleRowSelection(row, true);
              }
            });
          } else {
            _this.$refs.table.clearSelection();
          }
        },
        // 分页全选-全选时禁用选择框
        checkSelectable (row, index) {
          return !this.allCheck;
        },
        // 数组对象去重
        reduceSame: function (arr) {
          let obj = {};
          return arr.reduce(function (prev, item) {
            obj[item.id] ? "" : (obj[item.id] = 1 && prev.push(item))
            return prev
              ;
          }, []);
        },
        // 分页
        handleCurrentChange (val) {
          this.page = val;
          if (val == 1) {
            this.lists = [
              { id: '1', time: '2019-09-09 12:12:12' },
              { id: '2', time: '2019-09-09 12:12:12' },
              { id: '3', time: '2019-09-09 12:12:12' },
              { id: '4', time: '2019-09-09 12:12:12' },
              { id: '5', time: '2019-09-09 12:12:12' },
              { id: '6', time: '2019-09-09 12:12:12' },
              { id: '7', time: '2019-09-09 12:12:12' },
              { id: '8', time: '2019-09-09 12:12:12' },
              { id: '9', time: '2019-09-09 12:12:12' },
              { id: '10', time: '2019-09-09 12:12:12' },
            ]
          } else {
            this.lists = [
              { id: '11', time: '2019-09-09 12:12:12' },
              { id: '12', time: '2019-09-09 12:12:12' },
              { id: '13', time: '2019-09-09 12:12:12' },
            ]
          }
        },
      },
      mounted () {
     
      },
      watch: {
        // 分页全选-监听数据变化
        lists: {
          handler (value) {
            if (this.allCheck) {
              this.lists.forEach(row => {
                if (row) {
                  this.$refs.table.toggleRowSelection(row, true)
                }
              });
            }
          },
          deep: true
        },
      }
    }
     
    script>
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279

    思路二 element-ui table跨页全选

    element-ui table里的全选功能只会全选当前页的所有数据
    当table有分页功能的时候实现跨页全选

    ①为table添加select方法(当用户手动勾选数据行的 Checkbox 时触发的事件)

    两个参数 selection,row 选中的数据 最后一个选中的数据

    定义一个变量用来保存选中的数据 将selection赋值给变量

    checkAdmittance(selection, row) {
    
    this.selectedList = selection;
    
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ②为table添加select-all方法(当用户手动勾选全选 Checkbox 时触发的事件)
    this.allCheck为true时

    
    
         if (!this.allCheck) {
                       // 全选选中时当前页所有数据选中,使用后台所有数据进行遍历.
          // 把所有数据赋值给当前选中变量,遍历所有数据
                       this.selectedList = this.pointData;
                       this.pointData.forEach((row) => {
                              if (row) {
               //为table添加ref属性 table
                                   this.$refs.table.toggleRowSelection(row, true);this.selectedList = [];
                              }
                       });
          this.allCheck = true;
         }else{ 
            this.$refs.table.clearSelection();
                                 this.allCheck = false;
            this.selectedList = [];
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    思路三 Element分页跨页全选操作(跨页记住已经勾选)

    1.HTLM 添加按钮全选,要有一个全选标识状态

    <el-button
            type="success"
                  @click="selectAll"
                >
                  {{ sign === 1 ? '全 选' : '取 消 全 选' }}
        </el-button>
    
        Table
         <el-table
                  v-loading="loading"
                  ref="accessControlTable"
                  :data="tableData"
                  :max-height="tableHeight"
                  :span-method="arraySpanMethod"
                  header-cell-class-name="custom-table"
                  @selection-change="handleSelectionChange"
                  @select="selectExceptCardIds"
                  @select-all="selecetAll"
                >
                  <el-table-column
                    type="selection"
                    width="30"
                  />
                  <el-table-column
                    prop="id"
                    type="index"
                    align="center"
                    width="50"
                    label="序号"/>
                ...
            </el-table>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    2.JS相关代码如下:

    // 数据
      data() {
        return {
                tableData: [],
                sign: 1, // 是否点击全选,1未点击全选,2,点击全选
                checkedArr: [], // 勾选的行数组
                uncheckedArr: [], // 取消勾选的行数组
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    相关操作方法如下:

    // 选中事件
        handleSelectionChange(val) {
          console.log('多选id===handleSelectionChange', val)
        },
    // 合并表格最后三列
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
          if (columnIndex === 6) {
            return [0, 0]
          } else if (columnIndex === 5) {
            return [1, 2]
          }
        },
    // 全选切换
        selectAll() {
          this.sign = this.sign === 1 ? 2 : 1
          console.log('sign', this.sign)
          this.chooseAllPages()
          this.checkedArr = []
          this.uncheckedArr = []
        },
        // 全选所有页面
        chooseAllPages() {
          if (this.sign === 2) {
            // 全选
            this.$nextTick(() => {
              this.tableData.forEach(row => {
                // 没有取消过的勾选
                if (this.uncheckedArr.map(v => v.id).indexOf(row.id) < 0) {
                  this.$refs.accessControlTable.toggleRowSelection(row, true)
                }
              })
            })
          } else {
            // 取消全选
            this.$nextTick(() => {
              this.$refs.accessControlTable.clearSelection()
            })
          }
        },
    // 切换分页时选择之前选中
        checkedSelected() {
          console.log('切换分页时选择之前选中')
          this.$nextTick(() => {
            if (this.checkedArr.length === 0) return
            this.tableData.forEach(row => {
              if (this.checkedArr.map(v => v.id).indexOf(row.id) >= 0) {
                this.$refs.accessControlTable.toggleRowSelection(row, true)
              }
            })
          })
        },
    // 全选后取消单个选择事件,当用户手动勾选数据行的 Checkbox 时触发的事件
        selectExceptCardIds(selection, row) {
          // selection 当前页面所有选中的项
          // row 当前点击操作的项
          console.log('全选后取消单个选择事件,当用户手动勾选数据行的 Checkbox 时触发的事件', selection, row)
          // 所有页面不全选
          if (this.sign === 1) {
            if (selection.indexOf(row) >= 0) {
              // 新增(勾选上)
              selection.map(el => {
                if (this.checkedArr.map(v => v.id).indexOf(el.id) === -1) {
                  this.checkedArr.push(el)
                }
              })
              console.log('所有选中的', this.checkedArr)
            } else {
              // 取消勾选
              this.checkedArr.map((el2, index) => {
                if (el2.id === row.id) {
                  this.checkedArr.splice(index, 1)
                }
              })
              console.log('删除后选中的', this.checkedArr)
            }
          } else {
            // 所有页面全选
            if (selection.indexOf(row) >= 0) {
              // 重新勾选上
              this.uncheckedArr.map((el, index) => {
                if (el.id === row.id) {
                  this.uncheckedArr.splice(index, 1)
                }
              })
            } else {
              // 取消勾选
              this.uncheckedArr.push(row)
            }
            console.log('剔除的id集合', this.uncheckedArr)
          }
        },
    // 当前页面全选
        selecetAll(selection) {
          console.log('当前页面全选', selection)
          if (this.sign === 1) {
            // 不是全选按钮状态下-考虑选中哪些
            if (selection.length > 0) {
              // 选中
              selection.map(row => {
                if (this.checkedArr.map(v => v.id).indexOf(row.id) < 0) {
                  this.checkedArr.push(row)
                }
              })
            } else {
              // 取消选中
              this.tableData.map(row => {
                this.checkedArr.map((el2, index) => {
                  if (el2.id === row.id) {
                    this.checkedArr.splice(index, 1)
                  }
                })
              })
            }
            console.log('不是全选按钮状态下-考虑选中哪些', this.checkedArr)
          } else {
            // 全选按钮状态下-考虑不选中哪里
            if (selection.length === 0) {
              this.tableData.map(row => {
                this.uncheckedArr.push(row)
              })
            } else {
              selection.map(row => {
                this.uncheckedArr.map((el, index) => {
                  if (el.id === row.id) {
                    this.uncheckedArr.splice(index, 1)
                  }
                })
              })
            }
            console.log('全选按钮状态下-考虑不选中哪里', this.uncheckedArr)
          }
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132

    备注:其中id是每条记录的唯一标识,当全选按钮点击后,按钮变成取消全选,切换标识sign的值,接口传参带入全选标识和剔除行id数组uncheckedArr,若是默认状态非全选,则带入非全选标识和选中行id数组checkedArr。

    作者:欧_991e 链接:https://www.jianshu.com/p/8415a0c416ed 来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 相关阅读:
    高通导航器软件开发包使用指南(9)
    Java云原生(Spring Native)开发初体验报告
    接口自动化测试小结
    JS Echarts之雷达图 | 使用Excel画雷达图
    速看:免费领取4台阿里云服务器_申请入口及领取流程
    大数据培训企业开发案例实时读取本地文件到HDFS案例
    【数据结构】——单链表
    云原生场景下高可用架构的最佳实践
    Matlab:Matlab编程语言学习之向量化编程的简介、技巧总结案例应用之详细攻略
    数据库优化
  • 原文地址:https://blog.csdn.net/qq_39900031/article/details/126415906