elementui el-table的sortable排序

附上elementui的官网

安装elementui以及引入

安装

npm i element-ui -S

引入,在main.js中

全部引入
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';

Vue.use(ElementUI);

new Vue({
  el: '#app',
  render: h => h(App)
});
按需引入,创建elementui.js,在main.js中直接引入
elementui.js
import Vue from 'vue';
import 'element-ui/lib/theme-chalk/index.css';
import { Table, TableColumn } from 'element-ui';
Vue.use(Table);
Vue.use(TableColumn);
main.js
import "../plugins/elementui";

el-table排序

el-table-column添加sortable属性

<el-table-column width="100" prop="name" label="账号" sortable> </el-table-column>

这个只是当前页面的排序,如果翻到下一页,排序还是默认的第二页的排序

如果排序需要用到后端去排序,需要将sortable的属性值设置为custom
并且给el-table添加sort-change方法,
设置默认排序类型和默认以哪一列排序 :default-sort =“{‘prop’: prop,‘order’:type}”

排序的字符为prop,如果排序的字符和td显示的字符不一样,可以用template

<el-table-column width="100" prop="parent_name" label="组织" sortable='custom'>
   <template slot-scope="scope">
     <span>{{scope.row.organization.parents?cope.row.organization.parent.name:''}}</span>
   </template>
</el-table-column>
template
<el-table height="100%" v-loading="$store.state.loading" ref="table" @sort-change="onSortChange" :default-sort ="{'prop': prop,'order':type}" :data="dateTable">
   <el-table-column width="100" prop="parent_name" label="组织" sortable='custom'>
      <template slot-scope="scope">
        <span>{{scope.row.organization.parent?scope.row.organization.parent.name:''}}</span>
      </template>
   </el-table-column>
</el-table>

script
data() {
    return {
      currentPage3: 1,
      skip:0,
      limit:10,
      prop:'created',
      type:'desc',
    }
  },
methods:{
	onSortChange({ prop, order }) {
      console.log(prop,order,this.currentPage3);
      order=='descending'?this.type='desc':''
      order=='ascending'?this.type='asc':this.type='desc'
      this.prop=prop
      this.$axios({
        method: this.COMMON.btnModule.account[0].methods,
        url: this.APIurl.apiUrl+this.COMMON.btnModule.account[0].path,
        params: { 
          skip: (this.currentPage3-1)*this.limit,
          limit: this.limit,
          order: this.prop,
          type: this.type,
        },
      })
      .then((res) => {
        console.log( res.data.datas,res.data.datas.length);
        this.$store.commit('setTableData',res.data)
      })
    },
}

效果如下

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值