List列表

  • 一般由主要信息、主要操作、次要信息、次要操作组成。
  • 主要信息和主要操作放在列表的左边,次要信息和次要操作放在列表的右边。
  1. import { FormGroup, Validators, FormControl } from '@angular/forms';
  2. @Component({
  3. selector: 'demo-list-form',
  4. template: `
  5. <form [formGroup]="registerForm">
  6. <List [renderHeader]="renderHeader" [renderFooter]="renderFooter">
  7. <InputItem
  8. [placeholder]="'please input account'"
  9. [error]="isError"
  10. [focus]="onFocus"
  11. [value]="formData.username"
  12. [clear]="true"
  13. [content]="'Account'"
  14. formControlName="username"
  15. id="username"
  16. (onErrorClick)="inputErrorClick($event)"
  17. (onChange)="inputChange($event)"
  18. >
  19. </InputItem>
  20. <InputItem
  21. id="password"
  22. formControlName="password"
  23. [placeholder]="'please input password'"
  24. [type]="'password'"
  25. [(ngModel)]="formData.password"
  26. [value]="formData.password"
  27. [content]="'Password'"
  28. >
  29. </InputItem>
  30. <ListItem [extra]="_switch">Confirm Infomation</ListItem>
  31. <ListItem>
  32. <Range
  33. style="padding: 7px"
  34. [defaultValue]="[20, 80]"
  35. [min]="0"
  36. [max]="100"
  37. (onAfterChange)="afterChange($event)"
  38. ></Range>
  39. </ListItem>
  40. <ListItem [extra]="_stepper">Number of Subscribers</ListItem>
  41. <ListItem>
  42. <a Button [type]="'primary'" [size]="'small'" [inline]="true" (onClick)="onSubmit()">
  43. Submit
  44. </a>
  45. <a Button style="margin-left: 2.5px;" [size]="'small'" [inline]="true" (onClick)="onReset()">
  46. Reset
  47. </a>
  48. </ListItem>
  49. </List>
  50. </form>
  51. <ng-template #_switch>
  52. <Switch class="oneui-mobile" [checked]="true" (onChange)="switchCheck($event)"></Switch>
  53. </ng-template>
  54. <ng-template #_stepper>
  55. <Stepper [value]="stepper_value" [min]="1" [showNumber]="true" (onChange)="setpperChange($event)"></Stepper>
  56. </ng-template>
  57. `,
  58. styles: [
  59. `
  60. /deep/ .my-list .spe .am-list-extra {
  61. flex-basis: initial;
  62. }
  63. `
  64. ]
  65. export class DemoListFormComponent implements OnInit {
  66. renderFooter: Function;
  67. registerForm: FormGroup;
  68. stepper_value: number = 20;
  69. isError: boolean = false;
  70. onFocus: object = {
  71. focus: false
  72. };
  73. formErrors: any = {
  74. username: '',
  75. password: ''
  76. };
  77. formData: any = {
  78. username: '',
  79. password: ''
  80. };
  81. validationMessage: any = {
  82. username: {
  83. minlength: 'At least four characters for account',
  84. maxlength: 'At most ten characters for account',
  85. required: 'username requied'
  86. },
  87. password: {}
  88. };
  89. constructor() {}
  90. renderHeader() {
  91. return 'Form Validation';
  92. }
  93. bindRenderFooter() {
  94. return (this.formErrors && this.formErrors['username']) || '';
  95. }
  96. onClick() {
  97. console.log('click');
  98. }
  99. buildForm(): void {
  100. this.registerForm = new FormGroup({
  101. username: new FormControl(this.formData.username, [
  102. Validators.required,
  103. Validators.maxLength(10),
  104. Validators.minLength(5)
  105. ]),
  106. password: new FormControl(this.formData.password, [])
  107. });
  108. this.registerForm.valueChanges.subscribe(data => this.onValueChanged(data));
  109. this.onValueChanged();
  110. }
  111. onValueChanged(data?: any) {
  112. if (!this.registerForm) {
  113. return;
  114. }
  115. const form = this.registerForm;
  116. for (const field in this.formErrors) {
  117. this.formErrors[field] = '';
  118. const control = form.get(field);
  119. if (control && control.dirty && !control.valid) {
  120. const messages = this.validationMessage[field];
  121. for (const key in control.errors) {
  122. this.formErrors[field] += messages[key] + '\n';
  123. }
  124. }
  125. }
  126. beforeSubmit() {
  127. const form = this.registerForm;
  128. for (const field in this.formErrors) {
  129. this.formErrors[field] = '';
  130. const control = form.get(field);
  131. if (control && !control.valid) {
  132. const messages = this.validationMessage[field];
  133. for (const key in control.errors) {
  134. this.formErrors[field] += messages[key] + '\n';
  135. if (field === 'username') {
  136. this.onFocus = {
  137. focus: true
  138. };
  139. }
  140. }
  141. return false;
  142. } else {
  143. return true;
  144. }
  145. }
  146. switchCheck(value) {
  147. console.log('switch status:', value);
  148. }
  149. onSubmit() {
  150. if (this.beforeSubmit()) {
  151. console.log(this.registerForm.value);
  152. this.onReset();
  153. } else {
  154. alert('Validation failed');
  155. }
  156. }
  157. onReset() {
  158. this.registerForm.reset();
  159. this.formData = {
  160. ...{
  161. username: '',
  162. password: ''
  163. }
  164. };
  165. this.isError = false;
  166. }
  167. afterChange(event) {
  168. console.log(event, 'afterChange');
  169. }
  170. inputErrorClick(e) {
  171. alert('At least four charactors for account');
  172. }
  173. inputChange(e) {
  174. if (e.replace(/\s/g, '').length < 5 && e.replace(/\s/g, '').length > 0) {
  175. this.isError = true;
  176. } else {
  177. this.isError = false;
  178. }
  179. this.formData.username = e;
  180. }
  181. setpperChange($event) {
  182. console.log($event, 'change');
  183. }
  184. ngOnInit() {
  185. this.buildForm();
  186. this.renderFooter = this.bindRenderFooter.bind(this);
  187. }
  188. }

API

参数说明类型默认值
[thumb]缩略图(当为 string 类型时作为 img src)string | TemplateRef-
[extra]右边内容string | TemplateRef-
[arrow]箭头方向(右,上,下),如果是empty则存在对应的dom,但是不显示‘horizontal’ | ‘up’ | ‘down’ | ‘empty’-
[align]子元素垂直对齐‘top’ | ‘middle’ | ‘bottom’‘middle’
[error]报错样式,右侧文字颜色变成橙色booleanfalse
[multipleLine]多行booleanfalse
[wrap]是否换行,默认情况下,文字超长会被隐藏,booleanfalse
[platform]设定组件的平台特有样式, 默认为 cross, 是指组件会自动检测设备 UA 应用不同平台的样式‘android’ | ‘ios’ | ‘cross’‘cross’
点击事件的回调函数EventEmitter<void>-