edit.vue 11.6 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
<template>
  <el-dialog
    :width="dialogWidth"
    :class="dialogFullScreen ? 'full-screen' : 'notfull-screen'"
    :close-on-click-modal="false"
    center
    :visible.sync="showDialog"
    :fullscreen="dialogFullScreen"
    @close="handleCloseDialog('close')"
  >
    <template v-slot:title>
      {{ option.title + "--" + modelType }}
      <button
        type="button"
        aria-label="Close"
        class="el-dialog__headerbtn"
        style="right: 50px"
        @click="dialogFullScreen = !dialogFullScreen"
      >
        <i class="el-dialog__close el-icon el-icon-full-screen" />
      </button>
    </template>

    <!--主表详情信息-->
    <component
      :is="'EditForm'"
      ref="mainForm"
      v-model="saveForm"
      :option="option"
      :model-type="modelType"
      :show-dialog="showDialog"
      @changeRowColNum="handleSetRowColNum"
    />

    <!--关联表相关-->
    <template v-for="(item, index) in joinEntitys">
      <component
        :is="getComponentByJoinType(item.joinType, item)"
        :ref="'joinForm' + index"
        :key="index"
        v-model="saveForm[item.fieldNameInMainEntityOnSave]"
        :value-new.sync="saveForm[item.fieldNameInMainEntityOnId]"
        :option="item"
        :model-type="modelType"
        :relate-data="saveForm"
      />

      <!--孙子的关联表-->
      <template v-for="(grandsonItem, grandsonIndex) in item.joinEntitys">
        <component
          :is="getComponentByJoinType(grandsonItem.joinType, grandsonItem)"
          ref="grandsonForm"
          :key="index + '.' + grandsonIndex"
          :option="grandsonItem"
          :model-type="modelType"
          :relate-data="saveForm"
        />
      </template>
    </template>

    <!--自定义的卡片插槽-->
    <slot name="customCard" />

    <div slot="footer" style="text-align: center">
      <slot v-if="modelType == 'edit'" name="editBtn" :rowData="rowData" />
      <el-button type="danger" plain @click="handleCloseDialog('close')"
        >关闭</el-button
      >
      <el-button
        v-if="modelType != 'view'"
        type="primary"
        plain
        @click="handleValidateAndSave"
        >保存</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import EditForm from "./edit-form";
import EditTable from "./edit-table";
export default {
  name: "EditDialog",
  components: { EditForm, EditTable },
  props: {
    visible: {
      type: [Boolean],
      default: () => {
        return false;
      }
    },
    rowData: {
      // 查询参数,对应表格那行数据
      type: [Object],
      default: () => {
        return {};
      }
    },
    // 预处理详情接口数据
    handleDetailData: {
      type: Function,
      default: data => {
        return data;
      }
    },
    // 预处理提交数据
    submitDetailData: {
      type: Function,
      default: (data, tpe) => {
        return data;
      }
    },
    modelType: String, // add view edit
    option: {
      require: true,
      type: Object,
      default: () => {
        return {
          title: "", // 页面标题
          labelWidth: "", // 表单输入框label宽度
          queryFormFields: [], // 查询表单条件
          buttons: {
            // 按钮
            query: {},
            edit: {},
            delete: {},
            add: {}
          },
          columns: [] // 表格列
        };
      }
    }
  },
  data() {
    return {
      showDialog: false, // 编辑详情弹框是否显示
      dialogFullScreen: false, // 弹出框全屏
      cardRowColNum: 2, // 主信息一行显示几列

      // 提交表单的数据
      saveForm: {},
      // 已成功校验的关联表单个数
      countForValidJoinForm: 0
    };
  },
  computed: {
    // 弹出框的宽度,根据一行显示几列动态调整
    dialogWidth() {
      if (this.cardRowColNum == 2) {
        return "60%";
      }
      if (this.cardRowColNum == 3) {
        return "70%";
      }
      if (this.cardRowColNum == 4) {
        return "80%";
      }
      return "60%";
    },
    // 关联属性表
    joinEntitys() {
      if (this.isBlank(this.option.joinEntitys)) {
        return [];
      } else {
        return this.option.joinEntitys;
      }
    },
    // 一对一关联表的个数
    countJoinEntityOneToOne() {
      let entitys = this.joinEntitys.filter(
        item => item["joinType"] == "OneToOne"
      );
      if (entitys == null) {
        return 0;
      }
      return entitys.length;
    }
  },
  watch: {
    // 监控dialog的显示隐藏变量
    visible(newValue, oldValue) {
      this.showDialog = newValue;
      // 为主表的编辑表单,渲染上默认值
      this.initDefaultSaveForm();
    },
    rowData(newValue, oldValue) {
      if (newValue != null) {
        this.queryByPrimarykey(newValue);
      }
    }
  },
  mounted() {
    // 为主表的编辑表单,渲染上默认值
    this.initDefaultSaveForm();
  },
  methods: {
    // 暴露给外部crud页面,回传saveForm的值
    getSaveForm() {
      return this.saveForm;
    },
    setSaveForm(saveForm) {
      this.saveForm = saveForm;
    },
    initDefaultSaveForm() {
      // saveForm的默认值
      let defaultSaveForm = {};
      this.option.columns.forEach(item => {
        let key = item.editField;
        if (this.isBlank(key)) {
          key = item.field;
        }
        let val = item.defaultValue;
        if (this.isNotBlank(val)) {
          defaultSaveForm[key] = val;
        }
      });
      // 为主表的编辑表单,渲染上默认值
      this.saveForm = this.deepClone(defaultSaveForm);
      console.log("编辑框默认值:" + JSON.stringify(this.saveForm));
    },
    handleCloseDialog(val) {
      // 为主表的编辑表单,渲染上默认值
      this.initDefaultSaveForm();
      this.showDialog = false; // 编辑详情弹框是否显示
      this.dialogFullScreen = false; // 弹出框全屏
      this.cardRowColNum = 2; // 主信息一行显示几列
      this.countForValidJoinForm = 0; // 已成功校验的关联表单个数
      this.$emit("closeEvent", val);
    },
    handleTopCloseDialog() {
      // 为主表的编辑表单,渲染上默认值
      this.initDefaultSaveForm();
      this.showDialog = false; // 编辑详情弹框是否显示
      this.dialogFullScreen = false; // 弹出框全屏
      this.cardRowColNum = 2; // 主信息一行显示几列
      this.countForValidJoinForm = 0; // 已成功校验的关联表单个数
    },
    // 设置一行显示几列
    handleSetRowColNum(num) {
      this.cardRowColNum = num;
    },
    // 根据关联类型计算组件
    getComponentByJoinType(type, item) {
      if (type == "OneToOne") {
        return "EditForm";
      } else if (type == "OneToMany") {
        return "EditTable";
      } else {
        return "";
      }
    },
    async queryByPrimarykey(rowData) {
      const { data, code } = await this.option.buttons.queryByPrimarykey.api(
        rowData
      );
      if (code != "200") return;
      this.showDialog = true;
      this.saveForm = this.handleDetailData(data);
    },
    // 保存前,先调用校验
    handleValidateAndSave() {
      this.countForValidJoinForm = 0;
      // 主表单校验
      this.$refs.mainForm.validate(mainValid => {
        if (mainValid == false) {
          console.warn("主表单校验失败");
          return;
        }
        console.log("主表单校验完成");
        if (this.joinEntitys == null || this.joinEntitys.length == 0) {
          // 如果子表没有信息,直接提交
          this.handleSave();
          return;
        }
        for (let i = 0; i < this.joinEntitys.length; i++) {
          console.log(`开始校验子表单-${i} 校验`);
          let item = this.joinEntitys[i];
          console.log(item);
          if (
            this.$refs["joinForm" + i] == null ||
            item.hide == true ||
            this.saveForm[item.fieldNameInMainEntityOnSave] == null ||
            this.saveForm[item.fieldNameInMainEntityOnSave].length == 0
          ) {
            console.warn("子表单校验失败");
          } else {
            const childrenChecked = this.checkedChildrenValidate(
              this.saveForm[item.fieldNameInMainEntityOnSave],
              item.columns
            );
            if (!childrenChecked) {
              return;
            }

            //  console.log('子表单没有数据,直接跳过')
            this.countForValidJoinForm++;
            console.log(
              "已经校验的子表单:" +
                this.countForValidJoinForm +
                " 共:" +
                this.joinEntitys.length
            );
            // 所有关联表单校验通过
            if (this.countForValidJoinForm == this.joinEntitys.length) {
              console.log("子表单校验完成,提交主表单");
              this.handleSave();
            }
            continue;
          }
          let joinForm = this.$refs["joinForm" + i];
          if (toString.call(joinForm) == "[object Array]") {
            joinForm = joinForm[0];
          }
          joinForm.validate(joinValid => {
            if (joinValid) {
              this.countForValidJoinForm++;
              console.log(
                "已经校验的子表单:" +
                  this.countForValidJoinForm +
                  " 共:" +
                  this.joinEntitys.length
              );
              // 所有关联表单校验通过
              if (this.countForValidJoinForm == this.joinEntitys.length) {
                console.log("子表单校验完成,提交主表单");
                this.handleSave();
              }
            } else {
              console.warn(`子表单${i}校验失败:`);
            }
          });
        }
      });
    },
    async handleSave() {
      const params = this.submitDetailData(this.saveForm, this.modelType);
      // 新增
      if (this.modelType == "add") {
        const { code, message } = await this.option.buttons.add.api(params);
        if (code == "200") {
          // 保存结束,关闭对话框
          this.handleCloseDialog();
          // 向外层发关闭事件
          // this.$emit('closeEvent')
          return;
        } else {
          // ;(this.countForValidJoinForm = 0), // 已成功校验的关联表单个数
          console.log(`提交表单调用新增接口失败:${message}`);
        }
      }
      // 修改
      if (this.modelType == "edit") {
        const { code, message } = await this.option.buttons.edit.api(params);
        if (code == "200") {
          // 保存结束,关闭对话框
          this.handleCloseDialog();
          // 向外层发关闭事件
          // this.$emit('closeEvent')
          return;
        } else {
          // ;(this.countForValidJoinForm = 0), // 已成功校验的关联表单个数
          console.log(`提交表单调用更新接口失败:${message}`);
        }
      }
    },
    // 子表单数据校验
    checkedChildrenValidate(list, confingList) {
      const configFileds = confingList.map(item => item.field);
      for (let i = 0; i < list.length; i++) {
        const item = list[i];
        for (let key = 0; key < configFileds.length; key++) {
          if (
            item.hasOwnProperty(configFileds[key]) &&
            !item[configFileds[key]]
          ) {
            return false;
          }
        }
      }
      return true;
    }
  }
};
</script>

<style scoped lang="scss">
.notfull-screen {
  ::v-deep.el-dialog__body {
    background-color: rgb(240, 242, 245);
    padding: 5px;
    max-height: 60vh;
    overflow: auto;
  }
}
.full-screen {
  ::v-deep.el-dialog__body {
    background-color: rgb(240, 242, 245);
    padding: 5px;
    height: calc(100vh - 110px);
    overflow: auto;
  }
}
</style>