• antd-vue + vue3 实现a-table动态增减行,通过a-from实现a-table行内输入验证


     一、效果图

    图一:校验效果

    二、主要代码

    注意:

    1、form 与 table 绑定的是同一个数据 tableSource 并且是一个数据(ElementUI 需要 对象包数组)

    2、form用的是 name 绑定  -> :name="[index, 'vlan_id']"

    3、form-item 总要需要加上 rules  -> :rules="rules.blur"

    1. <a-form ref="tableFormRef" :model="tableSource" :label-col="{ style: { width: '10px' } }" :wrapper-col="{ span: 0 }" :rules="rules">
    2. <a-table
    3. class="ant-table-striped"
    4. bordered
    5. :dataSource="tableSource"
    6. :columns="tableColumns"
    7. :pagination="false"
    8. :scroll="{ x: 1260 }"
    9. :row-class-name="(_record, index) => (index % 2 === 1 ? 'table-striped' : null)">
    10. <template #bodyCell="{ column, text, record, index }">
    11. <template v-if="column.dataIndex == 'vlan_id'">
    12. <a-form-item class="custom-form-item" label=" " :name="[index, 'vlan_id']" :rules="rules.blur">
    13. <a-input style="width: 100%" v-model:value="record[column.dataIndex]">a-input>
    14. a-form-item>
    15. template>
    16. template>
    17. a-table>
    18. a-form>

    1、template
    1. <div class="bottom-box">
    2. <div class="title-box">
    3. <p class="order-title">工单操作p>
    4. <a-button style="margin: 0 0 10px 0px" type="primary" size="small" @click="handleRowAdd">增加行a-button>
    5. div>
    6. <div class="table-box">
    7. <a-form ref="tableFormRef" :model="tableSource" :label-col="{ style: { width: '10px' } }" :wrapper-col="{ span: 0 }" :rules="rules">
    8. <a-table
    9. class="ant-table-striped"
    10. bordered
    11. :dataSource="tableSource"
    12. :columns="tableColumns"
    13. :pagination="false"
    14. :scroll="{ x: 1260 }"
    15. :row-class-name="(_record, index) => (index % 2 === 1 ? 'table-striped' : null)">
    16. <template #bodyCell="{ column, text, record, index }">
    17. <template v-if="column.dataIndex == 'vlan_id'">
    18. <a-form-item class="custom-form-item" label=" " :name="[index, 'vlan_id']" :rules="rules.blur">
    19. <a-input style="width: 100%" v-model:value="record[column.dataIndex]">a-input>
    20. a-form-item>
    21. template>
    22. <template v-else-if="column.dataIndex == 'cloud'">
    23. <a-form-item class="custom-form-item" label=" " :name="[index, 'cloud']" :rules="rules.cloud">
    24. <a-select style="width: 100%" v-model:value="record[column.dataIndex]" @change="hanlePlatformChange(index)">
    25. <a-select-option v-for="item in platforms" :value="item.value" :key="item.value">{{ item.label }}a-select-option>
    26. a-select>
    27. a-form-item>
    28. template>
    29. <template v-else-if="column.dataIndex == 'related_pool'">
    30. <a-form-item class="custom-form-item" label=" " :name="[index, 'related_pool']" :rules="rules.relatedPool">
    31. <a-select style="width: 100%" v-model:value="record[column.dataIndex]">
    32. <a-select-option v-for="item in platform[index].children" :value="item.value" :key="item.value">{{ item.label }}a-select-option>
    33. a-select>
    34. a-form-item>
    35. template>
    36. <template v-else-if="column.dataIndex == 'allocated' || column.dataIndex == 'purpose' || column.dataIndex == 'vlan_domain'">
    37. <a-form-item class="custom-form-item" label=" " :name="[index, column.dataIndex]" :rules="rules.change">
    38. <a-select style="width: 100%" v-model:value="record[column.dataIndex]">
    39. <a-select-option v-for="item in column.list" :value="item.value" :key="item.value">{{ item.label }}a-select-option>
    40. a-select>
    41. a-form-item>
    42. template>
    43. <template v-else-if="column.dataIndex == 'operation'">
    44. <a-button style="margin: 0 5px" type="primary" size="small" @click="handleRowDel(index)" danger>删除a-button>
    45. template>
    46. <template v-else>
    47. <a-input style="width: 100%" v-model:value="record[column.dataIndex]">a-input>
    48. template>
    49. template>
    50. a-table>
    51. a-form>
    52. div>
    53. <div class="btn-box">
    54. <a-button v-if="sendFail" style="margin: 0 5px" @click="handleCancleApply">取消申请a-button>
    55. <a-button style="margin: 0 5px" type="primary" @click="handleSubmit">提交a-button>
    56. div>
    57. div>

    2、script

    1. <script setup>
    2. import { h, reactive, ref } from 'vue';
    3. // 路由跳转
    4. import { useRouter } from 'vue-router';
    5. const { currentRoute } = useRouter();
    6. const router = useRouter();
    7. const tableFormRef = ref(); // form标签
    8. /**
    9. *
    10. * 表格数据
    11. */
    12. let tableSource = ref([]);
    13. // 校验规则
    14. const rules = {
    15. blur: [{ required: true, message: '请输入', trigger: 'blur' }],
    16. change: [{ required: true, message: '请选择', trigger: 'change' }],
    17. cloud: [{ required: true, message: '请选择所属平台', trigger: 'change' }],
    18. relatedPool: [{ required: true, message: '请选择硬件资源池', trigger: 'change' }]
    19. };
    20. // 提交申请
    21. const handleSubmit = () => {
    22. let params = {};
    23. if (tableSource.value.length == 0) {
    24. return message.error('工单不能为空!');
    25. }
    26. // form的校验方法
    27. tableFormRef.value.validate().then(() => {
    28. const tableSourceParams = JSON.parse(JSON.stringify(tableSource.value));
    29. params = {
    30. ...formState, // 其他的参数
    31. status: 1,
    32. deviceLists: tableSourceParams
    33. };
    34. // 接口
    35. submitOrder(params).then(res => {
    36. if (res.code == 8200) {
    37. cancelId.value = res.data.id;
    38. if (res.data.status == 3) {
    39. message.success('二级VLAN地址入网添加成功!');
    40. router.push({
    41. path: '/network-access/vlan'
    42. });
    43. } else if (res.data.status == 2) {
    44. message.error(failTip(res.data.errorMessage));
    45. sendFail.value = true;
    46. // if (route.query.id) {
    47. // echoDate();
    48. // }
    49. }
    50. }
    51. });
    52. });
    53. };
    54. script>

  • 相关阅读:
    【C++STL基础入门】list的运算符重载和关于list的算法
    【C语言趣味教程】(5) 常量:字面常量 | 类型常量 | const 关键字 | const 的声明 | 程序中的只读概念 | const 保护机制 | 如何巧妙区分 “指针常量“ 和 “常量指针“
    【excel密码】为什么工作表不能移动、复制了?
    【媒体邀约】媒体宣传——企业成长的催化剂
    C语言练习之递归实现n的k次方
    PlayWright(二十二)- allure插件(一)
    Android Studio 使用 自带的Hierarchy查看类/方法/调用的层级关系
    Linux------一篇博客了解Linux最常用的指令
    基础SQL DCL语句
    面试:Fragment懒加载
  • 原文地址:https://blog.csdn.net/snows_l/article/details/134286391