antd 表格合并行与scopedSlots一起使用

1、需求如图(表格中编辑间隔时间和备注):

在这里插入图片描述

2、直接贴代码:
html:
<a-table
  bordered
  :columns="inspectionColumns"
  :dataSource="inspectionData"
  :pagination="false"
  rowKey="id"
>
  <template
    v-for="col in ['intervalTime']"
    :slot="col"
    slot-scope="record"
  >
    <div :key="col">
      <a-input
        v-if="record.min"
        style="margin: -5px 0"
        :value="record.min"
        @change="e => changeMin(e.target.value, record)"
      />
      <template v-else>
        {{ record.min }}
      </template>
    </div>
  </template>
  <template
    v-for="item in ['remark']"
    :slot="item"
    slot-scope="record"
  >
    <div :key="item">
      <a-textarea 
        v-if="record.remark"
        style="margin: -5px 0"
        :value="record.remark"
        @change="e => changeRemark(e.target.value, record)"
      />
      <template v-else>
        {{ record.remark }}
      </template>
    </div>
  </template>
</a-table>

js:
<script>
const inspectionColumns = [
  {
    title: "名称",
    dataIndex: "name",
    width: 220,
  },
  {
    title: "间隔时间(分钟)",
    scopedSlots: {
      customRender: "intervalTime",
    },
    width: 150,
  },
  {
    title: '备注',
    scopedSlots: {customRender: 'remark'},
    customCell: ((remark, index) => {
      const obj = {
        children: remark,
        style: {},
        attrs: {},
      };
      if (index === 3) {
        obj.attrs.rowSpan = 2;
      }
      if (index === 4) {
        obj.style = {display: "none"};
      }
      return obj;
    })
  }
];
export default {
  data() {
    return {
      inspectionColumns,
      inspectionData: [
        {
          id: 1,
          name: '低满管度间隔时间(分钟)',
          min: '5',
          remark: '发送到发1'
        },
        {
          id: 2,
          name: '中满管度间隔时间(分钟)',
          min: '6',
          remark: '发送到发2'
        },
        {
          id: 3,
          name: '高满管度间隔时间(分钟)',
          min: '7',
          remark: '发送到发3'
        },
        {
          id: 4,
          name: '高满管度连续检测次数',
          min: '8',
          remark: '发送到发4'
        },
        {
          id: 5,
          name: '高满管度延长间隔时间(分钟)',
          min: '9',
          remark: '发送到发5'
        },
      ]
    }
  },
  methods: {
    changeMin(value, item) {
      console.log(value, item);
    },
    changeRemark(value, item) {
      console.log(value, item);
    },
  }
}
</script>
3、重要的代码片段:
{
  title: '备注',
  scopedSlots: {customRender: 'remark'},
  customCell: ((remark, index) => {
    const obj = {
      children: remark,
      style: {},
      attrs: {},
    };
    if (index === 3) {
      obj.attrs.rowSpan = 2; // 合并
    }
    if (index === 4) {
      obj.style = {display: "none"}; // 隐藏多余的项
    }
    return obj;
  })
}
4、最后效果:

在这里插入图片描述

5、细心的朋友们会发现,间隔时间的输入框与UI图有差距,是的,因为如果使用InputNumber 数字输入框,change事件只返回value,区分不出是哪条数据修改了,而a-input的change事件是输入框内容变化时的回调
changeMin(value, item) {
 console.log(value, item);
},
changeRemark(value, item) {
  console.log(value, item);
},

在这里插入图片描述

6、添加需求:值只能输入数字或者小数点儿,如图:小时能输入小数点儿、分钟能输入数字

在这里插入图片描述

// 第一项小时单位可以输入小数点儿
if(item.id == 1) { 
  this.waterLoggingData[item.id - 1].min =
    this.getDecimal(obj) == "0" || this.getDecimal(obj) == ""
      ? "0"
      : this.getDecimal(obj);
} else {
  this.waterLoggingData[item.id - 1].min =
    this.getNumber(obj) == "0" || this.getNumber(obj) == ""
      ? "0"
      : this.getNumber(obj);
}
7、完整代码:
// 只能输入数字
getNumber(obj) {
  obj.target.value = obj.target.value.replace(/[^\d]/g, "");
  return obj.target.value;
},
// 可输入四位小数点儿
getDecimal(obj) {
  obj.target.value = obj.target.value.replace(/[^\d.]/g, "");
  obj.target.value = obj.target.value.replace(/^\./g, "");
  obj.target.value = obj.target.value
    .replace(".", "$#$")
    .replace(/\./g, "")
    .replace("$#$", ".");
  obj.target.value = obj.target.value.replace(
    /^(\-)*(\d+)\.(\d\d\d\d).*$/,
    "$1$2.$3"
  );
  return obj.target.value;
},
// 修改间隔时间
changeMin(obj, item) {
  this.$nextTick(() => {
    if (this.activeTab == "1") {
      this.inspectionData[item.id - 1].min = 
        this.getNumber(obj) == "0" || this.getNumber(obj) == ""
          ? "0"
          : this.getNumber(obj);
    } else if (this.activeTab == "2") {
      // 第一项小时单位可以输入小数点儿
      if(item.id == 1) { 
        this.waterLoggingData[item.id - 1].min =
          this.getDecimal(obj) == "0" || this.getDecimal(obj) == ""
            ? "0"
            : this.getDecimal(obj);
      } else {
        this.waterLoggingData[item.id - 1].min =
          this.getNumber(obj) == "0" || this.getNumber(obj) == ""
            ? "0"
            : this.getNumber(obj);
      }
    } else if (this.activeTab == "3") {
      this.riverWaterData[item.id - 1].min =
        this.getNumber(obj) == "0" || this.getNumber(obj) == ""
          ? "0"
          : this.getNumber(obj);
    } else if (this.activeTab == "4") {
      this.networkData[item.id - 1].min =
        this.getNumber(obj) == "0" || this.getNumber(obj) == ""
          ? "0"
          : this.getNumber(obj);
    } else if (this.activeTab == "5") {
      this.pumpData[item.id - 1].min =
        this.getNumber(obj) == "0" || this.getNumber(obj) == ""
          ? "0"
          : this.getNumber(obj);
    } else if (this.activeTab == "6") {
      this.gateData[item.id - 1].min =
        this.getNumber(obj) == "0" || this.getNumber(obj) == ""
          ? "0"
          : this.getNumber(obj);
    }
  });
  switch (this.activeTab) {
    case "1":
      this.$emit("getInspectionEditData", this.inspectionData);
      break;
    case "2":
      this.$emit("getWaterLoggingData", this.waterLoggingData);
      break;
    case "3":
      this.$emit("getRiverWaterData", this.riverWaterData);
      break;
    case "4":
      this.$emit("getNetworkData", this.networkData);
      break;
    case "5":
      this.$emit("getPumpData", this.pumpData);
      break;
    case "6":
      this.$emit("getGateData", this.gateData);
      break;
    default:
      break;
  }
},

我为你翻山越岭 却无心看风景

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Antd 表格组件的 `customRender` 和 `scopedSlots` 是用于自定义表格单元格内容的两种不同方式。 `customRender` 是一个函数,用于自定义表格单元格的渲染内容。你可以在函数中根据传入的参数来返回自定义的渲染结果。 `scopedSlots` 是一个对象,用于自定义表格单元格的内容。你可以在对象中定义不同的插槽名称,并将其对应的内容作为表格单元格的内容。 当 `customRender` 和 `scopedSlots` 同时存在时,Antd 表格组件会优先使用 `customRender` 来渲染单元格内容。这意味着,如果你同时使用了 `customRender` 和 `scopedSlots`,`scopedSlots` 中的插槽将会失效。 如果你想要使用 `scopedSlots` 来自定义表格单元格的内容,可以将 `customRender` 的值设为 `undefined` 或 null,这样 Antd 表格组件就会使用 `scopedSlots` 中定义的插槽来渲染单元格内容。 以下是一个示例代码: ```vue <template> <a-table :columns="columns" :data-source="data"> <template v-slot:title> Custom Table </template> <template v-slot:name="{ text }"> <span style="color: red">{{ text }}</span> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', customRender: undefined, // or null scopedSlots: { customRender: 'name' } }, { title: 'Age', dataIndex: 'age' }, { title: 'Address', dataIndex: 'address' } ], data: [ { name: 'John Doe', age: 30, address: 'New York' }, // ... ] }; } }; </script> ``` 在上面的示例中,我们将 `customRender` 的值设为 `undefined`,并在 `scopedSlots` 中使用 `name` 插槽来自定义 `Name` 列的内容。这样,`scopedSlots` 中的插槽就会生效,并且 `Name` 列的单元格内容会渲染为红色字体。 希望以上解答能对你有帮助!如有任何疑问,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值