1. 程式人生 > 其它 >Element-ui Table tree 結構使用(解決無展開箭頭)

Element-ui Table tree 結構使用(解決無展開箭頭)

這兩天在寫後臺管理的頁面,需要使用到 Table ,而且會有下級。就想到了使用 Element table tree 結構。

使用過程中,一直不顯示展開箭頭,摸索除錯了大半天,在這裡特別記錄下。

在 Table 基礎上使用是比較簡單的,直接加上幾個對應的屬性繫結就可以,程式碼如下:

    <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" border>
      <el-table-column prop="date" label="日期" sortable width="180" />
      <el-table-column prop="name" label="姓名" sortable width="180" />
      <el-table-column prop="address" label="地址" />
    </el-table>

export 
default { data() { return { tableData: [{ id: 1, date: '2016-05-02', name: '王小虎', address: '上海市普陀區金沙江路 1518 弄' }, { id: 2, date: '2016-05-04', name: '王小虎', address: '上海市普陀區金沙江路 1517 弄' }, { id: 3, date:
'2016-05-01', name: '王小虎', address: '上海市普陀區金沙江路 1519 弄', children: [{ id: 31, date: '2016-05-01', name: '王小虎', address: '上海市普陀區金沙江路 1519 弄' }, { id: 32, date: '2016-05-01', name: '王小虎', address: '上海市普陀區金沙江路 1519 弄' }] }, { id:
4, date: '2016-05-03', name: '王小虎', address: '上海市普陀區金沙江路 1516 弄' }] } } }

上面這個是正常的使用。

官方示例中,有::tree-props="{children: 'children', hasChildren: 'hasChildren'}" 繫結的屬性。

所以下面幾點

注意:

1. 必須有 row-key="id",否則不會顯示展開箭頭;

2. tree-props 中的 children 可以對映為自己資料中的欄位,如果一致都可以省略

3. 不是懶載入情況下,不需要 hasChildren(繫結的 table 資料中不能有,否則不能顯示)【我自己就栽在這一條】

4. 不是懶載入下,children 欄位一致, tree-props 可以省略,只需要 row-key 即可

Table 原始碼:

從原始碼中看,treeProps 是有預設值的,一樣時不用傳,rowKey 沒有所以一定需要傳。