当前位置:   article > 正文

Vue中使用vxe-table组件分页查询,多页选择数据回显,分页记录保存选中的数据_vxe-table分页

vxe-table分页

官方示例:vxe-table v3https://vxetable.cn/v3/#/table/advanced/page

 当表格中需要渲染的数据量比较大,有几万几十万条数据时,在前端分页将会非常慢,建议将当前页码和每页数量传递个后端,后端分好后给前端渲染。

后端查询SQL时,通过limit 和offset 查询分页:

sql += ` limit ${pageSize} offset ${pageSize * (currentPage - 1)} `

后端除了返回要显示的页数据外,通常还要传递总数量total:

  1. router.get('/', async (ctx, next) => {
  2. let { task_info, state, currentPage, pageSize, check_host_name_alias, need_update_tile, priority, check_progress, download_progress, downloadfialdcount, check_faild_count } = ctx.request.query
  3. let sql = `select * from (
  4. SELECT
  5. SUBSTRING (task_info, 1, 11) as task_info_substring,
  6. ( SELECT NAME FROM downloaduser WHERE machine_code = T.check_host_name ) AS "check_host_name_alias",
  7. ( SELECT NAME FROM downloaduser WHERE machine_code = T.download_host_name ) AS "download_host_name_alias",
  8. ( CASE T.STATE WHEN 1 THEN now( ) - T.check_start_time ELSE T.check_end_time - T.check_start_time END ) AS "check_use_time",
  9. ( CASE T.STATE WHEN 4 THEN now( ) - T.download_start_time ELSE T.download_end_time - T.check_start_time END ) AS "download_use_time",
  10. *
  11. FROM
  12. task T ) t2 where 1=1`
  13. if (task_info) {
  14. // console.log("task_info:", task_info)
  15. let task_info_arr = task_info.split("\n")
  16. sql += ` and t2.task_info_substring in (${format_arr_to_str(task_info_arr)})`
  17. }
  18. if (state) {
  19. sql += ` and state = ${state}`
  20. }
  21. if (need_update_tile) {
  22. sql += ` and need_update_tile like '%${need_update_tile}%'`
  23. }
  24. // sql += ' order by task_info asc'
  25. // 多字段排序
  26. // if (priority) {
  27. // sql += ` ,priority ${priority}`
  28. // }
  29. if (priority) {
  30. sql += ` order by priority ${priority}`
  31. }
  32. let data_count = await db.query(`select count(*) from (${sql}) t_temp`);//满足条件的记录总数
  33. let total = parseInt(data_count.rows[0].count)
  34. sql += ` limit ${pageSize} offset ${pageSize * (currentPage - 1)} `
  35. // console.log("sql:", sql)
  36. let data = await db.query(sql);
  37. ctx.body = {
  38. code: 0,
  39. message: "success",
  40. data: { ...data, total }
  41. }
  42. })

注意:这里查询总数量时,是用的`select count(*) from (${sql}) t_temp` ,这样查询速度比较快。如果全表查询,再返回查出的rows.length,这样速度会很慢 

对于多页选择,需要指定row-id=“主键字段”:checkbox-config="{ reserve: true }"

获取当前页选中的行数据:调用 this.$refs.ref_table_task.getCheckboxRecords()

获取当前页以外选中的行数据:调用 this.$refs.ref_table_task.getCheckboxReserveRecords()

注意:经测试,每页最多选中500行左右,否则在切换回选中的页时会非常卡 

给表格列表添加序号

每一页的第一条序号都是1:(默认)

<vxe-column type="seq" width="60" fixed="left"></vxe-column>

如果想让分页后序号不从1开始 ,而是逐渐累加,操作如下:

  1. const pageInfo = ref({
  2. currentPage: 1,
  3. pageSize: 10,
  4. pageTotal: 20,
  5. })
  6. // 配置显示序号
  7. const seqConfig = reactive({
  8. seqMethod({ rowIndex }) {
  9. return (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize + rowIndex + 1
  10. }
  11. })

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/49191
推荐阅读
相关标签
  

闽ICP备14008679号