当前位置:   article > 正文

Vue项目-Table添加Form表单校验

Vue项目-Table添加Form表单校验

一、HTML

  1. <template>
  2. <div class="taskInfo">
  3. <el-form
  4. :model="generateParams"
  5. :rules="formRules"
  6. ref="formRef"
  7. class="taskInfoForm"
  8. label-width="100px">
  9. <ul class="taskInfoSearch">
  10. <li>
  11. <el-form-item label="类型" prop="type">
  12. <el-select
  13. v-model="generateParams.type"
  14. placeholder="请选择类型">
  15. <el-option :value="1" label="aaa"></el-option>
  16. <el-option :value="2" label="bbb"></el-option>
  17. </el-select>
  18. </el-form-item>
  19. </li>
  20. <li>
  21. <el-form-item label="开始" prop="start">
  22. <el-input
  23. v-model="generateParams.start"
  24. placeholder="请输入开始值"></el-input>
  25. </el-form-item>
  26. </li>
  27. <li>
  28. <el-form-item label="结束" prop="end">
  29. <el-input
  30. v-model="generateParams.end"
  31. placeholder="请输入结束值"></el-input>
  32. </el-form-item>
  33. </li>
  34. <li>
  35. <el-form-item label="开始时间" prop="startTime">
  36. <el-date-picker
  37. v-model="generateParams.startTime"
  38. type="datetime"
  39. placeholder="请选择开始时间"
  40. format="YYYY/MM/DD hh:mm:ss"
  41. value-format="YYYY-MM-DD h:m:s" />
  42. </el-form-item>
  43. </li>
  44. <li>
  45. <el-form-item label="结束时间" prop="endTime">
  46. <el-date-picker
  47. v-model="generateParams.endTime"
  48. type="datetime"
  49. placeholder="请选择结束时间"
  50. format="YYYY/MM/DD hh:mm:ss"
  51. value-format="YYYY-MM-DD h:m:s" />
  52. </el-form-item>
  53. </li>
  54. </ul>
  55. <div class="taskInfoAddBtn">
  56. <el-button type="primary" @click="addRowData">新增表格数据</el-button>
  57. </div>
  58. <div class="taskInfoTab">
  59. <el-table
  60. :data="generateParams.studentInfo"
  61. :cell-class-name="cellClassName"
  62. :row-class-name="rowClassName"
  63. @cell-click="cellClick">
  64. <el-table-column label="姓名">
  65. <template #default="{ row, column }">
  66. <el-form-item
  67. :prop="'studentInfo.' + row.index + '.name'"
  68. :rules="formRules.name"
  69. v-if="row.index == rowIndex && column.index == columnIndex">
  70. <el-input
  71. v-model.number="row.name"
  72. type="number"
  73. @blur="hideInput"
  74. v-focus></el-input>
  75. </el-form-item>
  76. <span v-else>{{ row.name }}</span>
  77. </template>
  78. </el-table-column>
  79. <el-table-column label="年龄">
  80. <template #default="{ row, column }">
  81. <el-form-item
  82. :prop="'studentInfo.' + row.index + '.age'"
  83. :rules="formRules.age"
  84. v-if="row.index == rowIndex && column.index == columnIndex">
  85. <el-input
  86. v-model.number="row.age"
  87. type="number"
  88. @blur="hideInput"
  89. v-focus></el-input>
  90. </el-form-item>
  91. <span v-else>{{ row.age }}</span>
  92. </template>
  93. </el-table-column>
  94. <el-table-column label="性别">
  95. <template #default="{ row, column }">
  96. <el-form-item
  97. :prop="'studentInfo.' + row.index + '.sex'"
  98. :rules="formRules.sex"
  99. v-if="row.index == rowIndex && column.index == columnIndex">
  100. <el-input
  101. v-model.number="row.sex"
  102. type="number"
  103. @blur="hideInput"
  104. v-focus></el-input>
  105. </el-form-item>
  106. <span v-else>{{ row.sex }}</span>
  107. </template>
  108. </el-table-column>
  109. <el-table-column label="操作" width="100">
  110. <template #default="{ row }">
  111. <el-button type="danger" link @click="delRowData(row)"
  112. >删除</el-button
  113. >
  114. </template>
  115. </el-table-column>
  116. </el-table>
  117. </div>
  118. </el-form>
  119. <div class="generate">
  120. <el-button type="primary" @click="generate(formRef)"
  121. >任务信息生成</el-button
  122. >
  123. </div>
  124. <!-- 任务信息生成信息模态框 -->
  125. <TaskInfoModel ref="TaskInfoModelRef"></TaskInfoModel>
  126. </div>
  127. </template>

二、JS

  1. <script setup>
  2. // -------------------<<模块引入>>-------------------
  3. import { ref, reactive, onMounted, nextTick } from "vue";
  4. import { ElMessage } from "element-plus";
  5. // -------------------<<变量声明>>-------------------
  6. // 生成参数
  7. const generateParams = reactive({
  8. type: 1, // 类型
  9. start: 1000000000, // 开始
  10. end: 2000000000, // 结束
  11. startTime: "2024-08-05 15:33:55", // 开始时间
  12. endTime: "2024-08-15 15:33:55", // 结束时间
  13. studentInfo: [
  14. // 任务区域
  15. {
  16. name: 123.1122331, // 姓名
  17. age: 80.12345621, // 年龄
  18. sex: 1200.1, // 性别
  19. },
  20. {
  21. name: 114.1122331, // 姓名
  22. age: 80.12345621, // 年龄
  23. sex: 1200.1, // 性别
  24. },
  25. {
  26. name: 114.1122331, // 姓名
  27. age: 80.12345621, // 年龄
  28. sex: 1200.1, // 性别
  29. },
  30. ],
  31. });
  32. // form表单规则
  33. const formRules = reactive({
  34. type: [{ required: true, message: "请选择类型", trigger: "change" }],
  35. start: [{ required: true, message: "请输入开始", trigger: "blur" }],
  36. end: [{ required: true, message: "请输入结束", trigger: "blur" }],
  37. startTime: [{ required: true, message: "请选择开始时间", trigger: "change" }],
  38. endTime: [{ required: true, message: "请选择结束时间", trigger: "change" }],
  39. name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  40. age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
  41. sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
  42. });
  43. // 获取form表单ref
  44. const formRef = ref();
  45. // 行列index
  46. const rowIndex = ref(null);
  47. const columnIndex = ref(null);
  48. // 是否允许编辑
  49. const isEdit = ref(true);
  50. // -------------------<<函数定义>>-------------------
  51. // 生成
  52. const generate = (formEl) => {
  53. if (!formEl) return;
  54. formEl.validate((valid) => {
  55. if (valid) {
  56. console.log(generateParams);
  57. TaskInfoModelRef.value.openWin();
  58. } else {
  59. console.log("error submit!");
  60. //当验证失败跳转到空白的所在区域
  61. nextTick(() => {
  62. let isError = document.getElementsByClassName("is-error");
  63. isError[0].scrollIntoView({
  64. // 滚动到指定节点
  65. // 值有start,center,end,nearest
  66. block: "center",
  67. // 值有auto、instant,smooth,缓动动画
  68. behavior: "smooth",
  69. });
  70. });
  71. return false;
  72. }
  73. });
  74. };
  75. // 表格单元格className回调方法
  76. const cellClassName = ({ column, columnIndex }) => {
  77. column.index = columnIndex;
  78. };
  79. // 表格行className回调方法
  80. const rowClassName = ({ row, rowIndex }) => {
  81. row.index = rowIndex;
  82. };
  83. // 表格单元格点击事件
  84. const cellClick = (row, column) => {
  85. if (isEdit.value) {
  86. rowIndex.value = row.index;
  87. columnIndex.value = column.index;
  88. }
  89. };
  90. // input框失去焦点事件
  91. const hideInput = (e) => {
  92. if (e.target.value) {
  93. rowIndex.value = null;
  94. columnIndex.value = null;
  95. isEdit.value = true;
  96. } else {
  97. isEdit.value = false;
  98. }
  99. };
  100. // 新增数据
  101. const addRowData = () => {
  102. generateParams.studentInfo.push({
  103. name: "",
  104. age: "",
  105. sex: "",
  106. });
  107. };
  108. // 删除本行数据
  109. const delRowData = (row) => {
  110. if (generateParams.studentInfo.length > 1) {
  111. generateParams.studentInfo.splice(row.index, 1);
  112. } else {
  113. ElMessage.warning("最少保留1条数据");
  114. }
  115. };
  116. // -------------------<<函数执行>>-------------------
  117. onMounted(() => {});
  118. </script>

三、CSS

  1. <style lang="less" scoped>
  2. .taskInfo {
  3. width: 100%;
  4. height: 100%;
  5. :deep(.taskInfoForm) {
  6. width: 100%;
  7. height: calc(100% - 80px);
  8. .taskInfoSearch {
  9. width: 100%;
  10. height: 120px;
  11. display: flex;
  12. align-items: center;
  13. flex-wrap: wrap;
  14. li {
  15. width: 25%;
  16. height: 50%;
  17. .el-form-item__label {
  18. color: #fff;
  19. }
  20. .el-form-item {
  21. width: 100%;
  22. height: 100%;
  23. margin-bottom: 0px;
  24. display: flex;
  25. align-items: center;
  26. .el-date-editor.el-input,
  27. .el-input,
  28. .el-select {
  29. width: 100% !important;
  30. }
  31. }
  32. }
  33. }
  34. .taskInfoAddBtn {
  35. width: 100%;
  36. height: 50px;
  37. display: flex;
  38. align-items: center;
  39. justify-content: flex-end;
  40. }
  41. .taskInfoTab {
  42. width: 100%;
  43. height: calc(100% - 170px);
  44. position: relative;
  45. .el-table {
  46. width: 100%;
  47. height: 100%;
  48. background: transparent !important;
  49. position: absolute;
  50. .el-form-item {
  51. width: 100%;
  52. margin-bottom: 0px;
  53. .el-form-item__content {
  54. margin-left: 0px !important;
  55. display: flex;
  56. align-items: center;
  57. justify-content: center;
  58. .el-input,
  59. .el-select {
  60. width: 80% !important;
  61. }
  62. }
  63. }
  64. td,
  65. th {
  66. text-align: center;
  67. }
  68. }
  69. }
  70. }
  71. .generate {
  72. width: 100%;
  73. height: 80px;
  74. display: flex;
  75. align-items: center;
  76. justify-content: center;
  77. }
  78. }
  79. </style>

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

闽ICP备14008679号