1. 程式人生 > 其它 >Element樹形表格的簡單顯示

Element樹形表格的簡單顯示

一、在Element官網複製下來

二、使用後臺使用的遞迴

1、建立一個Dto

 public class MenuDtoModel
    {
        public int IMd { get; set; }
        public string MName { get; set; }
        public int ParentMenuId { get; set; }
        public int Level { get; set; }
        public string Url { get; set; }
        public int Type { get
; set; } public int OrderIndex { get; set; } public List<MenuDtoModel> ChildMenus; }

2、資料訪問層進行使用

public List<MenuDtoModel> AllShow()
        {
            using (var conn = new SqlConnection(_configuration.GetConnectionString("MSSQL")))
            {
                
var list = conn.Query<Menu>("select * from Menu").ToList(); return GetMenu(list); } } public List<MenuDtoModel> GetMenu(List<Menu> menus,int parenMenuId=0) { return menus.Where(p => p.Type == 1 && p.ParentMenuId == parenMenuId).Select(p => new
MenuDtoModel { IMd = p.IMd, MName = p.MName, ParentMenuId = parenMenuId, Level = p.Level, Url = p.Url, Type = p.Type, OrderIndex = p.OrderIndex, ChildMenus = GetMenu(menus, p.IMd) }).ToList(); }

三、前臺操作

主意使用ChildMenus

<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="IMd"
      border
      default-expand-all
      :tree-props="{ children: 'ChildMenus' }"
    >
      <el-table-column prop="MName" label="名稱"> </el-table-column>
      <el-table-column prop="Url" label="路徑"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="delterMenu(scope.row.IUd)">刪除</el-button>
          <el-button @click="GaiMenu(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
    };
  },
  methods: {
    getMenu() {
      this.$axios.get("http://localhost:39***/api/Menu/AllShow").then((res) => {
        this.tableData = res.data;
      });
    },
  },
  created() {
    this.getMenu();
  },
};
</script>

<style>
</style>

......待續