Vue之sortable實現排序功能


 

1.vue代碼

<template>
      <el-table @selection-change="handleSelectionChange" @sort-change="sortChange" v-loading="loading" id = "TableColumnID" element-loading-text="加載中..." :data="listData.List" highlight-current-row style="width: 100%;">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column width="80" prop="UserName" label="登錄名" :show-overflow-tooltip="true" sortable="custom"></el-table-column>
        <el-table-column width="80" prop="byName" label="姓名" :show-overflow-tooltip="true" sortable="custom"></el-table-column>
        <el-table-column :formatter="roleType" prop="Role" label="用戶權限" :show-overflow-tooltip="true" sortable="custom" width="150"></el-table-column>
        <el-table-column prop="LastActivityTime" label="更新時間" sortable="custom" width="200"></el-table-column>
        <el-table-column prop="CreateTime" label="創建時間" sortable="custom" width="200"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">編輯</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">刪除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

 

2.sort-change事件分析

如果需要后端實現排序功能,需要將sortable設置為custom,同時在 Table 上監聽sort-change事件,在事件回調中可以獲取當前排序的字段名和排序順序,從而向接口請求排序后的表格數據。

sort-change方法自帶三個參數,分別代表意義是:
column:當前列
prop:當前列需要排序的數據
order:排序的規則(升序、降序和默認[默認就是沒排序])

3.JS代碼:

 data() {
    return {
      loading: false,
      listData: [],
      addVisible: false,
      currentObj: { U: {}, UAccount: {} },
      queryData: {
        UserName: "",
        CustomerCode: "",
        CustomerName: "",
        role: 0,
        p_Role: "",
        prop : "",
        order : "",
        currentPage: 1,
        pageSize: 10
      },
      EUserP_Role: [],
      p_Role: {},
      ids: []
    };
  },
 methods: {
    sortChange(column,prop,order){
      if(column.prop == null || column.order == null){
        this.queryData.prop = "";
        this.queryData.order = "";
      }else{
         this.queryData.prop = column.prop;
         this.queryData.order = column.order;
      }
      this.getList();
    },
    getList() {
      this.loading = true;
      this.$API.User.List(this.queryData).then(res => {
        this.loading = false;
        this.listData = res;
        this.currentPage = 1;
        this.prop = column.prop;
        this.order = column.order;
      });
    }

 

 

參考---https://blog.csdn.net/nxw_tsp/article/details/86096914


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM