对象

创建对象,只对 schema.type="object" 时有效。

代码演示

基础样例

最简单的用法。

expand codeexpand code
import { Component } from '@angular/core';
import { SFSchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'form-object-simple',
  template: `<sf [schema]="schema" (formSubmit)="submit($event)"></sf>`,
})
export class FormObjectSimpleComponent {
  schema: SFSchema = {
    properties: {
      name: { type: 'string' },
      age: { type: 'number' },
    },
    required: ['name', 'age'],
    ui: {
      // 指定 `label` 和 `control` 在一行中各占栅格数
      spanLabel: 4,
      spanControl: 5,
    },
  };

  constructor(private msg: NzMessageService) {}

  submit(value: {}): void {
    this.msg.success(JSON.stringify(value));
  }
}
卡片容器

包含标题、内容、操作区域。

expand codeexpand code
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { SFObjectWidgetSchema, SFSchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'form-object-card',
  template: `
    <sf *ngIf="schema" [schema]="schema" (formSubmit)="submit($event)"></sf>
    <ng-template #extra>
      <a (click)="msg.success('Success')">More</a>
    </ng-template>
  `,
})
export class FormObjectCardComponent implements OnInit {
  @ViewChild('extra', { static: true }) private extra!: TemplateRef<void>;
  schema?: SFSchema;

  constructor(public msg: NzMessageService) {}

  ngOnInit(): void {
    this.schema = {
      properties: {
        name: { type: 'string' },
        age: { type: 'number' },
        address1: {
          title: '地址1',
          type: 'object',
          properties: {
            country: { type: 'string' },
            city: { type: 'string' },
            zone: { type: 'string' },
          },
          ui: {
            type: 'card',
            cardExtra: this.extra,
          } as SFObjectWidgetSchema,
        },
        address2: {
          title: '地址2',
          type: 'object',
          properties: {
            country: { type: 'string' },
            city: { type: 'string' },
            zone: { type: 'string' },
          },
          ui: {
            type: 'card',
            showExpand: false,
          } as SFObjectWidgetSchema,
        },
      },
      required: ['name', 'age'],
      ui: {
        spanLabelFixed: 150,
        grid: { span: 12, gutter: 16 },
      } as SFObjectWidgetSchema,
    };
  }

  submit(value: {}): void {
    this.msg.success(JSON.stringify(value));
  }
}

API

schema 属性

参数说明类型默认值
[properties]定义对象的属性{ [key: string]: SFSchema }-
[required]必填项属性string[]-
[maxProperties]最大属性个数,必须是非负整数number-
[minProperties]最小属性个数,必须是非负整数number-

ui 属性

参数说明类型默认值
[showExpand]是否显示扩展,点击隐藏内容,限 type === 'card'booleantrue
[expand]展开状态,限 type === 'card'booleantrue
[showTitle]是否显示标题booleanfalse
[type]渲染类型card, defaultdefault
[cardSize]等同 nzSize 属性small, defaultsmall
[cardBodyStyle]等同 nzBodyStyle 属性{ [key: string]: string }-
[cardBorderless]等同 nzBorderless 属性booleanfalse
[cardExtra]等同 nzExtra 属性string, TemplateRef<void>-
[cardActions]等同 nzActions 属性Array<TemplateRef>-