首页 > 编程语言 > 详细

vue 循环展示双层数组 ,及给点击当前的元素加样式

时间:2019-10-29 12:56:51      阅读:190      评论:0      收藏:0      [点我收藏+]

今天美工给了个图,要实现这样的功能。

技术分享图片

 

 后台返来的数据结构,是这样的

技术分享图片

 

 试了几次,我是这样实现的。

1、html

<table>
          <thead>
            <tr>
              <td>序号</td>
              <td>挂单号</td>
            </tr>
          </thead>
          <tbody id="tblList">
            <tr v-for="(item,index) in dataarr" :class="index==curindex? ‘active‘:‘‘">
              <td>{{index+1}}</td>
              <td @click="changeorder(item,index)">{{item.orderCode}}</td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="fl spinfo">
        <table>
          <thead>
            <tr>
              <td>序号</td>
              <td>商品编号</td>
              <td>商品名称</td>
              <td>数量</td>
              <td>售价</td>
              <td>金额</td>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(list,index) in splist[curindex]">
              <td>{{index+1}}</td>
              <td>{{list.productId}}</td>
              <td>{{list.productname}}</td>
              <td>{{list.salequantity}}</td>
              <td>{{list.saleprice}}</td>
              <td>{{list.subtotalamountsale}}</td>
            </tr>
          </tbody>
        </table>

2、js

export default {
  props: ["arr"],//从父组件传来的数据
  data() {
    return {
      dataarr: this.arr,
      curindex: 0,
      splist: []
    };
  },
  mounted() {
    this.getsplist();
  },
  methods: {
    getsplist() {
      for (let i = 0; i < this.arr.length; i++) {
        this.splist.push(this.arr[i].orderGoods);
      }
      return splist;//把数组里面的数组循环了出来,和外面的数组通过curindex 联系
    },
    changeorder(item, index) {
      this.curindex = index;
    }
  }
};

后经过同事指点,不必如此麻烦,可以直接这样。

1、html

   <table>
          <thead>
            <tr>
              <td>序号</td>
              <td>挂单号</td>
            </tr>
          </thead>
          <tbody id="tblList">
            <tr v-for="(item,index) in dataarr" :class="index==curindex? ‘active‘:‘‘">
              <td>{{index+1}}</td>
              <td @click="getsplist(item,index)">{{item.orderCode}}</td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="fl spinfo">
        <table>
          <thead>
            <tr>
              <td>序号</td>
              <td>商品编号</td>
              <td>商品名称</td>
              <td>数量</td>
              <td>售价</td>
              <td>金额</td>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(list,index) in splist">
              <td>{{index+1}}</td>
              <td>{{list.productId}}</td>
              <td>{{list.productname}}</td>
              <td>{{list.salequantity}}</td>
              <td>{{list.saleprice}}</td>
              <td>{{list.subtotalamountsale}}</td>
            </tr>
          </tbody>
        </table>

2、js

export default {
  name: "getOrder",
  props: ["arr"],
  data() {
    return {
      dataarr: this.arr,
      curindex: 0,
      splist: this.arr[0].orderGoods//默认是第一个的商品信息
    };
  },
  methods: {
     getsplist(item,index) {
       this.curindex = index;//添加点击的样式用
       this.splist=item.orderGoods//当前数组是循环的那个商品信息
     },
  }
};

 

vue 循环展示双层数组 ,及给点击当前的元素加样式

原文:https://www.cnblogs.com/zhoujuan/p/11757272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!