• 表格数据方法、分页方法及组件的封装和分页组件的复用


    请假列表

     1、数据获取与显示的通用方法封装

    1. <template>
    2. <div> <el-table
    3. :data="tableData"
    4. height="450"
    5. border
    6. style="width: 100%"
    7. :default-sort="{ prop: 'number', order: 'Ascending' }"
    8. >
    9. <el-table-column type="selection" width="55"> el-table-column>
    10. <el-table-column prop="id" label="用户ID" align="center">
    11. el-table-column>
    12. <el-table-column prop="userId" label="所属班级" align="center">
    13. el-table-column>
    14. <el-table-column prop="title" label="请假理由" align="center" sortable>
    15. el-table-column>
    16. <el-table-column prop="completed" label="批准情况" align="center">
    17. el-table-column>
    18. <el-table-column fixed="right" label="操作" width="100" align="center">
    19. <template slot-scope="scope">
    20. <el-button
    21. @click="edit(scope.row)"
    22. type="primary"
    23. icon="el-icon-edit"
    24. circle
    25. size="mini"
    26. >el-button>
    27. <el-button
    28. @click="del(scope.row)"
    29. type="danger"
    30. icon="el-icon-delete"
    31. circle
    32. size="mini"
    33. >el-button>
    34. template>
    35. el-table-column>
    36. el-table>div>
    37. template>
    38. <script>
    39. import { getTableData } from '@/utils/table.js'
    40. export default {
    41. data(){
    42. return{
    43. tableData: []
    44. }
    45. },
    46. created(){
    47. getTableData(this, '/works')
    48. }
    49. }
    50. script>
    51. <style>
    52. style>

     table.js

    1. //请假列表的 表格数据获取 封装方法的处理
    2. export function getTableData(root, url, params ){
    3. root.service.get(url, { params: params || {} })
    4. .then(res => {
    5. if(res.data.status === 200 ){
    6. root.tableData = res.data.data
    7. root.total = res.total
    8. }
    9. })
    10. .catch((error) => {
    11. throw error;
    12. });
    13. }

    2、数据转换

    共两层遍历:

    1、获取数据对象——user

    2、因为要进行转换格式的数据在每一个的user对象里,∴先进行遍历user对象

    3、每一个user对象都有很多数据,都是键值对的方式。当表格中需要转换的数据不只一两处,为了提高代码复用性,所以采用数组的方式,存放需要转换的数据的键名。最后只需要遍历user对象的数组,通过三元式进行判断,再修改新值(为了显示且不修改原数据而建)

    1. <template>
    2. <div> <el-table
    3. :data="tableData"
    4. height="450"
    5. border
    6. style="width: 100%"
    7. :default-sort="{ prop: 'number', order: 'Ascending' }"
    8. >
    9. <el-table-column type="selection" width="55"> el-table-column>
    10. <el-table-column prop="id" label="用户ID" align="center">
    11. el-table-column>
    12. <el-table-column prop="userId" label="所属班级" align="center">
    13. el-table-column>
    14. <el-table-column prop="title" label="请假理由" align="center" sortable>
    15. el-table-column>
    16. <el-table-column prop="completed_text" label="批准情况" align="center">
    17. el-table-column>
    18. <el-table-column fixed="right" label="操作" width="100" align="center">
    19. <template slot-scope="scope">
    20. <el-button
    21. @click="edit(scope.row)"
    22. type="primary"
    23. icon="el-icon-edit"
    24. circle
    25. size="mini"
    26. >el-button>
    27. <el-button
    28. @click="del(scope.row)"
    29. type="danger"
    30. icon="el-icon-delete"
    31. circle
    32. size="mini"
    33. >el-button>
    34. template>
    35. el-table-column>
    36. el-table>div>
    37. template>
    38. <script>
    39. import { getTableData } from '@/utils/table.js'
    40. export default {
    41. data(){
    42. return{
    43. tableData: []
    44. }
    45. },
    46. created(){
    47. getTableData(this, '/works', {} , ['completed'])
    48. }
    49. }
    50. script>
    51. <style>
    52. style>
    1. //请假列表的 表格数据获取 封装方法的处理
    2. export function getTableData(root, url, params, arr ){
    3. root.service.get(url, { params: params || {} })
    4. .then(res => {
    5. if(res.data.status === 200 ){
    6. root.tableData = res.data.data
    7. root.total = res.total
    8. root.tableData.map(item => {
    9. arr.map(aItem => [
    10. item[aItem] ? item[aItem + '_text'] = '已批准' : item[aItem + '_text'] = '未批准'
    11. ])
    12. })
    13. }
    14. })
    15. .catch((error) => {
    16. throw error;
    17. });
    18. }

    数据加载动画        ——      使用官方文档

    指令方式: 服务方式:

     后端实现分页

     查看请求后端接口:发现需要两个参数——当页数和每页条数

     前端的方法是用公式计算,参考博文

     这里用后端的方法:分页由后端完成,所以每次切换页面的数据条数和当前页码时,都要根据接口请求参数,发送请求。

    debug:总条数和数据页码不显示。 

    修改添加scss样式后:

    1. <template>
    2. <div class="absenceList">
    3. <el-table
    4. :data="tableData"
    5. height="450"
    6. border
    7. style="width: 100%"
    8. :default-sort="{ prop: 'number', order: 'Ascending' }"
    9. v-loading="loading"
    10. element-loading-text="拼命加载ing"
    11. element-loading-spinner="el-icon-loading"
    12. element-loading-background="rgba(0, 0, 0, 0.8)"
    13. >
    14. <el-table-column type="selection" width="55"> el-table-column>
    15. <el-table-column prop="id" label="用户ID" align="center">
    16. el-table-column>
    17. <el-table-column prop="userId" label="所属班级" align="center">
    18. el-table-column>
    19. <el-table-column prop="title" label="请假理由" align="center" sortable>
    20. el-table-column>
    21. <el-table-column prop="completed_text" label="批准情况" align="center">
    22. el-table-column>
    23. <el-table-column fixed="right" label="操作" width="100" align="center">
    24. <template slot-scope="scope">
    25. <el-button
    26. @click="edit(scope.row)"
    27. type="primary"
    28. icon="el-icon-edit"
    29. circle
    30. size="mini"
    31. >el-button>
    32. <el-button
    33. @click="del(scope.row)"
    34. type="danger"
    35. icon="el-icon-delete"
    36. circle
    37. size="mini"
    38. >el-button>
    39. template>
    40. el-table-column>
    41. el-table>
    42. <el-pagination
    43. @size-change="handleSizeChange"
    44. @current-change="handleCurrentChange"
    45. :current-page="currentPage"
    46. :page-sizes="[5, 10, 20, 50, 100]"
    47. :page-size="pageSize"
    48. layout="total, sizes, prev, pager, next, jumper"
    49. :total="total"
    50. >
    51. el-pagination>
    52. div>
    53. template>
    54. <script>
    55. import { getTableData } from "@/utils/table.js";
    56. export default {
    57. data() {
    58. return {
    59. tableData: [],
    60. total: 0,
    61. currentPage: 1,
    62. pageSize: 10,
    63. loading: true, //加载动画
    64. };
    65. },
    66. created() {
    67. getTableData(this, "/works", { page: this.currentPage, size: this.pageSize }, ["completed"]);
    68. },
    69. methods: {
    70. handleSizeChange(val) {
    71. this.pageSize = val;
    72. this.currentPage = 1;
    73. getTableData(this, "/works", { page: this.currentPage, size: val }, ["completed"]);
    74. },
    75. handleCurrentChange(val) {
    76. this.currentPage = val;
    77. getTableData(this, "/works", { page: val, size: this.pageSize }, ["completed"]);
    78. },
    79. },
    80. };
    81. script>
    82. <style lang="scss">
    83. .absenceList{
    84. .el-pagination{
    85. text-align: left;
    86. margin-top: 20px;
    87. }
    88. }
    89. style>
    1. //请假列表的 表格数据获取 封装方法的处理
    2. export function getTableData(root, url, params, arr ){
    3. root.service.get(url, { params: params || {} })
    4. .then(res => {
    5. if(res.data.status === 200 ){
    6. root.tableData = res.data.data
    7. root.total = res.data.total
    8. root.tableData.map(item => {
    9. arr.map(aItem => [
    10. item[aItem] ? item[aItem + '_text'] = '已批准' : item[aItem + '_text'] = '未批准'
    11. ])
    12. })
    13. root.loading = false
    14. }
    15. })
    16. .catch((error) => {
    17. throw error;
    18. });
    19. }

    后端分页组件封装和复用

     在common文件夹下,新建Pagin.vue组件,把上一个页面与分页相关的代码复制过来,修改后如下:

    1. <template>
    2. <div class="pagination">
    3. <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
    4. :page-sizes="[5, 10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
    5. :total="total" :url="url">
    6. el-pagination>
    7. div>
    8. template>
    9. <script>
    10. import { getTableData } from "@/utils/table.js";
    11. export default {
    12. props: {
    13. total: Number,
    14. url: String,
    15. },
    16. data() {
    17. return {
    18. currentPage: 1,
    19. pageSize: 10,
    20. };
    21. },
    22. created() {
    23. getTableData(
    24. this.$parent,
    25. this.url,
    26. { page: this.currentPage, size: this.pageSize },
    27. ["completed"]
    28. );
    29. },
    30. methods: {
    31. handleSizeChange(val) {
    32. this.pageSize = val;
    33. this.currentPage = 1;
    34. getTableData(
    35. this.$parent,
    36. this.url,
    37. { page: this.currentPage, size: val },
    38. ["completed"]
    39. );
    40. },
    41. handleCurrentChange(val) {
    42. this.currentPage = val;
    43. getTableData(this.$parent, this.url, { page: val, size: this.pageSize }, [
    44. "completed",
    45. ]);
    46. },
    47. },
    48. };
    49. script>
    50. <style lang="scss">
    51. .pagination {
    52. .el-pagination {
    53. text-align: left;
    54. margin-top: 20px;
    55. }
    56. }
    57. style>

    使用组件化的思想,封装分页组件, 需要注意的点:

            1、只保留与分页本身功能相关的参数

             2、发送后端请求的参数 ===>父组件根节点以及传递的url

     分页组件复用:

     AbsenceListsManage.vue

    1. <template>
    2. <div class="absenceListsManage">
    3. <el-table
    4. :data="tableData"
    5. height="450"
    6. border
    7. style="width: 100%"
    8. :default-sort="{ prop: 'number', order: 'Ascending' }"
    9. v-loading="loading"
    10. element-loading-text="拼命加载ing"
    11. element-loading-spinner="el-icon-loading"
    12. element-loading-background="rgba(0, 0, 0, 0.8)"
    13. >
    14. <el-table-column type="selection" width="55"> el-table-column>
    15. <el-table-column prop="id" label="用户ID" align="center">
    16. el-table-column>
    17. <el-table-column prop="userId" label="所属班级" align="center">
    18. el-table-column>
    19. <el-table-column prop="title" label="请假理由" align="center" sortable>
    20. el-table-column>
    21. <el-table-column prop="completed_text" label="批准情况" align="center">
    22. el-table-column>
    23. <el-table-column fixed="right" label="操作" width="100" align="center">
    24. <template slot-scope="scope">
    25. <el-button
    26. @click="edit(scope.row)"
    27. type="primary"
    28. icon="el-icon-edit"
    29. circle
    30. size="mini"
    31. >el-button>
    32. <el-button
    33. @click="del(scope.row)"
    34. type="danger"
    35. icon="el-icon-delete"
    36. circle
    37. size="mini"
    38. >el-button>
    39. template>
    40. el-table-column>
    41. el-table>
    42. <Paging :total="total" :url="url">Paging>
    43. div>
    44. template>
    45. <script>
    46. import Paging from "../common/Paging.vue";
    47. export default {
    48. components: {
    49. Paging,
    50. },
    51. data() {
    52. return {
    53. tableData: [],
    54. total: 0,
    55. loading: true, //加载动画
    56. url: "/works",
    57. };
    58. },
    59. };
    60. script>
    61. <style lang="scss">
    62. .absenceListsManage {
    63. .el-pagination {
    64. text-align: left;
    65. margin-top: 20px;
    66. }
    67. }
    68. style>

    使用组件化后最后效果一样,代码可读性更强。 

  • 相关阅读:
    2022年最新山东建筑八大员(材料员)模拟真题及答案
    【JDBC篇】java连接mysql数据库过程原理剖析(一)
    目前安卓、鸿蒙、澎湃的关系
    带网络变压器的RJ45网口连接器/集成RJ45网口连接器
    按照指定条件对数据进行分组并对每个分组内的全部数据应用自定义函数进行聚合计算groupby().apply()
    vue + el-checkbox 单选功能
    Java使用HttpClient实现远程服务调用
    IIC协议
    缓存P27,28,29
    【MATLAB源码-第43期】基于matlab的turbo码误码率仿真比较不同迭代次数,采用logmap/sova算法。
  • 原文地址:https://blog.csdn.net/qq_45947664/article/details/128188863