add.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!-- -->
  2. <template>
  3. <div class="tab6Add">
  4. <i class="el-icon-arrow-left" title="返回" @click="$router.go(-1)"></i>
  5. <div class="title">数据{{ ruleForm.id ? "编辑" : "新增" }}</div>
  6. <div class="from">
  7. <div class="row" v-for="item in data" :key="item.name">
  8. <div>{{ item.name }}:</div>
  9. <el-input
  10. v-if="item.type === 'varchar'"
  11. v-model="ruleForm[item.name]"
  12. :maxlength="item.length"
  13. show-word-limit
  14. ></el-input>
  15. <el-input
  16. @input="changeInput(item.name)"
  17. v-else-if="item.type === 'int'"
  18. v-model="ruleForm[item.name]"
  19. :maxlength="item.length"
  20. show-word-limit
  21. ></el-input>
  22. <el-date-picker
  23. format="yyyy 年 MM 月 dd 日"
  24. value-format="yyyy-MM-dd"
  25. v-else-if="item.type === 'datetime'"
  26. v-model="ruleForm[item.name]"
  27. type="date"
  28. placeholder="选择日期"
  29. >
  30. </el-date-picker>
  31. </div>
  32. </div>
  33. <!-- 底部按钮 -->
  34. <div class="btn">
  35. <el-button @click="$router.go(-1)">返 回</el-button>
  36. <el-button type="primary" @click="save">保 存</el-button>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. import {
  42. getInfoById,
  43. recordInsert,
  44. recordDetail,
  45. recordUpdate,
  46. } from "@/utils/api";
  47. export default {
  48. name: "tab6Add",
  49. components: {},
  50. data() {
  51. //这里存放数据
  52. return {
  53. tableId: null,
  54. ruleForm: {},
  55. data: [],
  56. };
  57. },
  58. //监听属性 类似于data概念
  59. computed: {},
  60. //监控data中的数据变化
  61. watch: {},
  62. //方法集合
  63. methods: {
  64. // 点击保存
  65. async save() {
  66. if (this.ruleForm.id) {
  67. // 是编辑
  68. let res = await recordUpdate({
  69. id: this.ruleForm.id,
  70. record: this.ruleForm,
  71. tableId: this.tableId,
  72. });
  73. if (res.code === 0) {
  74. this.$message.success("新增成功");
  75. this.$router.go(-1);
  76. } else this.$message.warning(res.msg);
  77. } else {
  78. let res = await recordInsert({
  79. record: this.ruleForm,
  80. tableId: this.tableId,
  81. });
  82. if (res.code === 0) {
  83. this.$message.success("新增成功");
  84. this.$router.go(-1);
  85. } else this.$message.warning(res.msg);
  86. }
  87. },
  88. //数字类型只能输入正整数
  89. changeInput(val) {
  90. let pattern = /^[1-9][0-9]*$/; //正整数的正则表达式
  91. //不符合正整数时
  92. if (!pattern.test(this.ruleForm[val])) {
  93. //input框绑定的内容非数字的全部置为空
  94. this.ruleForm[val] = this.ruleForm[val].replace(/[^\d]/g, "");
  95. }
  96. this.ruleForm[val] = Number(this.ruleForm[val]);
  97. },
  98. },
  99. //生命周期 - 创建完成(可以访问当前this实例)
  100. created() {},
  101. //生命周期 - 挂载完成(可以访问DOM元素)
  102. async mounted() {
  103. // 表格id
  104. let id = Number(this.$route.params.id);
  105. this.tableId = id;
  106. // 数据id,用来编辑
  107. let k = this.$route.query.k;
  108. if (k) {
  109. // 如果是编辑
  110. k = Number(k);
  111. let res1 = await recordDetail(id, k);
  112. this.ruleForm = res1.data;
  113. }
  114. let res = await getInfoById(id, {
  115. pageNum: 1,
  116. pageSize: 1,
  117. });
  118. let temp = res.data.fieldNames;
  119. this.data = temp.filter((v) => {
  120. return v.name !== "id" && v.name !== "update_time" && v.name !== "uuid" ;
  121. });
  122. },
  123. beforeCreate() {}, //生命周期 - 创建之前
  124. beforeMount() {}, //生命周期 - 挂载之前
  125. beforeUpdate() {}, //生命周期 - 更新之前
  126. updated() {}, //生命周期 - 更新之后
  127. beforeDestroy() {}, //生命周期 - 销毁之前
  128. destroyed() {}, //生命周期 - 销毁完成
  129. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  130. };
  131. </script>
  132. <style lang='less' scoped>
  133. .tab6Add {
  134. position: relative;
  135. width: 100%;
  136. height: 100%;
  137. /deep/.el-input__inner {
  138. padding-right: 70px;
  139. }
  140. /deep/.el-icon-arrow-left {
  141. position: absolute;
  142. left: 10px;
  143. top: 0;
  144. font-size: 30px;
  145. display: block;
  146. cursor: pointer;
  147. }
  148. .title {
  149. font-weight: 700;
  150. padding-left: 60px;
  151. height: 30px;
  152. line-height: 30px;
  153. }
  154. .from {
  155. height: 650px;
  156. overflow-y: auto;
  157. width: 1200px;
  158. margin: 20px 0 0 100px;
  159. .row {
  160. display: flex;
  161. align-items: center;
  162. justify-content: space-between;
  163. margin-bottom: 10px;
  164. & > div {
  165. width: calc(100% - 910px);
  166. text-align: right;
  167. }
  168. /deep/.el-input {
  169. width: 900px;
  170. }
  171. }
  172. }
  173. .btn {
  174. width: 160px;
  175. height: 40px;
  176. position: absolute;
  177. bottom: 40px;
  178. left: 50%;
  179. transform: translateX(-50%);
  180. }
  181. }
  182. </style>