# 17. JSelectDept 部门选择
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/01.%20%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/03.%20%E7%BB%84%E4%BB%B6%E5%BA%93%E6%96%87%E6%A1%A3/07.%20%E9%80%89%E6%8B%A9%E5%99%A8%E7%BB%84%E4%BB%B6/17.%20JSelectDept%20%E9%83%A8%E9%97%A8%E9%80%89%E6%8B%A9

JSelectDept 部门选择组件使用指南


# JSelectDept 部门选择组件

> [!info] 部门选择器，支持树形结构、多选、异步加载等功能。

## 基本用法

```vue
<template>
  <JSelectDept v-model:value="deptId" placeholder="请选择部门" />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const deptId = ref('');
</script>
```

## Props 属性

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| value | `string \| array` | - | 选中值 |
| multiple | `boolean` | `true` | 是否多选 |
| disabled | `boolean` | `false` | 是否禁用 |
| placeholder | `string` | `'请选择部门'` | 占位符 |
| params | `object` | - | 请求参数 |
| rowKey | `string` | `'key'` | 作为值的字段名（主键） |
| labelKey | `string` | `'title'` | 作为标签的字段名 |
| sync | `boolean` | `true` | 是否同步加载 |
| checkable | `boolean` | `true` | 是否显示复选框 |
| checkStrictly | `boolean` | `false` | 父子节点选中状态是否关联 |
| showButton | `boolean` | `true` | 是否显示选择按钮 |

## Emits 事件

| 事件名 | 参数 | 说明 |
|--------|------|------|
| options-change | - | 选项变化时触发 |
| change | `value: string \| array` | 值变化时触发 |
| select | `options: array, values: array` | 选择后触发，返回选项和值 |
| update:value | `value: string \| array` | v-model 更新事件 |

## 单选模式

```vue
<template>
  <JSelectDept
    v-model:value="deptId"
    placeholder="请选择部门"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const deptId = ref('');  // 单个部门 ID
</script>
```

## 多选模式

```vue
<template>
  <JSelectDept
    v-model:value="deptIds"
    :multiple="true"
    placeholder="请选择多个部门"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const deptIds = ref([]);  // 部门 ID 数组
</script>
```

## 在 BasicForm 中使用

```vue
<template>
  <BasicForm @register="registerForm" />
</template>

<script setup lang="ts">
import { useForm } from '@/components/Form';

const [registerForm] = useForm({
  schemas: [
    {
      field: 'deptId',
      label: '所属部门',
      component: 'JSelectDept',
      componentProps: {
        placeholder: '请选择部门',
      },
    },
    {
      field: 'deptIds',
      label: '管理范围',
      component: 'JSelectDept',
      componentProps: {
        multiple: true,
        placeholder: '请选择管理范围',
      },
    },
  ],
});
</script>
```

## 禁用状态

```vue
<JSelectDept
  v-model:value="deptId"
  :disabled="true"
  placeholder="部门（禁用）"
/>
```

## 带参数请求

```vue
<template>
  <JSelectDept
    v-model:value="deptId"
    :params="{ status: '1' }"
    placeholder="启用状态的部门"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const deptId = ref('');
</script>
```

## 自定义字段映射

通过 `rowKey` 和 `labelKey` 自定义字段映射：

```vue
<template>
  <JSelectDept
    v-model:value="deptId"
    row-key="id"
    label-key="departName"
    placeholder="使用自定义字段的部门选择"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const deptId = ref('');
</script>
```

## 异步加载

```vue
<template>
  <JSelectDept
    v-model:value="deptId"
    :sync="false"
    placeholder="异步加载部门"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const deptId = ref('');
</script>
```

## 完整示例

```vue
<template>
  <BasicForm @register="registerForm" />
</template>

<script setup lang="ts">
import { useForm } from '@/components/Form';

const [registerForm] = useForm({
  schemas: [
    {
      field: 'deptId',
      label: '归属部门',
      component: 'JSelectDept',
      componentProps: {
        placeholder: '请选择部门',
      },
      rules: [{ required: true, message: '请选择部门' }],
    },
    {
      field: 'dataScope',
      label: '数据范围',
      component: 'JSelectDept',
      componentProps: {
        multiple: true,
        placeholder: '请选择数据权限范围',
      },
    },
  ],
  labelWidth: 100,
});
</script>
```

## 弹窗模式 (DeptSelectModal)

使用部门选择弹窗：

```vue
<template>
  <DeptSelectModal
    @register="registerModal"
    @getSelectResult="handleDeptSelected"
  />
  <a-button @click="openModal">选择部门</a-button>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useModalInner } from '@/components/Modal';
import DeptSelectModal from '@/components/Form/src/jeecg/components/modal/DeptSelectModal.vue';

const deptId = ref('');
const [registerModal, { openModal }] = useModalInner();

function handleDeptSelected(options, values) {
  console.log('选中的部门选项：', options);
  console.log('选中的部门值：', values);
}
</script>
```

## 相关文件

- 组件路径：`@/components/Form/src/jeecg/components/JSelectDept.vue`
- 弹窗组件：`@/components/Form/src/jeecg/components/modal/DeptSelectModal.vue`

## 相关组件

- [[09. BasicForm 表单]] - 表单容器组件
- [[18. JSelectUser 用户选择]] - 用户选择组件
- [[16. ApiSelect API选择器]] - API 下拉选择
