当前位置:   article > 正文

antd-vue + vue3 实现a-table动态增减行,通过a-from实现a-table行内输入验证_a-table里面加from标签

a-table里面加from标签
 一、效果图

图一:校验效果

二、主要代码

注意:

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>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/91538
推荐阅读
相关标签
  

闽ICP备14008679号