muse-ul中data-table表格如何获取勾选项对应的数据

这个框架的表格有个问题,他返回的不是当前选中项的数据而是角标。
所以我们的思路就是已知selects可以拿到所有选中的角标,那我们就循环一下角标然后那对应数据数组内的对应数据就行了。

代码

<template>
  <div>
    <mu-container>
      <!-- z-depth是阴影大小 -->
      <mu-paper :z-depth="1">
        <!-- 
      selectable:是否可选择
      select-all:是否开启全选选择框
      selects.sync="selects":选择的数组,sync用来双向控制
      checkbox:是否显示 mu-checkbox
      columns="columns":是否显示列的配置
     -->
        <mu-data-table
          selectable
          :selects.sync="selects"
          checkbox
          :columns="columns"
          :data="list"
        >
          <template slot-scope="scope">
            <td>{{ scope.row.name }}</td>
            <td class="is-right">{{ scope.row.calories }}</td>
            <td class="is-right">{{ scope.row.fat }}</td>
            <td class="is-right">{{ scope.row.carbs }}</td>
            <td class="is-right">{{ scope.row.protein }}</td>
            <td class="is-right">{{ scope.row.iron }}%</td>
          </template>
        </mu-data-table>
        <mu-flex align-items="center" style="padding: 8px" wrap="wrap">
          selects:
          <mu-chip
            delete
            v-for="selectIndex in selects"
            @delete="removeSelect(selectIndex)"
            style="margin: 8px"
            color="green"
            :key="selectIndex"
            >{{ list[selectIndex].name }}</mu-chip
          >
        </mu-flex>
      </mu-paper>
    </mu-container>
    <van-button @click="submit">提交选项</van-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      selects: [],
      selectsvalue: [],
      sort: {
        name: "",
        // order: 'asc'
      },
      //列的显示
      columns: [
        { title: "Dessert (100g serving)", width: 200, name: "name" },
        { title: "Calories", name: "calories", width: 126, align: "center" },
        { title: "Fat (g)", name: "fat", width: 126, align: "center" },
        { title: "Carbs (g)", name: "carbs", width: 126, align: "center" },
        { title: "Protein (g)", name: "protein", width: 126, align: "center" },
        { title: "Iron (%)", name: "iron", width: 126, align: "center" },
      ],
      //表格数据显示
      list: [
        {
          name: "Frozen Yogurt",
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: 1,
        },
        {
          name: "Ice cream sandwich",
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: 1,
        },
        {
          name: "Eclair",
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: 7,
        },
        {
          name: "Cupcake",
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: 8,
        },
        {
          name: "Gingerbread",
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          iron: 16,
        },
        {
          name: "Jelly bean",
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: 0.0,
          iron: 0,
        },
        {
          name: "Lollipop",
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          iron: 2,
        },
        {
          name: "Honeycomb",
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: 6.5,
          iron: 45,
        },
        {
          name: "Donut",
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: 4.9,
          iron: 22,
        },
        {
          name: "KitKat",
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: 7,
          iron: 6,
        },
      ],
      tableList: [],
    };
  },
  methods: {
    //按钮方法
    submit() {
      //每次保存先清空,否则会把上次保存的数据重复保存进去
      this.tableList = [];
      //遍历每一项selects。i当角标来拿list内的对应角标的数据存到tableList内。
      this.selects.forEach((i) => {
        this.tableList.push(this.list[i].name);
      });
      //打印出来看看
      console.log(this.a, "ceshi");
    },
    
    removeSelect(selectIndex) {
      const index = this.selects.indexOf(selectIndex);
      this.selects.splice(index, 1);
    },
  },
};
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

接口写好了吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值