*初级小白*~ 2022-06-30 18:07 采纳率: 97.9%
浏览 92
已结题

vue 表格 input编辑问题


<template>
  <div>
    <table class="table">
      <thead>
        <tr>
          <th>序号</th>
          <th>商品名称</th>
          <th>商品分类</th>
          <th>销售数量</th>
          <th>商品价格</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in stu" :key="index">
          <td>
            <div>{{ index + 1 }}</div>
          </td>
          <td>
            {{ item.name }}
          </td>
          <td>
            {{ item.type }}
          </td>
          <td>
            <div v-if="!item.isChange">{{ item.num }}</div>
            <div v-else>
              <a-input v-model:value="userNum" />
            </div>
          </td>
          <td>
            <div v-if="!item.isChange">{{ item.price }}</div>
            <div v-else>
              <a-input v-model:value="userPrice" />
            </div>
          </td>
          <td>
            <button type="button" v-html="'修改'" @click="handleChangeClick(item)" v-if="!item.isChange" />
            <template v-else>
              <button type="button" v-html="'保存'" @click="handleChangeOkClick(item)" />
              <button type="button" v-html="'取消'" @click="handleCancelClick(item)" />
            </template>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import { reactive, ref } from "vue";

export default {
  setup() {
    let stu = reactive([
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
      { name: "行情数据", type: 17, num: 6, price: "¥30", isChange: false },
    ]);
    const userNum = ref();
    const userPrice = ref();
    //修改
    const handleChangeClick = (item) => {
      // console.log(item);
      item.isChange = !item.isChange;
      userNum.value = item.num;
      userPrice.value = item.price;
    };
    //保存
    const handleChangeOkClick = (item) => {
      item.isChange = false;
      const dataItem = Object.assign({}, item, {
        num: userNum.value,
        price: userPrice.value,
      });
      Object.assign(stu.filter(itm => item.name === itm.name)[0], dataItem);
    };

    //取消
    const handleCancelClick = (item) => {
      item.isChange = !item.isChange;
    };
    return {
      stu,
      handleChangeClick,
      handleChangeOkClick,
      handleCancelClick,
      userPrice,
      userNum,
    };
  },
};
</script>

<style lang="scss" scoped>
.table {
  border: none;
  border-collapse: collapse;
  background: white;
  text-align: center;
  margin-top: 10px;

  thead {
    tr {
      color: white;
      height: 30px;

      th {
        background-color: rgba(255, 140, 45, 0.44);
        min-width: 100px;
        position: -webkit-sticky;
        position: sticky;
        top: 0;
        z-index: 3;
        border: 1px solid rgba(0, 0, 0, 0.1);
      }
    }
  }

  tbody {
    tr {
      height: 30px;
      color: black;

      td {
        border: 1px solid rgba(0, 0, 0, 0.1);
        position: -webkit-sticky;
        position: sticky;
        min-width: 100px;
      }
    }
  }
}
</style>

序号1和序号2得数据不一样,当我点击序号1的修改,再点击序号2的修改,如何让这俩行的值保持原来的数据,两个不会互相影响

img

img

  • 写回答

4条回答 默认 最新

  • CSDN专家-showbo 2022-06-30 18:33
    关注

    userNum ,userPrice 应该弄成数组形式,然后通过下标index来获取,改下面的

    <template>
      <div>
        <table class="table">
          <thead>
            <tr>
              <th>序号</th>
              <th>商品名称</th>
              <th>商品分类</th>
              <th>销售数量</th>
              <th>商品价格</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in stu" :key="index">
              <td>
                <div>{{ index + 1 }}</div>
              </td>
              <td>
                {{ item.name }}
              </td>
              <td>
                {{ item.type }}
              </td>
              <td>
                <div v-if="!item.isChange">{{ item.num }}</div>
                <div v-else>
                  <a-input v-model:value="userNum[index]" />
                </div>
              </td>
              <td>
                <div v-if="!item.isChange">{{ item.price }}</div>
                <div v-else>
                  <a-input v-model:value="userPrice[index]" />
                </div>
              </td>
              <td>
                <button type="button" v-html="'修改'" @click="handleChangeClick(index)" v-if="!item.isChange" />
                <template v-else>
                  <button type="button" v-html="'保存'" @click="handleChangeOkClick(index)" />
                  <button type="button" v-html="'取消'" @click="handleCancelClick(index)" />
                </template>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </template>
     
    <script>
    import { reactive, ref } from "vue";
     
    export default {
      setup() {
        let stu = reactive([
          { name: "行情数据", type: 17, num: 16, price: "¥10", isChange: false },
          { name: "行情数据", type: 18, num: 26, price: "¥20", isChange: false },
          { name: "行情数据", type: 19, num: 36, price: "¥30", isChange: false },
          { name: "行情数据", type: 20, num: 46, price: "¥40", isChange: false },
          { name: "行情数据", type: 21, num: 56, price: "¥50", isChange: false },
          { name: "行情数据", type: 22, num: 66, price: "¥60", isChange: false },
          { name: "行情数据", type: 23, num: 76, price: "¥70", isChange: false },
          { name: "行情数据", type: 24, num: 86, price: "¥80", isChange: false },
          { name: "行情数据", type: 25, num: 96, price: "¥90", isChange: false },
          { name: "行情数据", type: 26, num: 106, price: "¥100", isChange: false },
        ]);
        const userNum = ref([]);
        const userPrice = ref([]);
        //修改
        const handleChangeClick = (index) => {
          let item=stu[index];
          item.isChange = !item.isChange;
          userNum.value[index] = item.num;
          userPrice.value[index] = item.price;
        };
        //保存
        const handleChangeOkClick = (index) => {
          let item=stu[index];
          item.isChange = false;
          item.num=userNum.value[index];
          item.price=userPrice.value[index];
        };
     
        //取消
        const handleCancelClick = (index) => {
          let item=stu[index];
          item.isChange = !item.isChange;
        };
        return {
          stu,
          handleChangeClick,
          handleChangeOkClick,
          handleCancelClick,
          userPrice,
          userNum,
        };
      },
    };
    </script>
     
    <style lang="scss" scoped>
    .table {
      border: none;
      border-collapse: collapse;
      background: white;
      text-align: center;
      margin-top: 10px;
     
      thead {
        tr {
          color: white;
          height: 30px;
     
          th {
            background-color: rgba(255, 140, 45, 0.44);
            min-width: 100px;
            position: -webkit-sticky;
            position: sticky;
            top: 0;
            z-index: 3;
            border: 1px solid rgba(0, 0, 0, 0.1);
          }
        }
      }
     
      tbody {
        tr {
          height: 30px;
          color: black;
     
          td {
            border: 1px solid rgba(0, 0, 0, 0.1);
            position: -webkit-sticky;
            position: sticky;
            min-width: 100px;
          }
        }
      }
    }
    </style>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 7月9日
  • 已采纳回答 7月1日
  • 创建了问题 6月30日

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口