vue el-table翻页问题,勾选的问题

如果不需要回显

直接设置reserve-selection 为true

如果需要回显

1.reserve-selection 设置为false

2.需要设置row-key

3.定义2个变量selectList,selectAllIds
selectAllIds 保存唯一的id
selectList 保存勾选的值

4.el-table里增加事件
@select=“handleSelectionChange”
@select-all=“handleAllChange”

5.注意不要使用select-change
利用@select的第二个参数来增减selectAllIds, selectList里的值,确保都是勾选上的
注意

6.selectList,selectAllIds初始化的时候要塞入回显的值

7.利用toggleRowSelection勾选数据(需要放入翻页的事件里)

注意下面的例子显示的是已经封装过的el-table

 <sys-table
          :loading="loading"
          :data="tableData"
          :columns="tableColumns"
          :num="params.currPage"
          :pageSize="params.pageSize"
          :pageTotal="pageTotal"
          :height="tableHeight"
          @onChange="handleChange"
          row-key="id"
          @select="handleSelectionChange"
          @select-all="handleAllChange"
          :pageSizes="[2, 10, 15]"
          ref="table"
        >
          <template #projectName="{ scope }">
            <el-link type="primary" @click="handleView(scope.row.id)">
              {{ scope.row.projectName }}
            </el-link>
          </template>
        </sys-table>
getBillList(this.params).then(res => {
        if (res && res.status === '0') {
          this.tableData = res.data.records
          this.pageTotal = Number(res.data.total)
          this.toggleData()
        } else {
          this.tableData = []
          this.pageTotal = 0
        }
        this.loading = false
      })
    // 勾选或不勾选
    handleSelectionChange(selection, row) {
      // 每次勾选都需要判断是否勾选了已经选中的数据

      if (!this.selectAllIds.includes(row.id)) {
        this.selectList.push(row)
        this.selectAllIds.push(row.id)
      } else {
        const index = this.selectAllIds.findIndex(item => {
          return item === row.id
        })
        const indexList = this.selectList.findIndex(item => {
          return item.id === row.id
        })
        this.selectAllIds.splice(index, 1)
        this.selectList.splice(indexList, 1)
      }
    },

    handleAllChange(selecteds) {
      if (selecteds.length > 0) {
        const tempList = []
        selecteds.forEach(item => {
          if (!this.selectAllIds.includes(item.id)) {
            tempList.push(item.id)
            this.selectList.push(item)
          }
        })
        this.selectAllIds = [...this.selectAllIds, ...tempList]
      } else {
        this.tableData.forEach(item => {
          this.selectAllIds.forEach((id, index) => {
            if (id === item.id) {
              this.selectList.splice(index, 1)
            }
          })
        })
        this.selectAllIds = this.selectAllIds.filter(item => {
          const obj = this.tableData.find(itemData => {
            return itemData.id === item
          })
          return !obj
        })
      }
    },
  toggleData() {
      this.$refs.table.toggleSelection(this.selectAllIds, 'id')
    },
 private toggleSelection(rows: any, id: string = 'id') {
    this.$nextTick(() => {
      if (rows) {
        rows.forEach((row: any) => {
          const data: any[] = Array.from(this.$attrs.data)
          const obj = data.find((item: any) => {
            return item[id] === row
          })
          if (obj) {
            ;(this.$refs.table as any).toggleRowSelection(obj, true)
          }
        })
      } else {
        ;(this.$refs.table as any).clearSelection()
      }
    })
  }

单选问题

隐藏顶部的全选按钮
使用 @select=“handleSelectionChange” 事件

if (rows.length > 1) {
      const del_row = rows.shift()
      this.$refs.table.toggleRowSelection(del_row, false)
      this.currentProject = rows
      console.log('sss', rows)
    } else if (rows.length === 1) {
      this.currentProject = rows
    } else {
      this.currentProject = []
    }
### Vue3 Element Plus El-Table 单选示例 为了实现在 `Vue3` 中使用 `Element Plus` 的 `el-table` 组件完成单行选择功能,可以按照如下方式构建: #### 安装依赖库 确保已经安装了必要的包: ```bash npm install vue@next npm install element-plus ``` #### 创建基础模板结构 创建一个简单的页面布局来展示表格及其操作按钮。 ```html <template> <div style="margin: 20px;"> <!-- 表格 --> <el-table :data="tableData" @selection-change="handleSelectionChange"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> <!-- 显示已选项信息 --> <p v-if="selectedRow">您选择了:{{ selectedRow.name }}</p> </div> </template> ``` #### 编写脚本逻辑 通过 JavaScript 来管理状态以及处理事件响应。 ```javascript <script setup> import { ref } from 'vue'; // 初始化表格数据 const tableData = [ { date: '2016-05-03', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' }, { date: '2016-05-02', name: 'John', address: 'New York No. 1 Lake Park' } ]; // 存储当前被选中的行对象 let selectedRow = ref(null); function handleSelectionChange(selection) { // 当前只允许单选,则直接赋值给 selectedRow 变量 if (selection.length === 1) { selectedRow.value = selection[0]; } else { selectedRow.value = null; } } </script> ``` 上述代码展示了如何利用 `el-table-column` 设置带有复选框的选择列,并监听其变化以便追踪用户的交互行为。每当用户勾选不同的行时都会触发 `handleSelectionChange()` 方法,在这里可以根据实际需求调整业务逻辑[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值