zhanghua
2023-02-09 32107ca1dd2c3930dcedf768e3dff163ff3bca35
src/views/operate/car/myIndex/create/law/index.vue
@@ -20,21 +20,7 @@
          <!-- 所属部门 -->
          <el-form-item class="optionItems" label="所属部门:" prop="departId">
            <!-- <el-input v-model="car.depart" placeholder="请填写所属部门"></el-input> -->
            <el-select v-model="car.departName" placeholder="请选择所属部门">
              <el-option value="1">
                <el-tree
                  ref="tree"
                  :check-strictly="true"
                  :data="departList"
                  :props="defaultProps"
                  show-checkbox
                  @check-change="handleCheck"
                  default-expand-all
                  node-key="id"
                >
                </el-tree>
              </el-option>
            </el-select>
            <departTree @selectDepart="selectDepart"></departTree>
          </el-form-item>
          <!-- 车主姓名 -->
          <el-form-item class="optionItem" label="车主姓名:" prop="ownerName">
@@ -74,7 +60,11 @@
</template>
<script>
import { validateCarNum, validatePhone, validateName } from "@/utils/validate";
import departTree from "@/components/departTree/index.vue";
export default {
  components: {
    departTree,
  },
  data() {
    const checkCarNum = (rule, value, callback) => {
      if (value) {
@@ -183,12 +173,7 @@
      },
    };
  },
  created() {
    // 获取全部部门列表
    this.$axios.get("/sccg/depart/tree").then((res) => {
      this.departList = res.data;
    });
  },
  created() {},
  methods: {
    // 添加车辆
    handleCar() {
@@ -221,34 +206,32 @@
    handleStop() {
      this.$emit("closeDialog", { flag: false, index: 0 });
    },
    // 部门修改
    handleCheck(data, checked) {
    selectDepart(depart) {
      if (depart) {
        this.car.departId = depart.departId;
        this.car.departName = depart.departName;
      }
    },
    nodeClick(data, checked) {
      this.checkedId = data.comcode;
      this.$refs.tree.setCheckedNodes([data]);
      this.car.departId = data.id;
      this.car.departName = data.departName;
      // 获取当前选择的id在数组中的索引
      const indexs = this.selectOrg.orgsid.indexOf(data.id);
      // 如果不存在数组中,并且数组中已经有一个id并且checked为true的时候,代表不能再次选择。
      if (indexs < 0 && this.selectOrg.orgsid.length === 1 && checked) {
        this.$message({
          message: "只能选择一个部门!",
          type: "warning",
          showClose: true,
        });
        // 设置已选择的节点为false 很重要
        this.$refs.tree.setChecked(data, false);
      } else if (this.selectOrg.orgsid.length === 0 && checked) {
        // 发现数组为空 并且是已选择
        // 防止数组有值,首先清空,再push
        this.selectOrg.orgsid = [];
        this.selectOrg.orgsid.push(data.id);
      } else if (
        indexs >= 0 &&
        this.selectOrg.orgsid.length === 1 &&
        !checked
      ) {
    },
    handleCheck(data, checked) {
      if (checked == true) {
        this.checkedId = data.comcode;
        this.$refs.tree.setCheckedNodes([data]);
        this.car.departId = data.id;
        this.car.departName = data.departName;
        this.selectOrg.orgsid = [];
        this.selectOrg.orgsid.push(data.id);
      } else {
        // 再次直接进行赋值为空操作
        this.selectOrg.orgsid = [];
        this.car.departName = "";
        this.user.departmentId = "";
      }
    },
  },