Skip to content

Commit c7d8938

Browse files
author
calvin
committed
docs(sample-code): add sample code for table and form
1 parent 12a673a commit c7d8938

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

frontend/angular-best-practices.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,79 @@ createForm() {
136136
}
137137
```
138138
139+
### 普通表单
140+
141+
对于一些有一定交互的表单,我们建议使用普通的表单去构建,通过在对应的`component`里面去写交互逻辑,推荐使用ng-alain提供的`se`组件,需要注意的是`se`组件需要搭配指令`se-container`使用,否则会在运行时报错。
142+
143+
`se`的用法如下:
144+
145+
```ts
146+
import { Component } from '@angular/core';
147+
148+
@Component({
149+
selector: 'components-edit-horizontal',
150+
template: `
151+
<form nz-form #f="ngForm" se-container="1" labelWidth="150">
152+
<se label="App Key" error="请填写" optional="(选填)" optionalHelp="通过控制台-查看KEY获取" extra="额外提示信息">
153+
<input type="text" nz-input [(ngModel)]="i.ak" name="ak" required>
154+
</se>
155+
<se label="App Secret" error="请填写,最多32位">
156+
<input type="text" nz-input [(ngModel)]="i.sk" name="sk" required maxlength="32">
157+
</se>
158+
<se>
159+
<button nz-button nzType="primary" [disabled]="f.invalid">Save</button>
160+
</se>
161+
</form>`,
162+
})
163+
export class ComponentsEditHorizontalComponent {
164+
i: any = {};
165+
}
166+
```
167+
168+
### 动态表单
169+
170+
> [参考资料](https://ng-alain.com/form/getting-started/zh)
171+
172+
对于后台系统的查询页面而言,大部分都是比较规整,为了减少重复编写模板代码,我们建议采用动态表单,详细资料可以参考angular[官方文档](https://angular.cn/guide/dynamic-form)和[ng-alain](https://ng-alain.com/form/getting-started/zh)。因为我们决定大量使用ng-alain所带来的特性,同样我们也决定采用有它提供的基于[JSON Schema](http://json-schema.org/)标准构建的动态表单
173+
174+
**基本原则:**
175+
176+
没有复杂交互的表单都可以采用这种动态表单,如果涉及复杂的交互建议还是使用普通的表单,另外,禁止在动态表单中去写`if...else...`或者其他逻辑性代码,只能写声明式代码。
177+
178+
用法如下:
179+
180+
```ts
181+
@Component({
182+
selector: 'app-home',
183+
template: `
184+
<sf [schema]="schema" (formSubmit)="submit($event)"></sf>
185+
`
186+
})
187+
export class HomeComponent {
188+
schema: SFSchema = {
189+
properties: {
190+
email: {
191+
type: 'string',
192+
title: '邮箱',
193+
format: 'email',
194+
maxLength: 20
195+
},
196+
name: {
197+
type: 'string',
198+
title: '姓名',
199+
minLength: 3
200+
}
201+
}
202+
};
203+
204+
submit(value: any) {
205+
206+
}
207+
}
208+
```
209+
210+
211+
139212
一些建议:
140213
141214
- 针对复杂表单(表单项的验证规则是可变的情况),使用 `from.valid/invalid` 判断时,
@@ -147,6 +220,51 @@ createForm() {
147220
- 如果一个验证规则被重复多次使用,请使用类似 `const pattern = Validators.pattern(moneyRegex)` ,让代码变得更简洁.
148221
- 表单 `control` 统一使用 `touched` 进行错误提示。
149222
223+
## 表格
224+
225+
表格是后台经常会使用的功能,为了简化表格的写法,增加可维护性,我们推荐使用ng-alain提供的[st组件](https://ng-alain.com/components/table/zh)
226+
227+
简单示例如下:
228+
229+
```ts
230+
import { Component } from '@angular/core';
231+
import { STColumn } from '@delon/abc';
232+
233+
@Component({
234+
selector: 'components-table-basic',
235+
template: `
236+
<st [data]="url" [req]="{params: params}" [columns]="columns"></st>`,
237+
})
238+
export class ComponentsTableBasicComponent {
239+
url = `/users?total=2&field=list`;
240+
params = { a: 1, b: 2 };
241+
columns: STColumn[] = [
242+
{ title: '编号', index: 'id' },
243+
{ title: '头像', type: 'img', width: '50px', index: 'picture.thumbnail' },
244+
{ title: '邮箱', index: 'email' },
245+
{ title: '电话', index: 'phone' },
246+
{ title: '注册时间', type: 'date', index: 'registered' },
247+
];
248+
}
249+
```
250+
251+
表格中需要用到的CheckBox,RadioButton, 点击, 分页等等都由`change`事件统一管理,通过`eventType`去判断对应的类型,切记组件里面要去定义change的处理回调把一些不需要的事件排除,避免发不需要的请求。
252+
253+
示例如下:
254+
255+
```ts
256+
class DemoTable {
257+
onChange(e: STChange) {
258+
const eventType = e.eventType
259+
if(eventType === 'ps' || eventType === 'pi') {
260+
this.query()
261+
} else if(eventType === 'checkbox') {
262+
// do something
263+
}
264+
}
265+
}
266+
```
267+
150268
## Dependency Injection
151269
152270
- Use `providedIn: 'root'` for all services, either global or local, which should be available as singletons.

0 commit comments

Comments
 (0)