51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Element-Plus仅把获取到的数据中的二级数组渲染到表格

获取到一个数据中,嵌套着二级数据,此时,比如获取到分类(商品),此时仅需要渲染商品,而不需要分类,且他俩有部分共同的属性,没学多少,不知道有什么更高效的方法,所以我遍历了。

接口数据

{
  "code": 200,
  "data": [{
      "id": 1,
      "sortname": "数码电器",
      "sortimg": "2",
      "sortdes": "The digital electrical appliances",
      "addtime": "2022-10-30",
    },
    {
      "id": 2,
      "sortname": "家居家具",
      "sortimg": "2",
      "sortdes": "Household furniture",
      "addtime": "2022-10-30",
      "children": [{
          "id": 21,
          "fruit": "小米12",
          "pic": "/src/assets/images/1.jpg",
          "price": 2999,
          "adddate": "2022-10-30",
          "state": false
        },
        {
          "id": 22,
          "fruit": "苹果15",
          "pic": "/src/assets/images/1.jpg",
          "price": 5999,
          "adddate": "2022-10-30",
          "state": true
        }
      ]
    },
    {
      "id": 3,
      "sortname": "水果生鲜",
      "sortimg": "7",
      "sortdes": "Fresh fruit",
      "addtime": "2022-10-30",
      "children": [{
          "id": 31,
          "fruit": "青柠",
          "pic": "/src/assets/images/1.jpg",
          "price": 5,
          "adddate": "2022-10-30",
          "state": true
        },
        {
          "id": 32,
          "fruit": "柚子",
          "pic": "/src/assets/images/2.jpg",
          "price": 4.5,
          "adddate": "2022-10-30",
          "state": false
        },
        {
          "id": 33,
          "fruit": "橘子",
          "pic": "/src/assets/images/3.jpg",
          "price": 3,
          "adddate": "2022-10-30",
          "state": false
        },
        {
          "id": 36,
          "fruit": "柠檬",
          "pic": "/src/assets/images/6.jpg",
          "price": 4,
          "adddate": "2022-10-30",
          "state": false
        },
      ]
    },
    {
      "id": 4,
      "sortname": "美妆清洁",
      "sortimg": "12",
      "sortdes": "Beauty makeup clean",
      "addtime": "2022-10-30",
      "children": [{
          "id": 41,
          "fruit": "洗面奶",
          "pic": "/src/assets/images/1.jpg",
          "price": 9.9,
          "adddate": "2022-10-30",
          "state": true
        },
        {
          "id": 42,
          "fruit": "神仙水",
          "pic": "/src/assets/images/1.jpg",
          "price": 999,
          "adddate": "2022-10-30",
          "state": true
        }
      ]
    },
  ]
}

forEach遍历

      res.forEach((item, index) => {
        if (item.chilren !== null) {
          if (item.children === undefined) {
            return null;
          }
          let newarr = item.children;
          // console.log(newarr);
          arr.push(...newarr);
        } else {
          return false;
        }
        // console.log(arr);
        tableData.value = arr;

首先获取到包含二层数据的数组,并获取到二层数组,然后进行数组拼接,最后拿到一个新的仅包含所有二层数组值的新数组。

渲染表格

  <el-table :data="tableData">
    <el-table-column type="selection" />
    <el-table-column prop="id" label="id" />
    <el-table-column prop="fruit" label="商品名称" show-overflow-tooltip />
    <el-table-column prop="pic" label="缩略图">
      <template #default="scope"> <img :src="scope.row.pic" alt="" class="goodsimg" /> </template>
    </el-table-column>
    <el-table-column prop="price" label="商品价格">
      <template #default="scope"> {{ scope.row.price }} 元</template>
    </el-table-column>
    <el-table-column prop="adddate" label="添加时间"> </el-table-column>
    <el-table-column label="上架" prop="state">
      <template #default="scope"> <el-switch v-model="scope.row.state" /></template>
    </el-table-column>
    <el-table-column fixed="right" label="操作">
      <template #default="scope">
        <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

Element-Plus有一个树形数据来渲染嵌套数据,但是不是我想要的。

正在自学,如果路过的大佬有更好的方法请告知一下!

赞(0)
未经允许不得转载:工具盒子 » Element-Plus仅把获取到的数据中的二级数组渲染到表格