vue2.0基於vue-cli,element-ui餓了麼vue前端開源專案製作vue的樹形table,treeTable
阿新 • • 發佈:2019-01-24
<template> <el-table :data="data" border style="width: 100%" :row-style="showTr"> <el-table-column v-for="(column, index) in columns" :key="column.dataIndex" :label="column.text"> <template scope="scope"> <span v-if="spaceIconShow(index)" v-for="(space, levelIndex) in scope.row._level" class="ms-tree-space"></span> <button class="button is-outlined is-primary is-small" v-if="toggleIconShow(index,scope.row)" @click="toggle(scope.$index)"> <i v-if="!scope.row._expanded" class="el-icon-caret-right" aria-hidden="true"></i> <i v-if="scope.row._expanded" class="el-icon-caret-bottom" aria-hidden="true"></i> </button> <span v-else-if="index===0" class="ms-tree-space"></span> {{scope.row[column.dataIndex]}} </template> </el-table-column> <el-table-column label="操作" v-if="treeType === 'normal'" width="260"> <template scope="scope"> <button type="button" class="el-button el-button--default el-button--small"> <router-link :to="{ path: requestUrl + 'edit', query: {id: scope.row.Oid} }" tag="span"> 編輯 </router-link> </button> <el-button size="small" type="danger" @click="handleDelete()"> 刪除 </el-button> <button type="button" class="el-button el-button--success el-button--small"> <router-link :to="{ path: requestUrl, query: {parentId: scope.row.parentOId} }" tag="span"> 新增下級樹結構 </router-link> </button> </template> </el-table-column> </el-table> </template> <script> import Utils from '../utils/index.js' // import Vue from 'vue' export default { name: 'tree-grid', props: { // 該屬性是確認父元件傳過來的資料是否已經是樹形結構了,如果是,則不需要進行樹形格式化 treeStructure: { type: Boolean, default: function () { return false } }, // 這是相應的欄位展示 columns: { type: Array, default: function () { return [] } }, // 這是資料來源 dataSource: { type: Array, default: function () { return [] } }, // 這個作用是根據自己需求來的,比如在操作中涉及相關按鈕編輯,刪除等,需要向服務端傳送請求,則可以把url傳過來 requestUrl: { type: String, default: function () { return '' } }, // 這個是是否展示操作列 treeType: { type: String, default: function () { return 'normal' } }, // 是否預設展開所有樹 defaultExpandAll: { type: Boolean, default: function () { return false } } }, data () { return {} }, computed: { // 格式化資料來源 data: function () { let me = this if (me.treeStructure) { let data = Utils.MSDataTransfer.treeToArray(me.dataSource, null, null, me.defaultExpandAll) console.log(data) return data } return me.dataSource } }, methods: { // 顯示行 showTr: function (row, index) { let show = (row._parent ? (row._parent._expanded && row._parent._show) : true) row._show = show return show ? '' : 'display:none;' }, // 展開下級 toggle: function (trIndex) { let me = this let record = me.data[trIndex] record._expanded = !record._expanded }, // 顯示層級關係的空格和圖示 spaceIconShow (index) { let me = this if (me.treeStructure && index === 0) { return true } return false }, // 點選展開和關閉的時候,圖示的切換 toggleIconShow (index, record) { let me = this if (me.treeStructure && index === 0 && record.children && record.children.length > 0) { return true } return false }, handleDelete () { this.$confirm('此操作將永久刪除該記錄, 是否繼續?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'error' }).then(() => { this.$message({ type: 'success', message: '刪除成功!' }) }).catch(() => { this.$message({ type: 'info', message: '已取消刪除' }) }) } } } </script> <style scoped> .ms-tree-space{position: relative; top: 1px; display: inline-block; font-family: 'Glyphicons Halflings'; font-style: normal; font-weight: 400; line-height: 1; width: 18px; height: 14px;} .ms-tree-space::before{content: ""} table td{ line-height: 26px; } </style>
index.js