vue翻页表格

基于element-plus

父组件

<style scoped>
.father {
  height: 100%;
  width: 100%;
  /*display: flex;*/
//align-items: center; //justify-content: center; position: absolute;
  top: 20%
}

.dy {
  position: absolute;
  width: 50%;
}

.el-pagination {
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -40%);

}
</style>

<template>
  <div class="father">
    <dy :table-data=number.arry />
    <el-pagination background layout="prev, pager, next"
                   :total="100"
                   @current-change="gets"

    />
  </div>
</template>

<script setup>
import {reactive} from "vue";
import dy from './table.vue'
const number=reactive({
  currentPageNumber:1,//页码//每页显示多少数据
  arry:[]
})
//获取当前页码
const gets = (value) => {
 number.arry.splice(0)
  number.currentPageNumber=value
  console.log(value)
  //首页内容
  if(number.currentPageNumber===1){
  for(let i=0;i<5;i++){
     number.arry.push({date:'12',
       name:'第1页',
       address:'33'})
  }
  }
  else{
    let num=value;
    for(let i=num;i<num+5;i++){
      number.arry.push({date:'12',
        name:'第'+num+'页',
        address:'33'})
    }
  }
}
gets(1)
</script>

子组件

<style scoped>
.el-table{
  position: absolute;
  left: 50%;
  transform: translate(-50%);
}
</style>
<template>
  <el-table  :data="tableData"  border style="width: 70%">
    <el-table-column prop="date" label="Date" width="" />
    <el-table-column prop="name" label="Name" width="" />
    <el-table-column prop="address" label="Address" />
  </el-table>
</template>

<script lang="ts" setup>
 defineProps({
       tableData:Array
     }
 )

</script>

### Vue 表格翻页保持选中状态的实现方法 在 Vue 中实现表格翻页时保存选中状态的功能,可以通过多种方式完成。以下是基于 Element UI 和 Ant Design Vue 的两种常见解决方案。 #### 使用 Element UI 实现 通过设置 `:reserve-selection="true"` 属性可以轻松实现翻页时保留选中状态的功能[^4]。此属性的作用是在分页切换时不丢失已选中的行数据。具体代码如下: ```html <template> <el-table ref="tableRef" :data="tableData" style="width: 100%" :row-key="getRowKey" :reserve-selection="true"> <el-table-column type="selection" width="55"></el-table-column> <!-- 其他列定义 --> </el-table> </template> <script> export default { data() { return { tableData: [], // 数据源 }; }, methods: { getRowKey(row) { return row.id; // 唯一标识字段 }, clearSelection() { this.$refs.tableRef.clearSelection(); // 清除所有选中项 } } }; </script> ``` 上述代码中,`get-row-key` 方法用于指定每一行的唯一键值,通常为数据库主键或其他唯一标识符。这样即使分页发生变化,也可以正确匹配并恢复之前的选择状态[^1]。 --- #### 使用 Ant Design Vue 实现 Ant Design Vue 提供了类似的机制来支持翻页时的状态保持。其核心在于合理配置 `rowSelection` 参数以及自定义逻辑处理。以下是一个完整的例子: ```html <a-table :row-key="(record) => record.id" :columns="columns" :data-source="dataSource" :pagination="pagination" @change="handleTableChange" :row-selection="rowSelectionConfig" ></a-table> ``` 其中,`rowSelectionConfig` 是一个对象,用来描述如何管理选中行为。例如: ```javascript computed: { rowSelectionConfig() { const selectedRowKeys = []; return { onChange: (selectedRowKeys, selectedRows) => { console.log('Selected Rows:', selectedRows); }, getCheckboxProps: (record) => ({ props: { disabled: false, }, }), }; }, }, methods: { handleTableChange(pagination, filters, sorter) { // 处理分页变化事件 console.log('Pagination Changed', pagination); }, } ``` 为了确保跨分页的数据一致性,可以在每次分页变更前手动存储当前选中的行 ID 列表,并在加载新数据后重新应用这些选择[^2]。 --- #### 自定义缓存方案 对于更复杂的需求(如某些特殊场景下无法直接依赖框架特性),还可以采用手动生成缓存的方式。即每当用户勾选某一行时,将其对应的唯一标识加入到全局变量或 Vuex Store 中;当页面刷新或者跳转至其他分页后再依据该集合自动标记对应条目为“已选中”。这种方法虽然稍显繁琐,但灵活性更高[^3]^。 --- ### 总结 无论是借助第三方库内置选项还是自行开发插件扩展功能模块,最终目标都是让用户获得流畅的操作体验的同时减少不必要的重复劳动成本。以上便是关于 **Vue 表格翻页保持选中状态** 几种主流解决思路及其实际应用场景分析说明文档内容摘录整理而成的结果展示形式之一而已啦!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值