Skip to content

Commit 70e4370

Browse files
committed
docs(docs): 更新数据联动相关文档
1 parent 1cda05d commit 70e4370

File tree

7 files changed

+2048
-1798
lines changed

7 files changed

+2048
-1798
lines changed

packages/demo/src/index/views/Demo/schemaTypes/91.Object-property-dependencies/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
export default {
66
schema: {
7-
title: '单向依赖',
8-
description: '最基本的属性单向依赖,ui-schema 配置 onlyShowIfDependent 只在被依赖时才显示',
7+
title: 'Object property dependencies',
98
type: 'object',
109
properties: {
1110
unidirectional: {
12-
title: 'Unidirectional',
11+
title: '单向依赖',
12+
description: '最基本的属性单向依赖,ui-schema 配置 onlyShowIfDependent 只在被依赖时才显示',
1313
type: 'object',
1414
'ui:options': {
1515
onlyShowIfDependent: true

packages/docs/docs/zh/guide/adv-config.md

Lines changed: 132 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ sidebarDepth: 2
66

77
## 数据联动
88

9-
要实现数据联动可以有多种方法来实现
9+
> 要实现数据联动可以有多种方法来实现,支持一些打破 `JSON Schema` 规范的配置,可以结合实际场景选择方案。
1010
11-
* 通过 [anyOf](https://form.lljj.me/#/demo?type=AnyOf) 配置
12-
> 详细 AnyOf、oneOf 配置请 [点击查看](/zh/rules/combining.html)
11+
遵循 `JSON Schema` 规范包含如下几种方式:
12+
* [通过 JSON Schema anyOf 配置](#anyof-实现数据联动)
13+
* [通过 object dependencies 实现联动](#object-dependencies-实现数据联动)
14+
* [Todo: 通过 if else 实现联动](#通过-if-else-实现联动)
1315

14-
* 通过 object dependencies 实现联动
15-
* 通过 if else 实现联动 Todo
16-
* 自定义组件配置 [ui:field 使用已有联级组件](/zh/guide/adv-config.html#demo-联级选择)
17-
* 通过 [ui-schema fieldStyle](/zh/guide/basic-config.html#ui-schema) 动态配置 style样式隐藏显示
16+
打破 `JSON Schema` 规范包含如下几种方式:
17+
18+
* [通过自定义组件配置 ui:field 使用已有联级组件](#通过-ui-field-调用自己的联级组件)
19+
* [通过 ui-schema fieldStyle 动态样式](#ui-schema-fieldstyle-实现联动)
1820

1921
### anyOf 实现数据联动
22+
基于 [JSON Schema anyOf](https://json-schema.org/understanding-json-schema/reference/combining.html#anyof) 规范,[详细anyOf配置可参考这里](/zh/rules/combining.html#anyof)**适用于根据类型选择然后使用不同的数据结构或ui样式**
2023

21-
anyOf联动如下演示:(点击 `保存` 按钮查看 `formData` 数据)
24+
比如:设置个人资料可以通过 `firstName` + `lastName` 或者 通过 `userId` 两种方式来设置。如下演示:(点击 `保存` 按钮查看 `formData` 数据),也可以查看 [其它anyOf在线演示](https://form.lljj.me/#/demo?type=AnyOf)
2225

2326
::: demo
2427
```html
@@ -56,6 +59,12 @@ export default {
5659
title: '通过用户名设置',
5760
required: ['firstName'],
5861
properties: {
62+
type: {
63+
'ui:widget': 'HiddenWidget',
64+
title: '类型',
65+
type: 'string',
66+
default: 'userInfo'
67+
},
5968
firstName: {
6069
type: 'string',
6170
title: '名字',
@@ -71,6 +80,12 @@ export default {
7180
{
7281
title: '通过用户id设置',
7382
properties: {
83+
type: {
84+
'ui:widget': 'HiddenWidget',
85+
title: '类型',
86+
type: 'string',
87+
default: 'userId'
88+
},
7489
idCode: {
7590
type: 'string',
7691
title: 'ID',
@@ -178,22 +193,121 @@ export default {
178193
```
179194
:::
180195

181-
182196
>* 推荐使用 `anyOf``oneOf` 只能有一个符合的结果
183197
184198
### object dependencies 实现数据联动
185199

186-
object dependencies 目前只支持属性联动,schema联动不支持暂时的计划也不打算支持
200+
基于 [JSON Schema Object dependencies](https://json-schema.org/understanding-json-schema/reference/object.html#property-dependencies) 规范,**适用于根据需要根据值是否为空(undefined)来做联动设置***目前只支持 property dependencies*
187201

188-
### 通过 if else 实现联动
189-
目前来看 if else 比较容易解决数据联动的场景,且可以根据值来判断但依旧不支持逻辑表达式
202+
比如:填写了 `信用卡号` 就必须填写 `账单地址`。如下演示,也可以查看 [Object-property-dependencies在线演示](https://form.lljj.me/#/demo?type=Object-property-dependencies)
190203

191-
### 通过 ui:field 调用自己的联级组件
192-
打破 JSON Schema 规范
204+
::: demo
205+
```html
206+
<template>
207+
<vue-form
208+
v-model="formData"
209+
:schema="schema"
210+
>
211+
</vue-form>
212+
</template>
193213

194-
### ui-schema fieldStyle 实现联动
195-
打破 JSON Schema 规范
196-
因为 ui-schema和formData 本身都是响应式数据,所以完全可以动态动态 ui-schema的值,并且像有些类似框架直接提供了 使用函数表达式的能力
214+
<script>
215+
export default {
216+
name: 'Demo',
217+
data() {
218+
return {
219+
formData: {},
220+
schema: {
221+
title: 'Object property dependencies',
222+
type: 'object',
223+
properties: {
224+
unidirectional: {
225+
title: '单向依赖',
226+
description: '最基本的属性单向依赖,ui-schema 配置 onlyShowIfDependent 只在被依赖时才显示',
227+
type: 'object',
228+
'ui:options': {
229+
onlyShowIfDependent: true
230+
},
231+
properties: {
232+
name: {
233+
title: 'Name',
234+
type: 'string'
235+
},
236+
credit_card: {
237+
title: 'Credit card',
238+
type: 'string'
239+
},
240+
billing_address: {
241+
title: 'Billing address',
242+
type: 'string'
243+
}
244+
},
245+
required: [
246+
'name'
247+
],
248+
dependencies: {
249+
credit_card: [
250+
'billing_address'
251+
]
252+
}
253+
},
254+
bidirectional: {
255+
title: '双向依赖',
256+
description: '显式地定义双向依赖,如果配置 onlyShowIfDependent 会导致初始化没有值时都无法渲染,这里需要使用者执行考虑',
257+
type: 'object',
258+
properties: {
259+
name: {
260+
title: 'Name',
261+
type: 'string'
262+
},
263+
credit_card: {
264+
title: 'Credit card',
265+
type: 'string'
266+
},
267+
billing_address: {
268+
title: 'Billing address',
269+
type: 'string'
270+
}
271+
},
272+
required: [
273+
'name'
274+
],
275+
dependencies: {
276+
credit_card: [
277+
'billing_address'
278+
],
279+
billing_address: [
280+
'credit_card'
281+
]
282+
}
283+
}
284+
}
285+
},
286+
}
287+
}
288+
}
289+
</script>
290+
```
291+
:::
292+
293+
> `ui-schema` 配置 `onlyShowIfDependent: true` 可以隐藏没触发依赖的项
294+
295+
### if else 实现联动
296+
> *暂不支持*
297+
298+
基于 [JSON Schema if then else](https://json-schema.org/understanding-json-schema/reference/conditionals.html)**适用于根据值等于一个常量时来做联动***目前版本不支持该特性*
299+
300+
就目前来看 if else 比较容易解决数据联动的场景,可以根据值来做判断,但依旧不能解决对值支持逻辑判断,比如`大于``小于`,后续版本会考虑支持该特性。
301+
302+
### ui:field 调用自己的联级组件
303+
可能打破 `JSON Schema` 规范,**适用于通过配置一个已有的自定义组件来渲染一些复杂的联动场景**
304+
305+
比如: [ui:field 使用已有省市区联级组件](/zh/guide/adv-config.html#demo-联级选择)
306+
307+
### ui-schema 响应式实现联动
308+
可能打破 `JSON Schema` 规范。`ui-schema``formData` 本身都是响应式数据,所以完全可以通过计算属性返回 ui-schema,配置 `ui:widget:HiddenWidget``ui:field: null``ui:fieldStyle` 等都可以实现样式联动。
309+
310+
这个方法可以说是目前的下下策,会使得 `ui-schema` 配置存在大量的条件判断。
197311

198312
## 树形结构
199313
* 树形结构需要使用 `$ref` 来递归调用自己
@@ -267,8 +381,6 @@ object dependencies 目前只支持属性联动,schema联动不支持暂时的
267381
```
268382
:::
269383

270-
271-
272384
## 空数据默认值
273385
默认在用户输入时如果清空了表单的数据,即空字符串 `''`,会默认设置值为 `undefined`,这样是为了保证和JSON Schema 规范保持一致。
274386

@@ -591,7 +703,7 @@ import { fieldProps } from '@lljj/vue-json-schema-form';
591703

592704
### Demo - 联级选择
593705

594-
* Demo中 `ui:field` 使用现有组件嵌入,不使用schema配置和方法
706+
* Demo中 `ui:field` 使用现有省市区联级组件嵌入,不使用schema配置和方法
595707
* [查看field组件源码](https://github.com/lljj-x/vue-json-schema-form/blob/master/packages/docs/docs/.vuepress/injectVue/field/DistpickerField.vue)
596708

597709
>* 使用省市区联动组件

packages/docs/docs/zh/guide/basic-config.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ uiSchema = {
163163
}
164164
```
165165

166-
>1. `ui:field` 自定义field组件参见这里 [自定义 field](/zh/guide/adv-config.html#自定义-field)
167-
>1. `ui:widget` 自定义widget组件参见这里 [自定义 widget](/zh/guide/adv-config.html#自定义-widget)
168-
166+
>1. `ui:field` 自定义field组件参见这里 [自定义 field](/zh/guide/adv-config.html#自定义field)
167+
>1. `ui:widget` 自定义widget组件参见这里 [自定义 widget](/zh/guide/adv-config.html#自定义widget)
168+
>1. `ui:widget` 配置 `HiddenWidget` 或者 `hidden` 既可隐藏当前元素
169169
170170
#### ui-schema配置在schema中
171171

0 commit comments

Comments
 (0)