|
| 1 | +<template> |
| 2 | + <div id="app"> |
| 3 | + <div class="fixed-button"> |
| 4 | + <el-button type="infor" @click="getData()">this.getData()</el-button> |
| 5 | + </div> |
| 6 | + <desc-pannel |
| 7 | + title="table-tree" |
| 8 | + desc="推荐使用" |
| 9 | + :good="[`columns 完全定制化`, `完全重写 el-table`]" |
| 10 | + :bad="[`需要学习额外的 TableStore API`]" |
| 11 | + > |
| 12 | + <table-tree |
| 13 | + v-loading="tableLoading" |
| 14 | + class="tree-table-view" |
| 15 | + :row-key="rowKey" |
| 16 | + :show-header="true" |
| 17 | + :data-source="data" |
| 18 | + :columns="columns1" |
| 19 | + :indent-size="20" |
| 20 | + :expand="true" |
| 21 | + :depth="0" |
| 22 | + :expand-depth="1" |
| 23 | + :expand-row-map.sync="expandRowMap" |
| 24 | + /> |
| 25 | + </desc-pannel> |
| 26 | + |
| 27 | + <desc-pannel |
| 28 | + title="table-tree-depth" |
| 29 | + desc="Use element-ui `Table` component and render itself recursively." |
| 30 | + :good="[`递归扩展,代码量少,可定制性高`, `支持自定义扩展、渲染深度,自定义渲染 table-column 'render'`]" |
| 31 | + > |
| 32 | + <table-tree-depth |
| 33 | + v-loading="tableLoading" |
| 34 | + class="tree-table-view" |
| 35 | + :row-key="rowKey" |
| 36 | + :show-header="true" |
| 37 | + :data-source="data" |
| 38 | + :columns="columns2" |
| 39 | + :expand="true" |
| 40 | + :depth="0" |
| 41 | + :expand-depth="1" |
| 42 | + :expand-row-map.sync="expandRowMap" |
| 43 | + /> |
| 44 | + </desc-pannel> |
| 45 | + |
| 46 | + <TreeView :label="tree.label" :nodes="tree.nodes" :depth="0"/> |
| 47 | + |
| 48 | + <desc-pannel |
| 49 | + title="table-tree-recursive" |
| 50 | + desc="Use element-ui `Table` component and write itself recursively." |
| 51 | + :good="[`可定制性高`, `table-column type可根据不同深度定制` ]" |
| 52 | + :bad="[`非递归扩展,代码量多`, `table-column 不支持自定义 cell render`]" |
| 53 | + > |
| 54 | + <table-tree-recursive |
| 55 | + class="tree-table-view" |
| 56 | + :data-source="data" |
| 57 | + :show-header="true" |
| 58 | + :columns="columns2" |
| 59 | + :expand="true" |
| 60 | + :depth="0" |
| 61 | + > |
| 62 | + <template slot-scope="{ scope, children, depth }"> |
| 63 | + <table-tree-recursive |
| 64 | + class="tree-table-view" |
| 65 | + :data-source="children" |
| 66 | + :show-header="false" |
| 67 | + :columns="columns2" |
| 68 | + :depth="depth" |
| 69 | + :expand="Boolean(children && children.length)" |
| 70 | + :selection="Boolean(!children || !children.length)" |
| 71 | + > |
| 72 | + <template slot-scope="{ scope, children, depth }"> |
| 73 | + <table-tree-recursive |
| 74 | + class="tree-table-view" |
| 75 | + :data-source="children" |
| 76 | + :show-header="false" |
| 77 | + :columns="columns2" |
| 78 | + :depth="depth" |
| 79 | + :expand="false" |
| 80 | + :selection="true" |
| 81 | + /> |
| 82 | + </template> |
| 83 | + </table-tree-recursive> |
| 84 | + <!-- <div v-else class="cell">no any data.</div> --> |
| 85 | + </template> |
| 86 | + </table-tree-recursive> |
| 87 | + </desc-pannel> |
| 88 | + </div> |
| 89 | +</template> |
| 90 | + |
| 91 | +<script> |
| 92 | +import dataSource, { TableData2, tree } from "./TreeData"; |
| 93 | +import DescPannel from "./components/DescPannel"; |
| 94 | +
|
| 95 | +import TableTreeRecursive from "../src/components/TableTreeRecursive"; |
| 96 | +import TableTreeDepth from "../src/components/TableTreeDepth"; |
| 97 | +import TreeView from "@/components/TreeViewDemo"; |
| 98 | +import TableTree from "@/components/TableTree"; |
| 99 | +
|
| 100 | +export default { |
| 101 | + name: "App", |
| 102 | + components: { |
| 103 | + TreeView, |
| 104 | + TableTree, |
| 105 | + DescPannel, |
| 106 | + TableTreeDepth, |
| 107 | + TableTreeRecursive |
| 108 | + }, |
| 109 | + data() { |
| 110 | + return { |
| 111 | + rowKey: "id", |
| 112 | + expandRowMap: {}, |
| 113 | + /* expandRowMap: { |
| 114 | + 0: [1111], |
| 115 | + 1: [11111], |
| 116 | + }, */ |
| 117 | + tableLoading: false, |
| 118 | + data: dataSource, |
| 119 | + columns1: [ |
| 120 | + { |
| 121 | + prop: 'index', |
| 122 | + key: 0, |
| 123 | + minWidth: 20, |
| 124 | + render(h, { rowIndex }) { |
| 125 | + return ++rowIndex |
| 126 | + } |
| 127 | + }, |
| 128 | + { |
| 129 | + prop: "_expand", |
| 130 | + key: 0, |
| 131 | + width: 20, |
| 132 | + minWidth: 40, |
| 133 | + render: (h, { store, row, /* column, */ depth }) => { |
| 134 | + if (row.children && row.children.length) { |
| 135 | + const expanded = store.isRowExpanded(row); |
| 136 | + const classname = expanded ? "el-icon-minus" : "el-icon-plus"; |
| 137 | +
|
| 138 | + return ( |
| 139 | + <i |
| 140 | + style={`padding-left: ${depth * 20}px`} |
| 141 | + class={classname} |
| 142 | + onClick={() => store.toggleRowExpansion(row)} |
| 143 | + /> |
| 144 | + ); |
| 145 | + } else { |
| 146 | + return <el-checkbox style={`padding-left: ${depth * 20}px`} />; |
| 147 | + } |
| 148 | + } |
| 149 | + }, |
| 150 | + { |
| 151 | + label: "商品 ID", |
| 152 | + prop: "id", |
| 153 | + key: 1 |
| 154 | + }, |
| 155 | + { |
| 156 | + label: "商品名称", |
| 157 | + prop: "name", |
| 158 | + key: 2 |
| 159 | + }, |
| 160 | + { |
| 161 | + label: "描述", |
| 162 | + prop: "desc", |
| 163 | + key: 3 |
| 164 | + } |
| 165 | + ], |
| 166 | + columns2: [ |
| 167 | + { |
| 168 | + label: "商品 ID", |
| 169 | + prop: "id", |
| 170 | + key: 1, |
| 171 | + width: 40, |
| 172 | + }, |
| 173 | + { |
| 174 | + label: "商品名称", |
| 175 | + prop: "name", |
| 176 | + key: 2 |
| 177 | + }, |
| 178 | + { |
| 179 | + label: "描述", |
| 180 | + prop: "desc", |
| 181 | + key: 3 |
| 182 | + }, |
| 183 | + { |
| 184 | + label: "operation", |
| 185 | + prop: "_operation", |
| 186 | + width: 300, |
| 187 | + key: 4, |
| 188 | + render: (h, { row, depth, expandDepth }) => |
| 189 | + depth > expandDepth && |
| 190 | + h( |
| 191 | + "el-button", |
| 192 | + { |
| 193 | + nativeOn: { |
| 194 | + click: () => { |
| 195 | + const content = prompt( |
| 196 | + "What's the description u wanna say?", |
| 197 | + row["desc"] |
| 198 | + ); |
| 199 | +
|
| 200 | + if (content.trim()) { |
| 201 | + row.desc = content.trim(); |
| 202 | + // this.getData() |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + }, |
| 207 | + ["click to modify desc"] |
| 208 | + ) |
| 209 | + } |
| 210 | + ], |
| 211 | + tree |
| 212 | + }; |
| 213 | + }, |
| 214 | + beforeMount() { |
| 215 | + // this.getData() |
| 216 | + }, |
| 217 | + mounted() { |
| 218 | + // this.getData() |
| 219 | + }, |
| 220 | + methods: { |
| 221 | + getData() { |
| 222 | + this.tableLoading = true; |
| 223 | +
|
| 224 | + setTimeout(() => { |
| 225 | + this.tableLoading = false; |
| 226 | + this.data = this.data === TableData2 ? dataSource : TableData2; |
| 227 | + }, 1000); |
| 228 | + } |
| 229 | + } |
| 230 | +}; |
| 231 | +</script> |
| 232 | + |
| 233 | +<style> |
| 234 | +#app { |
| 235 | + font-family: "Avenir", Helvetica, Arial, sans-serif; |
| 236 | + -webkit-font-smoothing: antialiased; |
| 237 | + -moz-osx-font-smoothing: grayscale; |
| 238 | + color: #2c3e50; |
| 239 | +} |
| 240 | +
|
| 241 | +.tree-table-view .el-table__expanded-cell[class*="cell"] { |
| 242 | + padding: 0 0 0 5px; |
| 243 | +} |
| 244 | +
|
| 245 | +.fixed-button { |
| 246 | + position: fixed; |
| 247 | + bottom: 20px; |
| 248 | + right: 20px; |
| 249 | + z-index: 100; |
| 250 | +} |
| 251 | +</style> |
0 commit comments