iView Table 可展开 on-expand="onExpand" 实例使用(完美版)

本人代码可以直接在https://run.iviewui.com/中运行,

两处点击处理效果一样, 完善操作逻辑


<!--
 * @Author: FGJ
 * @LastEditors: Do not edit
 * @Date: 2018-12-28 00:31:30
 * @LastEditTime: 2019-04-16 15:30:08
 * @Description: iview
 -->
<template>
    <section class="main">
       
            <Table stripe highlight-row  
                   :columns="tableColumns1" 
                   :data="tableData" 
                     
                   size="small" 
                   ref="table"     
                   @on-expand="onExpand"  
                   @on-select="onSelect"
                   @on-selection-change='selectionClick'
                  >
            </Table>        
         
       
    </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  },
                                // on:    {  click: () => { this.expand(params.row, params.index)} }
                                
                            })
                        }
                    },
                    {
                        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元",
                       stopStation:[
                            {stopStation:"东直门",id:11},
                            {stopStation:"雍和宫",id:22}, 
                            {stopStation:"安定门",id:33},
                            {stopStation:"西直门",id:33},
                            {stopStation:"复兴门",id:33},
                            {stopStation:"建国门",id:33}
                       ]
                                                            
                    },
                    {  
                       id:13, 
                       lineName:"公主坟线",
                       lineDirection:"机场-市区",
                       startStation:"T1",
                       stopStation:"all",
                       endStation:"公主坟",
                       lineType:"市区",
                       mileage:"100Km",
                       operateDepart:"PEK运营一部",
                       price:"20元" ,
                       stopStation:[
                            {stopStation:"东直门",id:11},
                            {stopStation:"西直门",id:22}, 
                            {stopStation:"建国门",id:33}
                       ]                            
                    },
                     {  
                       id:14, 
                       lineName:"公主坟线",
                       lineDirection:"机场-市区",
                       startStation:"T1",
                       stopStation:"all",
                       endStation:"公主坟",
                       lineType:"市区",
                       mileage:"100Km",
                       operateDepart:"PEK运营一部",
                       price:"20元" ,
                       stopStation:[
                            {stopStation:"东直门",id:11},
                            {stopStation:"西直门",id:22}, 
                            {stopStation:"建国门",id:33}
                       ]                         
                    },
                     {  
                       id:15, 
                       lineName:"公主坟线",
                       lineDirection:"机场-市区",
                       startStation:"T1",
                       stopStation:"all",
                       endStation:"公主坟",
                       lineType:"市区",
                       mileage:"100Km",
                       operateDepart:"PEK运营一部",
                       price:"20元" ,
                       stopStation:[
                            {stopStation:"东直门",id:11},
                            {stopStation:"西直门",id:22}, 
                            {stopStation:"建国门",id:33}
                       ]                      
                    },
                    {  
                       id:16, 
                       lineName:"公主坟线",
                       lineDirection:"机场-市区",
                       startStation:"T1",
                       stopStation:"all",
                       endStation:"公主坟",
                       lineType:"市区",
                       mileage:"100Km",
                       operateDepart:"PEK运营一部",
                       price:"20元" ,
                       stopStation:[
                            {stopStation:"东直门",id:11},
                            {stopStation:"西直门",id:22}, 
                            {stopStation:"建国门",id:33}
                       ]
                           
                    }                       
                    
                ]
               
               
            }
        },  
        created(){
          
        }, 
        watch:{

        }, 
        mounted () {          
            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); 
                // 展开点击项,展开之前恢复到初始状态
                this.tableData.sort() //更新视图
                for (let i = 0; i <  this.tableData.length; i++) {
                    this.tableData[i]._expanded = false;   
                    this.tableData[i]._text = "查看所有经停站"                       
                } 
                //点击展开
                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);  
                // console.log(this.tableData);
                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;   //其他行关闭 
                            item._text = "查看所有经停站"                          
                        }
                        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;   //其他行关闭    
                            item._text = "查看所有经停站"                       
                        }                      
                        return item; 
                    });
                       
                }
            },
          
        }
    }
</script>
<style scoped>
   
</style>
// table-expand.vue
<style scoped>
    .expand-row{
        margin-bottom: 16px;
    }
</style>
<template>
    <div>     
        <Row>
            <Col>
                <Steps :current="10">
                    <Step 
                        v-for="item in stopStationList" 
                        :title="item.stopStation" 
                        icon="ios-subway" 
                        :content="item._index"
                        :value="item.id" 
                        :key="item.id"
                    ></Step> 
                </Steps>
            </Col>
        </Row>
      
    </div>
</template>
<script>
    export default {
        props: {
            row: Object
        },
             
        data () {
            return {
                valueRow:this.row.stopStation,
                stopStationList:[]
            }
        },
        //  stopStation:[
        //     {stopStation:"东直门",id:11},
        //     {stopStation:"西直门",id:22}, 
        //     {stopStation:"建国门",id:33}
        // ]    
        created(){
        //    console.log(this.valueRow);
        //    this.stopStationList = this.row.stopStation
        //   给数据添加index
           this.stopStationList = this.row.stopStation.map((item,index) => {
                item._index = "经停站"+(index+1)              
                return item
            })
          
        }, 
        methods: {

        }
    };
</script>

 

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值