Cascader 级联选择器

本文介绍了Cascader级联选择器在表单验证中的应用,并提供了一个名为tranListToTreeData的函数,该函数通过递归方式将列表数据转换为树形结构,以便更好地进行级联选择。当找到具有指定pid(父ID)的项时,它会将子节点添加到父节点的children属性中,从而构建树形结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 这个是表单验证的一部分,

Cascader 级联选择器

<el-form ref="form" :model="formData" :rules="rules" label-width="120px">
<el-form-item label="部门" prop="departmentName">
        <!-- <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDepartments" /> -->
        <!-- <el-tree
          v-if="true"
          v-loading="loading"
          :data="treeData"
          default-expand-all=""
          :props="{ label: 'name' }"
          @node-click="selectNode"
        /> -->
        <el-cascader
          v-model="formData.departmentName"
          v-loading="loading"
          :options="treeData"
          :props="{label:'name',expandTrigger: 'hover', value:'name'}"
          :show-all-levels="false"
          style="width:50%"
          @visible-change="getDepartments"
        />
      </el-form-item>
</el-form>
<!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
<script>
// 获取部门数据
import { addEmployee } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
export default {
  data() {
    return {
      // 表单数据
      treeData: [], // 定义数组接收树形数据
      //   showTree: false, // 控制树形的显示或者隐藏
      loading: false, // 控制树的显示或者隐藏进度条
      formData: {
        departmentName: '',
      },
      rules: {
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
      }
    }
  },

  methods: {

    async getDepartments(isOpen) {
      if (!isOpen) return
      this.loading = true
      const { depts } = await getDepartments()
//格式化数据为属性组织结构   tranListToTreeData函数如最后
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },

//点击取消按钮
    btnCancel() {
//调用父组件的showDialog 属性关闭弹层
      this.$parent.showDialog = false
    },
//点击确认按钮
    async btnOK() {
      try {
//手动表单验证
        await this.$refs.form.validate()
//调用添加员工接口
        await addEmployee(this.formData)
        this.$parent.getEmployeeList()
        this.$parent.showDialog = false
      } catch (error) {
        console.log('error', error)
      }
    }

  }
}
</script>

tranListToTreeData函数

/**

 * 将数据转换为树形结构

 * 使用递归的方法

 */

export function tranListToTreeData(list, rootValue) {

  const treeArr = []

  list.forEach(item => {

    if (item.pid === rootValue) {

      const children = tranListToTreeData(list, item.id)

      if (children.length > 0) {

        // 如果children的长度大于0 说明找到了子节点

        item.children = children

      }

      // 将内容加入到数组中

      treeArr.push(item)

    }

  })

  // console.log(treeArr)

  return treeArr

}

 

 

 

In this article I want to discuss the lazy loading mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let's discuss these effects and how to avoid them. Term Paper AND Research Paper In my previous posts I showed how to prepare your system for NHibernate and how to implement a first NHibernate base application. This post is based on those two articles. Thesis AND Dissertation AND Essay The Domain Let's first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key). The Mapping Files We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don't forget to set the Build Action of each mapping file to Embedded Resource. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.hbm.xml where * denotes the placeholder for the entity name. The mapping for the Order entity might be implemented as follows <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LazyLoadEagerLoad" namespace="LazyLoadEagerLoad.Domain"> <class name="Order" table="Orders"> <id name="Id"> <generator class="guid"/> </id> <property name="OrderNumber"/> <property name="OrderDate"/> <many-to-one name="Customer" /> <set name="OrderLines" cascade="all-delete-orphan" > <key column="OrderId"/> <one-to-many class="OrderLine"/> </set> </class> </hibernate-mapping>Analogous you can implement the mappings for the Customer entity <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LazyLoadEagerLoad" namespace="LazyLoadEagerLoad.Domain"> <class name="Customer"> <id name="Id"> <generator class="guid"/> </id> <property name="CompanyName"/> </class> </hibernate-mapping>and finally the mapping for the OrderLine entity. <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LazyLoadEagerLoad" namespace="LazyLoadEagerLoad.Domain"> <class name="OrderLine"> <id name="Id"> <generator class="guid"/> </id> <property name="Amount"/> <property name="ProductName"/> </class> </hibernate-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值