# 14. JPopup 弹出选择
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/14.%20JPopup%20%E5%BC%B9%E5%87%BA%E9%80%89%E6%8B%A9

JPopup 弹出选择组件使用指南


# JPopup 弹出选择组件

> [!info] 通过弹窗选择数据的组件，基于在线报表配置实现，支持单选、多选、排序等功能。

## 基本用法

```vue
<template>
  <JPopup
    v-model:value="userId"
    code="user_popup"
    :fieldConfig="fieldConfig"
    placeholder="请选择用户"
  />
</template>

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

const userId = ref('');

// 字段配置
const fieldConfig = [
  {
    source: 'id',      // 弹窗返回的字段
    target: 'userId',   // 表单赋值的字段
  },
  {
    source: 'name',
    target: 'userName',
  },
];
</script>
```

## Props 属性

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| value | `string` | `''` | 选中值 |
| code | `string` | `''` | 在线报表编码（必填） |
| fieldConfig | `Array` | `[]` | 字段映射配置（必填） |
| width | `number` | `1200` | 弹窗宽度 |
| placeholder | `string` | `'请选择'` | 占位符 |
| multi | `boolean` | `false` | 是否多选 |
| sorter | `string` | `''` | 排序字段 |
| param | `object` | `{}` | 传递给弹窗的参数 |
| spliter | `string` | `','` | 多选值分隔符 |
| groupId | `string` | `''` | 分组ID（子表必填） |
| formElRef | `object` | - | 表单引用 |
| setFieldsValue | `function` | - | 表单赋值方法 |
| showAdvancedButton | `boolean` | `true` | 是否显示高级搜索按钮 |

## 字段配置 (fieldConfig)

```typescript
interface FieldConfigItem {
  source: string;  // 弹窗表格中的字段名
  target: string;  // 要赋值的表单字段名，多个用逗号分隔
}

const fieldConfig: FieldConfigItem[] = [
  {
    source: 'id',           // 弹窗返回的 id 字段
    target: 'userId',       // 赋值给表单的 userId 字段
  },
  {
    source: 'realname',     // 弹窗返回的 realname 字段
    target: 'name,displayName',  // 同时赋值给 name 和 displayName
  },
];
```

## 在 BasicForm 中使用

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

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

const [registerForm] = useForm({
  schemas: [
    {
      field: 'userId',
      label: '选择用户',
      component: 'JPopup',
      componentProps: {
        code: 'user_popup',
        fieldConfig: [
          { source: 'id', target: 'userId' },
          { source: 'realname', target: 'userName' },
        ],
        placeholder: '请选择用户',
      },
    },
  ],
});
</script>
```

## 单选与多选

### 单选模式

```vue
<JPopup
  v-model:value="deptId"
  code="dept_popup"
  :fieldConfig="[{ source: 'id', target: 'deptId' }]"
  :multi="false"
  placeholder="请选择部门"
/>
<!-- 返回值：单个 ID 字符串 -->
```

### 多选模式

```vue
<JPopup
  v-model:value="userIds"
  code="user_popup"
  :fieldConfig="[{ source: 'id', target: 'userIds' }]"
  :multi="true"
  placeholder="请选择多个用户"
/>
<!-- 返回值：逗号分隔的 ID 字符串，如 "id1,id2,id3" -->
```

## 子表使用

子表中使用 JPopup 必须设置 `groupId` 以确保每个弹窗独立：

```vue
<script setup lang="ts">
const schemas = [
  {
    field: 'productId',
    label: '商品',
    component: 'JPopup',
    componentProps: ({ tableContext }) => ({
      code: 'product_popup',
      fieldConfig: [{ source: 'id', target: 'productId' }],
      groupId: `product_${tableContext?.index}`,  // 确保每行独立
    }),
  },
];
</script>
```

## 事件

| 事件名 | 参数 | 说明 |
|--------|------|------|
| update:value | `value: string` | 值变化时触发 |
| popUpChange | `values: object` | 选中后触发，返回所有赋值字段 |
| focus | - | 输入框聚焦时触发 |

```vue
<JPopup
  v-model:value="userId"
  code="user_popup"
  :fieldConfig="fieldConfig"
  @popUpChange="handlePopupChange"
/>

<script setup lang="ts">
function handlePopupChange(values) {
  console.log('选中的完整数据：', values);
  // values: { userId: 'xxx', userName: '张三' }
}
</script>
```

## 动态参数

可以通过 `param` 属性向弹窗传递动态参数：

```vue
<template>
  <JPopup
    v-model:value="productId"
    code="product_popup"
    :fieldConfig="fieldConfig"
    :param="{ category: currentCategory, status: 1 }"
  />
</template>

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

const currentCategory = ref('electronics');
const fieldConfig = [{ source: 'id', target: 'productId' }];
</script>
```

## 回调赋值

JPopup 支持三种赋值方式：

### 1. 通过 v-model 赋值自身

```vue
<JPopup v-model:value="userId" ... />
```

### 2. 通过 formElRef 赋值表单

```vue
<JPopup
  :formElRef="formRef"
  :fieldConfig="fieldConfig"
  ...
/>
```

### 3. 通过 setFieldsValue 方法赋值

```vue
<JPopup
  :setFieldsValue="setFieldsValue"
  :fieldConfig="fieldConfig"
  ...
/>
```

## 完整示例：部门选择

```vue
<template>
  <BasicForm @register="registerForm">
    <template #deptId="{ model, field }">
      <JPopup
        v-model:value="model[field]"
        code="sys_depart_popup"
        :fieldConfig="[
          { source: 'id', target: 'deptId' },
          { source: 'departName', target: 'deptName' },
        ]"
        placeholder="请选择部门"
      />
    </template>
  </BasicForm>
</template>

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

const [registerForm] = useForm({
  schemas: [
    {
      field: 'deptId',
      label: '所属部门',
      component: 'JPopup',
    },
    {
      field: 'deptName',
      label: '部门名称',
      component: 'Input',
      // 只读，由 JPopup 赋值
      componentProps: { disabled: true },
    },
  ],
});
</script>
```

## 相关文件

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

## 相关组件

- [[09. BasicForm 表单]] - 表单容器组件
- [[18. JSelectUser 用户选择]] - 用户选择组件
- [[17. JSelectDept 部门选择]] - 部门选择组件
