vue(24) : 表格排序传递后端

<template>

  <el-table :key="tableKey" v-loading="listLoading" :data="listData" border fit highlight-current-row
    style="width: 100%;" :header-cell-style="{background:'#eef1f6',color:'#606266'}" @sort-change="sortChange">

    <el-table-column label="姓名" align="center" sortable prop="name" :class-name="getSortClass('name')">
      <template slot-scope="{row}">
        <span>{{ row.name }}</span>
      </template>
    </el-table-column>

    <el-table-column label="年龄" align="center" sortable prop="age" :class-name="getSortClass('age')">
      <template slot-scope="{row}">
        <span>{{ row.age }}</span>
      </template>
    </el-table-column>

    <el-table-column label="身高" align="center" sortable prop="height" :class-name="getSortClass('height')">
      <template slot-scope="{row}">
        <span>{{ row.height }}</span>
      </template>
    </el-table-column>
  </el-table>

</template>

<script>
  var currentSort = null
  var currentSortField = null
  export default {
    data() {
      return {
        tableKey: 0,
        listData: null,
        total: 0,
        listLoading: false,
        listQuery: {
          pageNo: 1,
          pageSize: 20,
          params: {
            name: null,
            age: null,
            height: null
          },
          sort: null
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        console.log(JSON.stringify(this.listQuery, null, '\t'))
        this.listData = []
        const o1 = {
          name: '可莉1',
          age: 15,
          height: 145
        }
        const o2 = {
          name: '可莉2',
          age: 14,
          height: 143
        }
        this.listData.push(o1)
        this.listData.push(o2)
      },
      sortChange(data) {
        const {
          prop,
          order
        } = data
        this.sortByID(prop, order)
      },
      sortByID(prop, order) {
        if (order === 'ascending') {
          order = 'asc'
        }
        if (order === 'descending') {
          order = 'desc'
        }
        if (order === 'asc') {
          this.listQuery.sort = `+:${prop}`
          currentSort = '+'
        } else if (order === 'desc') {
          this.listQuery.sort = `-:${prop}`
          currentSort = '-'
        } else {
          this.listQuery.sort = null
          currentSort = null
        }
        currentSortField = prop
        if (this.listQuery.sort != null && this.listQuery.sort.indexOf(':') != -1) {
          const strs = this.listQuery.sort.split(':')
          if (strs[0] === '+') {
            const sor = {
              field: strs[1],
              order: 'asc'
            }
            this.listQuery.sort = []
            this.listQuery.sort.push(sor)
          } else {
            const sor = {
              field: strs[1],
              order: 'desc'
            }
            this.listQuery.sort = []
            this.listQuery.sort.push(sor)
          }
        }
        if (this.listQuery.sort === undefined) {
          const sor = {
            field: 'age',
            order: 'desc'
          }
          this.listQuery.sort = []
          this.listQuery.sort.push(sor)
        }
        this.getList()
      },
      getSortClass: function(key) {
        if (currentSortField === key) {
          if (currentSort === '+') {
            return 'asc'
          } else if (currentSort === '-') {
            return 'desc'
          } else {
            return null
          }
        } else {
          return null
        }
      },
    }
  }
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值