溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue中Element-ui表格怎么實現(xiàn)樹形結(jié)構(gòu)表格

發(fā)布時間:2021-06-07 10:02:46 來源:億速云 閱讀:1462 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“vue中Element-ui表格怎么實現(xiàn)樹形結(jié)構(gòu)表格”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“vue中Element-ui表格怎么實現(xiàn)樹形結(jié)構(gòu)表格”這篇文章吧。

本文實例為大家分享了Element-ui表格實現(xiàn)樹形結(jié)構(gòu)表格的具體代碼,供大家參考,具體內(nèi)容如下

前端效果展示:

vue中Element-ui表格怎么實現(xiàn)樹形結(jié)構(gòu)表格

在el-table中,支持樹類型的數(shù)據(jù)的顯示。當(dāng) row 中包含 children 字段時,被視為樹形數(shù)據(jù)。渲染樹形數(shù)據(jù)時,必須要指定 row-key。支持子節(jié)點數(shù)據(jù)異步加載。

通過指定 row 中的 hasChildren 字段來指定哪些行是包含子節(jié)點。children 與 hasChildren 都可以通過 tree-props 配置。

row-key="id"和:tree-props="{children: 'children', hasChildren: 'hasChildren'}是必須的。

下面是vue的表格樹:

 <!--表格-->  
       <el-row>
            <el-table :data="tableData"  row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                        <el-table-column prop="privilegeName" label="權(quán)限名稱" >
                        </el-table-column>
                        <el-table-column prop="privilegeCode" label="權(quán)限編碼" >
                        </el-table-column>
                        <el-table-column prop="privilegeType" label="權(quán)限類別" :formatter="formatPrivilegeType" >
                        </el-table-column>
 
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                
                                <el-button type="primary" size="mini" @click="toAdd(scope)">新增</el-button>
                                <el-button type="primary" size="mini" @click="toEdit(scope)">編輯</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <br>
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.pageIndex"
                        :page-sizes="[5, 10, 20, 30, 40]"
                        :page-size=pagination.pageSize
                        layout="total, prev, pager, next"
                        :total=pagination.total>
                    </el-pagination>
</el-row>

后端代碼:SpringBoot+MyPlus+MySQL8 實現(xiàn)數(shù)據(jù)結(jié)構(gòu)查詢

前端全部代碼:

<style>
</style>
<template>
  <div id="privilege-manager">
   <!--頂部菜單欄-->
    <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-button
              class="el-icon-refresh"
              type="primary"
              @click="toAdd()">添加
            </el-button>
          </el-form-item>
      </el-form>
      <!--表格-->  
       <el-row>
            <el-table :data="tableData"  row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                        <el-table-column prop="privilegeName" label="權(quán)限名稱" >
                        </el-table-column>
                        <el-table-column prop="privilegeCode" label="權(quán)限編碼" >
                        </el-table-column>
                        <el-table-column prop="privilegeType" label="權(quán)限類別" :formatter="formatPrivilegeType" >
                        </el-table-column>
 
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                
                                <el-button type="primary" size="mini" @click="toAdd(scope)">新增</el-button>
                                <el-button type="primary" size="mini" @click="toEdit(scope)">編輯</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <br>
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.pageIndex"
                        :page-sizes="[5, 10, 20, 30, 40]"
                        :page-size=pagination.pageSize
                        layout="total, prev, pager, next"
                        :total=pagination.total>
                    </el-pagination>
        </el-row>
 
 
  </div>
</template>
 
<script>
export default{
    name: 'privilege-manager',
    data () {
     return {
        tableData: [],
        dialogFormEdit: false,
        dialogFormAdd:false,
        privilege: {
          id: '',
          privilegeName: '',
          privilegeCode: '',
          privilegeType: '',
          pid: '0'
        },
        pagination: {
            pageIndex: 1,
            pageSize: 10,
            total: 0,
        }
      }
    },
    methods:{
         init () {
        var self = this
         this.$axios({
            method:'post',
            url:'/api/baoan/privilege/getPage',
            data:{"page":this.pagination.pageIndex,"limit":this.pagination.pageSize, "pid": this.privilege.pid},
            headers:{
                'Content-Type':'application/json;charset=utf-8'      //改這里就好了
          }
        }).then(res => {
           console.log(res);
           self.pagination.total = res.data.datas.data.total;
           self.tableData = res.data.datas.data.records;
        })
          .catch(function (error) {
            console.log(error)
          })
        },
        handleSizeChange(val) {
                console.log(`每頁 ${val} 條`);
                this.pagination.pageSize = val;
                this.pagination.pageIndex = 1;
                this.init();
        },
        handleCurrentChange(val) {
                 console.log(`當(dāng)前頁: ${val}`);
                this.pagination.pageIndex = val;
                this.init();
        },
        // 權(quán)限類別轉(zhuǎn)換
        formatPrivilegeType: function( row, column) {
                 if(row.privilegeType === '1'){
                     return '目錄'
                 } else if(row.privilegeType === '2') {
                     return '菜單'
                 } else if (row.privilegeType === '3') {
                     return '按鈕'
                 } else {
                     return ''
                 }
        }
    },
    mounted: function () {
      this.init()
  }
 
 
}
</script>

總結(jié):

一、注意需要在前端表格里面改的是:

vue中Element-ui表格怎么實現(xiàn)樹形結(jié)構(gòu)表格

二、后端主要改的是:

(1)視圖層里面加入視圖層集合屬性,注意一定要命名為children,這樣前端才能渲染成樹型結(jié)構(gòu)。

vue中Element-ui表格怎么實現(xiàn)樹形結(jié)構(gòu)表格

以上是“vue中Element-ui表格怎么實現(xiàn)樹形結(jié)構(gòu)表格”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI