index.vue 13 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
<template>
  <el-form
    ref="form"
    :model="form"
    inline-message
    :disabled="options.disabled"
    :label-position="options.labelPosition || 'left'"
    :label-width="(options.labelWidth || 100) + 'px'"
    :size="size"
    :style="formStyle"
    class="avue-form-self"
    @submit.native.prevent="handleConfirm()"
  >
    <template v-if="options.column && options.column.length > 0">
      <form-content
        ref="formContent"
        :form="form"
        :columns="{ column: options.column }"
        :options="options"
        :is-show-important="isShowImportant"
      ></form-content>
    </template>
    <template v-if="options.group && options.group.length > 0">
      <el-tabs v-if="options.tabs" v-model="activeName" type="card">
        <el-tab-pane
          v-for="(g, gIndex) in options.group"
          :key="gIndex"
          :label="g.label || `标签${gIndex + 1}`"
          :name="`${gIndex}`"
        >
          <form-content
            ref="formContent"
            :form="form"
            :columns="g"
            :options="options"
            :is-show-important="isShowImportant"
          ></form-content>
        </el-tab-pane>
      </el-tabs>
      <!-- 表单的折叠组件 -->
      <el-collapse v-else v-model="collapseNames">
        <template v-for="(g, gIndex) in options.group">
          <el-collapse-item
            v-show="g.display"
            :key="gIndex"
            :name="g.prop"
            :disabled="true"
            :class="g.prop == '1669858313508_15760' ? 'otherItemClass' : ''"
          >
            <template slot="title">
              {{ g.label }}
              <span v-if="g.label == '二、排除标准'" class="noCol">
                {{ errorText }}
              </span>
            </template>
            <form-content
              v-if="collapseNames.includes(g.prop)"
              ref="formContent"
              :form="form"
              :columns="g"
              :options="options"
              :is-show-important="isShowImportant"
              @showError="showError"
            ></form-content>
          </el-collapse-item>
        </template>
      </el-collapse>
    </template>
    <div v-if="!options.closeBtn" class="form-footer">
      <template v-if="options.menuBtn">
        <template v-if="options.prevBtn">
          <el-button
            type="primary"
            :loading="loading"
            size="large"
            :disabled="disabled"
            @click="onPrev"
            >上一步</el-button
          >
        </template>
        <!-- 下一步按钮 -->
        <template v-if="options.submitBtn">
          <el-button
            type="primary"
            :icon="options.submitText == '提交' ? 'el-icon-check' : ''"
            :loading="loading"
            size="large"
            :disabled="disabled"
            @click="handleConfirm"
            >{{ options.submitText || "提交" }}</el-button
          >
        </template>
        <template v-if="options.emptyBtn">
          <el-button
            plain
            icon="el-icon-delete "
            size="large"
            @click="resetForm(true)"
            >{{ options.emptyText || "清空" }}</el-button
          >
        </template>
      </template>
      <template v-if="nextTabBtnShow">
        <el-form style="display: inline-block; margin-left: 10px">
          <el-button
            plain
            icon="el-icon-caret-right"
            size="large"
            @click="nextTab"
            >{{ options.nextTabText || "下一页" }}</el-button
          >
        </el-form>
      </template>
    </div>
  </el-form>
</template>

<script>
/**
 * @description 自定义表单
 */
import handleFormData from "./handleFormData"
import FormContent from "./FormContent"
import { isObject } from "@/utils/validate"
export default {
  name: "CustomForm",
  components: { FormContent },
  mixins: [handleFormData],
  props: {
    options: {
      //配置 数据
      type: Object,
      default: () => {
        return {}
      },
    },
    size: { type: String, default: "small" },
    formStyle: { type: Object },
    formEdit: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  provide() {
    return {
      vwForm: () => {
        const vwForm = {}
        for (const k in this.options) {
          if (k !== "column" && k !== "group") {
            vwForm[k] = this.options[k]
          }
        }
        return vwForm
      },
      // formId: "",
    }
  },
  data() {
    return {
      collapseNames: [],
      activeName: "0",
      loading: false,
      isShowImportant: false,
      form: {},
      errorText: "",
      disabled: false,
    }
  },
  computed: {
    nextTabBtnShow() {
      const { nextTabBtn, tabs, group } = this.options
      return tabs && group && group.length > 1 && nextTabBtn
    },
  },
  watch: {
    formEdit: {
      handler() {
        // console.log("configforms:", this.formEdit)
        this.initfields(this.formEdit)
      },
    },
  },
  created() {
    this.initforms()
    this.$nextTick(() => {
      // this.setformWatch(this.options, "form")
    })
  },
  mounted() {
    // 处理computeIndex
    let domArr = document.querySelectorAll(".computeIndex")
    for (let i = 0; i < domArr.length; i++) {
      domArr[i].style.height = "54px"
      domArr[i].innerHTML = ""
      if ((domArr.length - 1) / i == 2) {
        domArr[i].innerHTML = "6"
      }
      if (i == domArr.length - 1) {
        domArr[i].style.height = "53px"
      }
    }
  },
  methods: {
    clearAge() {
      // 去除年龄文本
      for (let i = 0; i < this.$refs.formContent.length; i++) {
        this.$refs.formContent[i].clearAge()
      }
    },
    // 显示不符合筛查条件
    showError(data) {
      if (data) {
        this.disabled = true
        this.errorText = "不符合筛查条件"
      } else {
        this.disabled = false
        this.errorText = ""
      }
    },
    nextTab() {
      this.initforms()
      const tabsLen = this.options.group.length
      let active = Number(this.activeName)
      let next = ++active
      if (next >= tabsLen) next = 0
      this.activeName = String(next)
      this.$emit("scrollTop")
      console.log(this.activeName)
    },

    imFieldChange(val = false) {
      this.isShowImportant = val
    },
    // 查询
    submit() {
      this.$emit("submit", this.form)
    },

    // 重置
    resetForm(flag) {
      if (flag) {
        this.$confirm("是否清空数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$message({
              type: "success",
              message: "清空成功!",
            })
            this.$refs.form.resetFields()
          })
          .catch(() => {})
      } else {
        if (this.$refs.form) this.$refs.form.resetFields()
      }
    },

    validate(func) {
      this.$refs.form.validate((valid) => {
        func(valid, () => {})
      })
    },
    onPrev() {
      this.$emit("onPrev", () => {
        // 完成之后的回调
        this.loading = false
      })
    },
    // 临时保存
    temporarySave() {
      //? 只传数据 获取当前步数,如果个人信息已经提交可以临时保存

      // this.$refs.form.validate((valid) => {
      //   if (valid) {
      const data = {}
      const form = this.deepClone(this.form)
      Object.keys(form).forEach((k) => {
        if (form[k] === undefined) {
          data[k] = ""
          return false
        }
        if (k === "YZZKJC") {
          // 牙周表格
          data[k] = JSON.stringify(form[k])
        } else if (
          Array.isArray(form[k]) &&
          form[k][0] &&
          isObject(form[k][0])
        ) {
          // 子表单 去除前端添加的显隐辅助数据($_)和 删除按钮辅助数据(showDelBtn)
          data[k] = form[k].map((item) => {
            for (let key in item) {
              if (["$_keyField", "$_hidden", "showDelBtn"].includes(key))
                delete item[key]
            }
            return item
          })
        } else {
          data[k] = form[k]
        }
      })
      this.$emit("temporaryConfirm", data, () => {
        // 完成之后的回调
        this.loading = false
      })
      // } else {
      //   this.loading = false
      // }
      // })
    },
    handleConfirm() {
      this.loading = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          const data = {}
          const form = this.deepClone(this.form)
          Object.keys(form).forEach((k) => {
            if (form[k] === undefined) {
              data[k] = ""
              return false
            }
            if (k === "YZZKJC") {
              // 牙周表格
              data[k] = JSON.stringify(form[k])
            } else if (
              Array.isArray(form[k]) &&
              form[k][0] &&
              isObject(form[k][0])
            ) {
              // 子表单 去除前端添加的显隐辅助数据($_)和 删除按钮辅助数据(showDelBtn)
              data[k] = form[k].map((item) => {
                for (let key in item) {
                  if (["$_keyField", "$_hidden", "showDelBtn"].includes(key))
                    delete item[key]
                }
                return item
              })
            } else {
              data[k] = form[k]
            }
          })
          this.$emit("handleConfirm", data, () => {
            // 完成之后的回调
            this.loading = false
          })
        } else {
          this.loading = false
        }
      })
    },

    // 表单赋值
    initforms() {
      const form = {}
      const collapseNames = []
      const initFunc = (column = []) => {
        if (column.length > 0) {
          column.forEach((item) => {
            if (!item.prop) return false

            if (
              item.type === "dynamic" ||
              item.type === "checkbox" ||
              item.type === "cascader" ||
              item.type === "upload" ||
              (item.type === "select" && item.multiple)
            ) {
              form[item.prop] = []
            } else if (
              item.type === "dental-tab" ||
              item.type === "dental-tab-tj"
            ) {
              form[item.prop] = {}
            } else if (item.type === "group") {
              initFunc(item.children.column)
            } else if (item.type !== "title") {
              form[item.prop] = item.value ? item.value : ""
            }
          })
        }
      }
      const { group, column } = this.options
      initFunc(column)
      group &&
        group.forEach((g) => {
          if (g.collapse) {
            collapseNames.push(g.prop)
          }
          initFunc(g.column)
        })
      this.collapseNames = collapseNames
      this.form = form
      this.loading = false
      this.$nextTick(() => {
        // 子组件数据初始化完成后
        setTimeout(() => {
          this.initfields(this.formEdit)
        }, 0)
        if (this.$refs.form && this.$refs.form.clearValidate) {
          this.$refs.form.clearValidate()
        }
      })
    },
    // 绑定部分值(此时表单已渲染)
    initfields(obj) {
      for (let k in obj) {
        // if (this.form.hasOwnProperty(k)) {
        this.form[k] = obj[k]
        // }
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.avue-form-self {
  padding: 0 11px;
  ::v-deep .el-collapse {
    border: none;
    .el-collapse-item {
      &:nth-child(1) {
        border-top: 1px solid #cccccc;
      }
    }
    .el-collapse-item__content {
      padding-bottom: 0px;
    }
    .el-collapse-item__header {
      border-radius: 4px;
      background: #fafafa;
      padding-left: 20px;
      border: 1px solid #cccccc;
      border-bottom-color: #ccc;
      border-top: 0px;
      font-size: 14px;
      font-family: AlibabaPuHuiTiM;
      color: #333333;
    }
    .el-collapse-item__wrap {
      border: none;
      padding: 10px;
      border-left: 1px solid #cccccc;
      border-right: 1px solid #cccccc;
      // border-bottom: 1px solid #cccccc !important;
      padding: 0px;
      // &:hover {
      // background-color: #ecf8ff;
      // outline: 1px dashed #ccc;
      // }
    }
    .el-collapse-item__header {
      font-size: 16px;
      font-weight: 600;
      color: rgba(0, 0, 0, 0.85);
      height: 50px;
      line-height: 50px;
    }
  }
  .form-footer {
    // position: absolute;
    // left: 50%;
    // transform: translateX(-50%);
    // bottom: -50px;
    margin-top: 20px;
    margin-bottom: 20px;
    text-align: center;
  }
}
::v-deep .el-col {
}
::v-deep .el-radio__input.is-disabled .el-radio__inner {
  // background-color: $base-color-default;
  // border-color: $base-color-default;
}
.noCol {
  font-size: 12px;
  margin-left: 12px;
  font-family: AlibabaPuHuiTiR;
  color: #ff4d4f;
}
.otherItemClass {
  ::v-deep {
    .row24:not(:last-child) {
      border-left: 0px;
    }
    .row24:last-child {
      border-left: 1px solid #cccccc;
    }
  }
}
::v-deep .el-collapse-item.is-disabled .el-collapse-item__header {
  // color: #bbb;
  cursor: default;
}
</style>