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

API

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