element-ui中table表格的折疊和隱藏方式
element-ui中table表格的折疊和隱藏
el-table的折疊展開
使用elment-ui的el-table組件做表格的折疊和展開 使用 default-expand-all屬性 通過click控制它為true或false來完成折疊展開 但是卻沒有效果?。。?/p>
通過查資料發(fā)現(xiàn)需要這樣來寫
<el-table v-if="refreshTable" :header-cell-style="{background:'#f4f3f9',color:'#606266'}" :data="deptList" style="width: 100%;margin-bottom: 20px;" row-key="id" :default-expand-all="Expand" :tree-props="{children: 'children', hasChildren: 'hasChildren' > -------------數(shù)據(jù)設(shè)置 data() { return { Expand: true, refreshTable: true, } } -------------鼠標點擊事件 /** 展開/折疊操作 */ toggleExpandAll() { this.refreshTable = false this.Expand = !this.Expand this.$nextTick(() => { this.refreshTable = true }) },
設(shè)置v-if table的顯示和隱藏 設(shè)置default-expand-all 兩個都要設(shè)置 向上面那樣 最后成功了 也不知道具體 原理 先這樣記住!
最近發(fā)現(xiàn)之前那個方法有bug 很不好用 又找到一個簡單好用得
this.$refs.table..toggleRowExpansion(this.tableData[0]) // 全部折疊
直接拿到table得dom元素, 然后調(diào)用element-ui的table表格得一個方法toggleRowExpansion就可以實現(xiàn)展開和折疊了
實現(xiàn)element-ui表格el-table展開行
<template> <el-table :data="tableData" style="width: 100%" @row-click="rowClick" :row-key='getRowKeys' :expand-row-keys="expands" @expand-change="expandChange" ref="tableList"> <el-table-column type="expand"> <template slot-scope="props"> <el-form label-position="right" inline class="demo-table-expand" align="right"> <el-form-item label="商品名稱"> <span>{{ props.row.name }}</span> </el-form-item> <el-form-item label="所屬店鋪"> <span>{{ props.row.shop }}</span> </el-form-item> <el-form-item label="商品 ID"> <span>{{ props.row.id }}</span> </el-form-item> <el-form-item label="店鋪 ID"> <span>{{ props.row.shopId }}</span> </el-form-item> <el-form-item label="商品分類"> <span>{{ props.row.category }}</span> </el-form-item> <el-form-item label="店鋪地址"> <span>{{ props.row.address }}</span> </el-form-item> <el-form-item label="商品描述"> <span>{{ props.row.desc }}</span> </el-form-item> </el-form> </template> </el-table-column> <el-table-column label="商品 ID" prop="id"></el-table-column> <el-table-column label="商品名稱" prop="name"></el-table-column> <el-table-column label="描述" prop="desc"></el-table-column> </el-table> </template>
<script> export default { data() { return { tableData: [ { id: "12987122", name: "好滋好味雞蛋仔", category: "江浙小吃、小吃零食", desc: "荷蘭優(yōu)質(zhì)淡奶,奶香濃而不膩", address: "上海市普陀區(qū)真北路", shop: "王小虎夫妻店"+'<br>'+"asfdasfafasdfasdfas", shopId: "10333", }, { id: "12987123", name: "好滋好味雞蛋仔", category: "江浙小吃、小吃零食", desc: "荷蘭優(yōu)質(zhì)淡奶,奶香濃而不膩", address: "上海市普陀區(qū)真北路", shop: "王小虎夫妻店", shopId: "10333", }, { id: "12987125", name: "好滋好味雞蛋仔", category: "江浙小吃、小吃零食", desc: "荷蘭優(yōu)質(zhì)淡奶,奶香濃而不膩", address: "上海市普陀區(qū)真北路", shop: "王小虎夫妻店", shopId: "10333", }, { id: "12987126", name: "好滋好味雞蛋仔", category: "江浙小吃、小吃零食", desc: "荷蘭優(yōu)質(zhì)淡奶,奶香濃而不膩", address: "上海市普陀區(qū)真北路", shop: "王小虎夫妻店", shopId: "10333", }, ], expands: [] }; }, methods: { rowClick(row){//點擊行時展開收起 this.$refs.tableList.toggleRowExpansion(row); }, expandChange(row,expandedRows){// let that = this; if(expandedRows.length){//此時展開 that.expands = []; if(row){ that.expands.push(row.id); } }else{//折疊 that.expands = []; } }, getRowKeys(row){//行數(shù)據(jù)的key,用于優(yōu)化table的渲染 return row.id; } } }; </script>
<style scoped> .demo-table-expand { font-size: 0; } .demo-table-expand .el-form-item { margin-right: 0; margin-bottom: 0; width: 50%; } </style><style>//在添加scoped時未生效.demo-table-expand .label{ width:150px; color: #99a9bf;} </style>
row-click
: 當某一行點擊時觸發(fā)row-key
: 行數(shù)據(jù)的key,用于優(yōu)化talbe的渲染expand-row-keys
: 可通過這個屬性設(shè)置表格的當前展開行(必需row-key屬性)expand-change
: 用于某一行點擊展開或者關(guān)閉時觸發(fā)(展開行時,第二個屬性為expandedRows,關(guān)閉行時,第二個屬性為expanded)
后續(xù)問題
列表中存在操作按鈕列,點擊操作按鈕時,同時會觸發(fā)row-click
問題解決
<el-button type="warning" size="mini" circle icon="el-icon-question" @click.stop="diagnose"></el-button>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-router跳轉(zhuǎn)時打開新頁面的兩種方法
這篇文章主要給大家介紹了關(guān)于vue-router跳轉(zhuǎn)時打開新頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用vue-router具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-07-07vue router 跳轉(zhuǎn)時打開新頁面的示例方法
這篇文章主要介紹了vue router 跳轉(zhuǎn)時打開新頁面的示例方法,本文通過示例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07使用Pinia Persistedstate插件實現(xiàn)狀態(tài)持久化的操作方法
Pinia 作為 Vue 的新一代狀態(tài)管理工具,以其輕量化和易用性受到開發(fā)者的喜愛,然而,Pinia 默認使用內(nèi)存存儲狀態(tài),為了解決這個問題,我們可以借助 Pinia Persistedstate 插件來實現(xiàn)狀態(tài)的持久化存儲,本文將詳細介紹該插件的使用方法,需要的朋友可以參考下2024-11-11詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳)
這篇文章主要介紹了詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04Vue結(jié)合后臺導(dǎo)入導(dǎo)出Excel問題詳解
這篇文章主要介紹了Vue結(jié)合后臺導(dǎo)入導(dǎo)出Excel問題詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02C#實現(xiàn)將一個字符轉(zhuǎn)換為整數(shù)
下面小編就為大家分享一篇C#實現(xiàn)將一個字符轉(zhuǎn)換為整數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12