当前位置:   article > 正文

VUE+Element-ui实战之el-tabs切换实时加载el-table表格数据

el-tabs

实战场景描述

实际开发过程中,需要Tab切换不同类型的数据,如果是每个Tab表单是静态数据还好,但通常情况下基本上涉及的都是table数据,页面打开时,我们看到的是一个打开的Tab内容,但其实vue是把所有的内容都渲染了,只是其他都隐藏了,这种可能就会造成打开页面时因为需要同时加载多个数据而卡顿,最佳实现思路肯定是Tab切换时才进行实时加载

1、确定好每个Tab内容

参考官网的例子改成我们的内容就好

2、Tab切换事件

今天的列子每个tab都涉及表格,为了方便后期维护和切换刷新,把每个Tab内容都单独抽离成一个组件,并且用v-if控制显示隐藏。这个操作在Tab点击切换时进行设置即可。

 

3、完整代码

主页面:

  1. <template>
  2. <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick" >
  3. <!-------------------------------------------------------->
  4. <el-tab-pane label="待审核" name="audit">
  5. <AuditList v-if="activeName == 'audit'" ref="audit"></AuditList>
  6. </el-tab-pane>
  7. <!-------------------------------------------------------->
  8. <el-tab-pane label="已通过" name="pass">
  9. <PassList v-if="activeName == 'pass'" ref="pass"></PassList></el-tab-pane>
  10. <!-------------------------------------------------------->
  11. <el-tab-pane label="已否决" name="noPass">
  12. <NoPassList v-if="activeName == 'noPass'" ref="noPass"></NoPassList>
  13. </el-tab-pane>
  14. <!-------------------------------------------------------->
  15. <el-tab-pane label="已忽略" name="ignore">
  16. <IgnoreList v-if="activeName == 'ignore'" ref="ignore"></IgnoreList>
  17. </el-tab-pane>
  18. </el-tabs>
  19. </template>
  20. <script>
  21. import PassList from "./PassList2";
  22. import NoPassList from "./NoPassList2";
  23. import IgnoreList from "./IgnoreList2";
  24. import AuditList from "./AuditList2";
  25. export default {
  26. name: "TEMPLATE",
  27. components: {
  28. PassList,
  29. NoPassList,
  30. IgnoreList,
  31. AuditList
  32. },
  33. data(){
  34. return {
  35. activeName: 'audit'
  36. };
  37. },
  38. mounted(){
  39. this.onQuery();
  40. },
  41. methods:{
  42. handleClick(tab, event) {
  43. this.activeName = tab.name;
  44. var that = this;
  45. setTimeout(function(){
  46. that.onQuery();
  47. },500);
  48. },
  49. onQuery() {
  50. this.$refs[this.activeName].getList();
  51. },
  52. }
  53. }
  54. </script>
  55. <style scoped>
  56. </style>
已通过PassList2.vue:
  1. <template>
  2. <div>
  3. <el-table
  4. :data="tableData"
  5. border
  6. stripe
  7. style="width: 100%">
  8. <el-table-column
  9. label="序号"
  10. align="center"
  11. width="70px">
  12. <template slot-scope="scope">
  13. {{(formInline.currentPage-1)*formInline.pageSize + scope.$index + 1}}
  14. </template>
  15. </el-table-column>
  16. <el-table-column
  17. label="人员姓名"
  18. prop="pName">
  19. </el-table-column>
  20. <el-table-column
  21. label="联系电话"
  22. prop="phone">
  23. </el-table-column>
  24. <el-table-column
  25. label="所在楼层"
  26. prop="floor">
  27. </el-table-column>
  28. <el-table-column
  29. label="房间编号"
  30. prop="spaceNum">
  31. </el-table-column>
  32. <el-table-column
  33. label="人员备注"
  34. prop="notes">
  35. </el-table-column>
  36. <el-table-column
  37. label="提交时间"
  38. prop="submitTime">
  39. </el-table-column>
  40. <el-table-column
  41. label="审核时间"
  42. prop="auditTime">
  43. </el-table-column>
  44. </el-table>
  45. <el-pagination background
  46. layout="total, prev, pager, next, sizes,jumper"
  47. :page-sizes="[5, 10, 15]"
  48. :page-size="formInline.pageSize"
  49. :total="dataTotalCount"
  50. @size-change="handleSizeChange"
  51. @current-change="handleCurrentChange">
  52. </el-pagination>
  53. </div>
  54. </template>
  55. <script>
  56. export default {
  57. name: "PassList",
  58. data(){
  59. return {
  60. dataTotalCount: 0, //查询条件的总数据量
  61. formInline: {
  62. currentPage: 1,
  63. pageSize:10,
  64. },
  65. tableData: [],
  66. }
  67. },
  68. methods:{
  69. //分页 初始页currentPage、初始每页数据数pagesize和数据testpage--->控制每页几条
  70. handleSizeChange: function(size) {
  71. this.formInline.pageSize = size;
  72. this.getList();
  73. },
  74. // 控制页面的切换
  75. handleCurrentChange: function(currentpage) {
  76. this.formInline.currentPage = currentpage;
  77. this.getList();
  78. },
  79. getList() {
  80. var that = this;
  81. that.axios.get('/dev-api/server/accessAudit/passList', {params:this.formInline})
  82. .then(function (response) {
  83. that.tableData = response.data.data.items;
  84. that.dataTotalCount = response.data.data.total;
  85. })
  86. .catch(function (error) {
  87. that.$message({
  88. type: 'error',
  89. message: '系统异常:'+error
  90. });
  91. });
  92. },
  93. }
  94. }
  95. </script>
  96. <style scoped>
  97. </style>

 已否决列表NoPassList2.vue

  1. <template>
  2. <div>
  3. <el-table
  4. :data="tableData"
  5. border
  6. stripe
  7. style="width: 100%">
  8. <el-table-column
  9. label="序号"
  10. align="center"
  11. width="70px">
  12. <template slot-scope="scope">
  13. {{(formInline.currentPage-1)*formInline.pageSize + scope.$index + 1}}
  14. </template>
  15. </el-table-column>
  16. <el-table-column
  17. label="人员姓名"
  18. prop="pName">
  19. </el-table-column>
  20. <el-table-column
  21. label="联系电话"
  22. prop="phone">
  23. </el-table-column>
  24. <el-table-column
  25. label="所在楼层"
  26. prop="floor">
  27. </el-table-column>
  28. <el-table-column
  29. label="房间编号"
  30. prop="spaceNum">
  31. </el-table-column>
  32. <el-table-column
  33. label="提交时间"
  34. prop="submitTime">
  35. </el-table-column>
  36. <el-table-column
  37. label="人员备注"
  38. prop="notes">
  39. </el-table-column>
  40. <el-table-column
  41. label="审核时间"
  42. prop="auditTime">
  43. </el-table-column>
  44. <el-table-column
  45. label="否决原因"
  46. prop="reason">
  47. </el-table-column>
  48. </el-table>
  49. <el-pagination background
  50. layout="total, prev, pager, next, sizes,jumper"
  51. :page-sizes="[5, 10, 15]"
  52. :page-size="formInline.pageSize"
  53. :total="dataTotalCount"
  54. @size-change="handleSizeChange"
  55. @current-change="handleCurrentChange">
  56. </el-pagination>
  57. </div>
  58. </template>
  59. <script>
  60. export default {
  61. name: "NoPassList",
  62. data(){
  63. return {
  64. dataTotalCount: 0, //查询条件的总数据量
  65. formInline: {
  66. currentPage: 1,
  67. pageSize:10,
  68. },
  69. tableData: [],
  70. }
  71. },
  72. methods:{
  73. //分页 初始页currentPage、初始每页数据数pagesize和数据testpage--->控制每页几条
  74. handleSizeChange: function(size) {
  75. this.formInline.pageSize = size;
  76. this.getList();
  77. },
  78. // 控制页面的切换
  79. handleCurrentChange: function(currentpage) {
  80. this.formInline.currentPage = currentpage;
  81. this.getList();
  82. },
  83. getList(data) {
  84. if(data){
  85. this.formInline.info = data.info;
  86. this.formInline.pType = data.pType;
  87. this.formInline.floor = data.floor;
  88. }
  89. var that = this;
  90. that.axios.get('/dev-api/server/accessAudit/noPassList', {params:this.formInline})
  91. .then(function (response) {
  92. that.tableData = response.data.data.items;
  93. that.dataTotalCount = response.data.data.total;
  94. })
  95. .catch(function (error) {
  96. that.$message({
  97. type: 'error',
  98. message: '系统异常:'+error
  99. });
  100. });
  101. },
  102. }
  103. }
  104. </script>
  105. <style scoped>
  106. </style>

已忽略列表IgnoreList2.vue

  1. <template>
  2. <div>
  3. <el-table
  4. :data="tableData"
  5. border
  6. stripe
  7. style="width: 100%">
  8. <el-table-column
  9. label="序号"
  10. align="center"
  11. width="70px">
  12. <template slot-scope="scope">
  13. {{(formInline.currentPage-1)*formInline.pageSize + scope.$index + 1}}
  14. </template>
  15. </el-table-column>
  16. <el-table-column
  17. label="人员姓名"
  18. prop="pName">
  19. </el-table-column>
  20. <el-table-column
  21. label="联系电话"
  22. prop="phone">
  23. </el-table-column>
  24. <el-table-column
  25. label="所在楼层"
  26. prop="floor">
  27. </el-table-column>
  28. <el-table-column
  29. label="房间编号"
  30. prop="spaceNum">
  31. </el-table-column>
  32. <el-table-column
  33. label="人员备注"
  34. prop="notes">
  35. </el-table-column>
  36. <el-table-column
  37. label="提交时间"
  38. prop="submitTime">
  39. </el-table-column>
  40. <el-table-column
  41. label="审核时间"
  42. prop="auditTime">
  43. </el-table-column>
  44. </el-table>
  45. <el-pagination background
  46. layout="total, prev, pager, next, sizes,jumper"
  47. :page-sizes="[5, 10, 15]"
  48. :page-size="formInline.pageSize"
  49. :total="dataTotalCount"
  50. @size-change="handleSizeChange"
  51. @current-change="handleCurrentChange">
  52. </el-pagination>
  53. </div>
  54. </template>
  55. <script>
  56. export default {
  57. name: "IgnoreList",
  58. data(){
  59. return {
  60. dataTotalCount: 0, //查询条件的总数据量
  61. formInline: {
  62. currentPage: 1,
  63. pageSize:10,
  64. },
  65. tableData: [],
  66. }
  67. },
  68. methods:{
  69. //分页 初始页currentPage、初始每页数据数pagesize和数据testpage--->控制每页几条
  70. handleSizeChange: function(size) {
  71. this.formInline.pageSize = size;
  72. this.getList();
  73. },
  74. // 控制页面的切换
  75. handleCurrentChange: function(currentpage) {
  76. this.formInline.currentPage = currentpage;
  77. this.getList();
  78. },
  79. getList(data) {
  80. if(data){
  81. this.formInline.info = data.info;
  82. this.formInline.pType = data.pType;
  83. this.formInline.floor = data.floor;
  84. }
  85. var that = this;
  86. that.axios.get('/dev-api/server/accessAudit/ignoreList', {params:this.formInline})
  87. .then(function (response) {
  88. that.tableData = response.data.data.items;
  89. that.dataTotalCount = response.data.data.total;
  90. })
  91. .catch(function (error) {
  92. that.$message({
  93. type: 'error',
  94. message: '系统异常:'+error
  95. });
  96. });
  97. },
  98. }
  99. }
  100. </script>
  101. <style scoped>
  102. </style>

待审核列表AuditList2.vue

  1. <template>
  2. <div>
  3. <el-table
  4. :data="tableData"
  5. border
  6. stripe
  7. style="width: 100%">
  8. <el-table-column
  9. align="center"
  10. label="序号"
  11. width="70px">
  12. <template slot-scope="scope">
  13. {{(formInline.currentPage-1)*formInline.pageSize + scope.$index + 1}}
  14. </template>
  15. </el-table-column>
  16. <el-table-column
  17. label="人员姓名"
  18. prop="pName">
  19. </el-table-column>
  20. <el-table-column
  21. label="联系电话"
  22. prop="phone">
  23. </el-table-column>
  24. <el-table-column
  25. label="所在楼层"
  26. prop="floor">
  27. </el-table-column>
  28. <el-table-column
  29. label="房间编号"
  30. prop="spaceNum">
  31. </el-table-column>
  32. <el-table-column
  33. label="认证图片"
  34. prop="imgUrl">
  35. </el-table-column>
  36. <el-table-column
  37. label="提交时间"
  38. prop="submitTime">
  39. </el-table-column>
  40. <el-table-column
  41. label="人员备注"
  42. prop="notes">
  43. </el-table-column>
  44. <el-table-column align="center" fixed="right" label="操作" width="300">
  45. <template slot-scope="scope">
  46. <el-button icon="el-icon-s-claim" size="mini">通过</el-button>
  47. <el-button icon="el-icon-s-release" size="mini">否决</el-button>
  48. <el-button icon="el-icon-takeaway-box" size="mini">忽略</el-button>
  49. </template>
  50. </el-table-column>
  51. </el-table>
  52. <el-pagination :page-size="formInline.pageSize"
  53. :page-sizes="[5, 10, 15]"
  54. :total="dataTotalCount"
  55. @current-change="handleCurrentChange"
  56. @size-change="handleSizeChange"
  57. background
  58. layout="total, prev, pager, next, sizes,jumper">
  59. </el-pagination>
  60. </div>
  61. </template>
  62. <script>
  63. export default {
  64. name: "TEMPLATE",
  65. data() {
  66. return {
  67. dataTotalCount: 0, //查询条件的总数据量
  68. formInline: {
  69. currentPage: 1,
  70. pageSize: 10,
  71. },
  72. tableData: [],
  73. }
  74. },
  75. methods: {
  76. //分页 初始页currentPage、初始每页数据数pagesize和数据testpage--->控制每页几条
  77. handleSizeChange: function (size) {
  78. this.formInline.pageSize = size;
  79. this.getList();
  80. },
  81. // 控制页面的切换
  82. handleCurrentChange: function (currentpage) {
  83. this.formInline.currentPage = currentpage;
  84. this.getList();
  85. },
  86. getList() {
  87. var that = this;
  88. that.axios.get('/dev-api/server/accessAudit/list', {params: that.formInline})
  89. .then(function (response) {
  90. that.tableData = response.data.data.items;
  91. that.dataTotalCount = response.data.data.total;
  92. })
  93. .catch(function (error) {
  94. that.$message({
  95. type: 'error',
  96. message: '系统异常:' + error
  97. });
  98. });
  99. }
  100. }
  101. }
  102. </script>
  103. <style scoped>
  104. </style>

最终效果

注:表格中所有数据均为mock模拟数据,如有雷同纯属巧合^_^

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

闽ICP备14008679号