数组高级用法
本文介绍 Piying-View 中数组的高级特性:增删改操作、deletionMode、groupMode 等。
数组基本操作
Section titled “数组基本操作”Piying-View 的 FieldArray 提供以下操作方法:
set — 设置指定索引的值
Section titled “set — 设置指定索引的值”直接调用 field.action.set(value, index):
支持跳过前面的元素直接插入:
remove — 删除指定索引的元素
Section titled “remove — 删除指定索引的元素”直接调用 field.action.remove(index):
updateValue — 整体替换数组
Section titled “updateValue — 整体替换数组”直接调用 field.form.control.updateValue(value):
clear — 清空数组
Section titled “clear — 清空数组”直接调用 field.form.control.clear():
reset — 重置到初始值
Section titled “reset — 重置到初始值”deletionMode — 删除模式
Section titled “deletionMode — 删除模式”控制数组元素被删除后的处理方式。
shrink(默认)— 收缩模式
Section titled “shrink(默认)— 收缩模式”删除元素后,数组长度自动缩短:
mark — 标记模式
Section titled “mark — 标记模式”删除元素后,该位置的值设为 undefined,数组长度保持不变:
groupMode — 组验证模式
Section titled “groupMode — 组验证模式”控制数组与元组的验证行为。groupMode 在解析 schema 时自动推导,无需手动赋值。
| Schema 类型 | 自动解析的 groupMode |
说明 |
|---|---|---|
v.tuple() |
'default' |
固定长度元组:超出长度部分自动截断 |
v.looseTuple() |
'loose' |
宽松元组:超出长度部分保留 |
v.strictTuple() |
'strict' |
严格元组:超出长度时验证失败 |
v.array() / v.tupleWithRest() 的 rest 部分 |
'reset' |
数组/可变尾部:不做固定键/长度约束 |
对象组的 groupMode(v.object() / v.looseObject() / v.strictObject())见 类型映射。
emptyValue — 空值
Section titled “emptyValue — 空值”当数组为空时,返回 emptyValue 作为最终值:
Array 属性
Section titled “Array 属性”FieldArray 的 length / controls / fixedControls$ / clear() 等属性,以及 Field 层面的 children / fixedChildren / restChildren,属于控件 API,统一见 AbstractControl — FieldArray 特有 API。
Tuple 与 TupleWithRest
Section titled “Tuple 与 TupleWithRest”Tuple — 固定长度元组
Section titled “Tuple — 固定长度元组”TupleWithRest — 元组 + 可变尾部
Section titled “TupleWithRest — 元组 + 可变尾部”- Record Schema 动态对象组 — 动态键值对表单
- formConfig — deletionMode / groupMode / emptyValue 配置详解