• VUE Element UI 排序功能结合后端接口实现


    1. 需求

    要求在页面列表上点击某个列的排序按钮,能够联动后端接口一起排序分页。

    2. 前端实现

    废话不多说,直接上代码

    <el-table
            ref="dsTable"
            :data="displayData"
            height="100%"
            :data-selectable="optionData.selectable"
            :auto-hide-selection="optionData.autoHideSelectable"
            :show-column-selection="optionData.showColumnSelection"
            :border="optionData.columnResizable"
            highlight-current-row
            @row-click="handleRowClick"
            @selection-change="handleSelectionChange"
            :default-sort="{ prop: 'entityName', order: 'descending' }"
            @sort-change="handleSortChange"
            :header-cell-class-name="handleHeaderCellClass"
          >
            <el-table-column
              label="实体名称"
              show-overflow-tooltip
              prop="entityName"
              sortable="custom"
              :sort-orders="['descending', 'ascending', null]"
            ></el-table-column>
            <el-table-column
              label="实体中文名"
              show-overflow-tooltip
              prop="entityChName"
            ></el-table-column>
            <el-table-column
              label="模型名称"
              show-overflow-tooltip
              prop="modelName"
            ></el-table-column>
            <el-table-column
              label="最新提交时间"
              show-overflow-tooltip
              prop="commitTime"
              width="135"
              :formatter="$timeFormatter"
              sortable="custom"
              :sort-orders="['descending', 'ascending', null]"
            ></el-table-column>
          </el-table>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    其中重要点是:
    el-table 标签上的:
    :default-sort=“{ prop: ‘entityName’, order: ‘descending’ }”
    @sort-change=“handleSortChange”
    :header-cell-class-name=“handleHeaderCellClass”
    el-table-column 标签上的
    :sort-orders=“[‘descending’, ‘ascending’, null]”

    下面是具体的JS方法

    export default {
      data() {
        return {
    	  // 排序
          orderArray: [],
          sortField: {},
          curSort: '',
          orderBySql: '',
        }
      },
      methods: {
        getData() {
          const params = {
            orderBySql: this.orderBySql,
          }
          this.$http
            .post(`/aaaa/bbbb/cccc/page`, params)
            .then(res => {
              this.tableLoading = false
              let resData = res.data
              if (resData.code == 1) {
                this.displayData = resData.data.list
                this.total = resData.data.total
              } else {
                this.$message.error(resData.message)
              }
            })
            .catch(e => {
              console.log(e,'服务器内部错误')
            })
        },
        handleSortChange({ column, prop, order }) {
          if (!order || this.sortField[prop] === order) {
            // 排序字段按触发顺序确定排列优先级,需要删除字段确保下次触发时位于最后优先级
            delete this.sortField[prop]
          } else {
            this.sortField[prop] = order
          }
          if (order) {
            // 参与排序
            let flagIsHave = false
            this.orderArray.forEach(element => {
              if (element.prop === prop) {
                element.order = order
                flagIsHave = true
              }
            })
            if (!flagIsHave) {
              this.orderArray.push({
                prop: prop,
                order: order,
              })
            }
            this.curSort = order == 'descending' ? 'desc' : 'asc'
          } else {
            // 不参与排序
            this.orderArray = this.orderArray.filter(element => {
              return element.prop !== prop
            })
            // 取消当前的排序,curSort是当前点击项的前一项的order
            if (this.orderArray.length <= 0) {
              this.curSort = 'asc'
            } else {
              this.curSort =
                this.orderArray[this.orderArray.length - 1].order == 'descending'
                  ? 'desc'
                  : 'asc'
              console.log(this.curSort, 'ppp')
            }
          }
          // 转换字段属性
          this.getSortList(this.orderArray)
        },
        getSortList(arr) {
          // 组装排序 语句
          if (arr.length > 0) {
            let orderBySql = ''
            arr.forEach(item => {
              let prop = this.strChange(item.prop)
              let order = item.order == 'descending' ? 'desc' : 'asc'
              orderBySql += prop + ' ' + order + ' ,'
            })
            orderBySql = 'order by ' + orderBySql.substr(0, orderBySql.length - 1)
            // 调后端列表接口
            this.orderBySql = orderBySql
            this.getData()
          } else {
            // 调后端列表接口
            if (
              typeof this.orderBySql != 'undefined' &&
              this.orderBySql != null &&
              this.orderBySql != ''
            ) {
              this.orderBySql = ''
              this.getData()
            }
          }
        },
        // 驼峰改下划线
        strChange(arg) {
          var str = arg.split('')
          for (var i = 0; i < str.length; i++) {
            if (str[i].charCodeAt() >= 65 && str[i].charCodeAt() <= 90) {
              str[i] = '_' + str[i].toLowerCase()
            }
          }
          return str.join('')
        },
        // 添加排序方法(可把多个字段共同加入排序)
        handleHeaderCellClass({ row, column, rowIndex, columnIndex }) {
          this.orderArray.forEach(element => {
            if (column.property === element.prop) {
              column.order = element.order
            }
          })
        },
      },
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118

    上述JS仅仅提供的是排序相关代码,其他代码自行补充。

    3. 后端实现

    后端接受到的参数是这样的:
    “orderBySql”: “order by commit_time desc”
    所以 后端代码就知道该怎么写了吧。

    若有错误,希望大佬指出。
    对你有帮助给点个👍再走呗。

  • 相关阅读:
    JAVA在线课程教学大纲系统计算机毕业设计Mybatis+系统+数据库+调试部署
    ubuntu安装Anaconda 以及 dataspell配置jupyter
    深度学习在医疗保健领域的应用:从图像识别到疾病预测
    supOS APP开发者课程练习册
    对OSI 7层模型的理解
    kali 切换系统语言
    stm32看门狗
    Runtime——KVC,KVO原理
    论文总结2 大样本情况下线性概率模型与广义线性模型的比较
    WPF Border设置渐变色
  • 原文地址:https://blog.csdn.net/weixin_44254243/article/details/131188819