iView Table 可展开 @on-expand="onExpand" 实例使用(一)

2 篇文章 0 订阅


<!--
 * @Author: FGJ
 * @LastEditors: Do not edit
 * @Date: 2018-12-28 00:31:30
 * @LastEditTime: 2019-04-16 10:05:43
 * @Description: iview
 -->
<template>
    <section class="main">       
        <div class="container">            
            <Table stripe highlight-row  
                   :columns="tableColumns1" 
                   :data="tableData" 
                   size="small" 
                   ref="table"     
                   @on-expand="onExpand"  
                   @on-select="onSelect"
                   @on-selection-change='selectionClick'
                  >
            </Table> 
        </div>
       
    </section>
</template>

<script>    
    import expandRow from './table-expand.vue';
    export default {   
        components: { expandRow }, 
        data () {
            return {              
                tableData:[], 
                //行数据标题
                tableColumns1:[
                    {
                        type: 'expand',
                        width: 50,
                        render: (h, params) => {
                            return h(expandRow, {
                                props: {  row: params.row  }
                            })
                        }
                    },
                    {
                        title: '线路名称',
                        key: 'lineName'
                    },
                    {
                        title: '线路方向',
                        key: 'lineDirection'
                    },
                    {
                        title: '始发站',
                        key: 'startStation'
                    },
                    {
                        title: '经停站',
                        key: '_text',    
                        width:120,
                        align: 'center',
                        render: (h, params) => {   
                            return h("span", {
                                props: {  row: params.row  },
                                style: {  color: 'tomato'} ,         
                                on:    {  click: () => { this.expand(params.row, params.index)} }                                
                            },params.row._text)
                        }
                    },
                    {
                        title: '终点站',
                        key: 'endStation'
                    },
                    {
                        title: '线路类型',
                        key: 'lineType'
                    },
                    {
                        title: '运营里程',
                        key: 'mileage'
                    },
                    {
                        title: '线路运营单位',
                        key: 'operateDepart'
                    },
                    {
                        title: '票价',
                        key: 'price'
                    },
                ],
               
                // 数据源
                tableData1: [
                    {   
                       id:12,
                       lineName:"公主坟线",
                       lineDirection:"机场-市区",
                       startStation:"T1",
                       stopStation:"all",
                       endStation:"公主坟",
                       lineType:"市区",
                       mileage:"100Km",
                       operateDepart:"PEK运营一部",
                       price:"20元",
                       stopStation1:"东直门",
                       stopStation2:"西直门",
                       stopStation3:"复兴门"                                       
                    },
                    {  
                       id:13, 
                       lineName:"公主坟线",
                       lineDirection:"机场-市区",
                       startStation:"T1",
                       stopStation:"all",
                       endStation:"公主坟",
                       lineType:"市区",
                       mileage:"100Km",
                       operateDepart:"PEK运营一部",
                       price:"20元" ,
                       stopStation1:"东直门",
                       stopStation2:"西直门",
                       stopStation3:"复兴门"                              
                    }
                ]
               
               
            }
        },  
        created(){
         
        }, 
        watch:{

        }, 
        mounted () {     
            // 给tableData1  增加_expanded/_text  
            this.tableData = this.tableData1.map(item => {
                item._expanded = false
                item._text = "查看所有经停站"
                return item
            })
            console.log(this.tableData);
            

        },  
        methods: {
            //点击展开收起
            expand(item,index){
                console.log(item,index);   
                if (item._expanded) {                   
                    // this.tableData.splice()  
                    // this.tableData.push()  
                    this.tableData.sort() //更新视图
                    this.tableData[index]._expanded = false
                    this.tableData[index]._text = "查看所有经停站"                        
                }else{
                    this.tableData.splice()  
                    this.tableData[index]._expanded = true
                    this.tableData[index]._text = "收起所有经停站" 
                }
                     
            },

            // 展开原生事件  点击左侧展收起         
            onExpand(row, status){
                // console.log(row,status);                
                if(status){
                    this.tableData1.splice()
                    this.tableData1.filter((item, index)=>{  
                        if(item.id == row.id){   
                            item._expanded = true;   //展开选中的行
                            // this.$set(item, "_text", "收起所有经停站")                           
                            item._text = "收起所有经停站"   
                        }else{
                            item._expanded = false;   //其他行关闭                           
                        }
                        return item; 
                    });                  
                    this.tableData = this.tableData1                
                }else{                
                    this.tableData.splice()   
                    this.tableData.map((item, index)=>{                                                    
                        if(item.id == row.id){   
                            item._expanded = false;   //展开选中的行
                            // this.$set(item, "_text", "查看所有经停站")                           
                            item._text = "查看所有经停站"   
                        }else{
                            item._expanded = false;   //其他行关闭                           
                        }                      
                        return item; 
                    });
                       
                }
            }
        }
           
    }
</script>
<style scoped>
   
</style>

 

// table-expand.vue
<style scoped>
    .expand-row{
        margin-bottom: 16px;
    }
</style>
<template>
    <div>
        <Row class="expand-row">
            <Col span="8">
                <span class="expand-key">经停站1: </span>
                <span class="expand-value">{{ row.stopStation1 }}</span>
            </Col>
            <Col span="8">
                <span class="expand-key">经停站2: </span>
                <span class="expand-value">{{ row.stopStation1 }}</span>
            </Col>
            <Col span="8">
                <span class="expand-key">经停站3: </span>
                <span class="expand-value">{{ row.stopStation1 }}</span>
            </Col>
        </Row>
      
    </div>
</template>
<script>
    export default {
        props: {
            row: Object
        }
    };
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值