|
| 1 | +# 一个简单查询展示页面 |
| 2 | + |
| 3 | +后台大部分页面都是简单查询展示页面,样式风格也是统一的,为了简化后台的开发,故做此简单的约定 |
| 4 | + |
| 5 | +## 页面布局 |
| 6 | + |
| 7 | +页面分 3 个部分:头部是一个页面名称,中部为搜索条件表单,下部分为数据展示表格,完整示例代码如下: |
| 8 | + |
| 9 | +```html |
| 10 | +<page-header title="企业账单" class="page-header-small"></page-header> |
| 11 | + |
| 12 | +<form nz-form [formGroup]="form" class="form-small"> |
| 13 | + <div shf-wrap="4" labelWidth="150"> |
| 14 | + <shf-item label="出账日开始日期"> |
| 15 | + <nz-date-picker formControlName="billDateStart"> </nz-date-picker> |
| 16 | + </shf-item> |
| 17 | + <shf-item label="出账日结束日期"> |
| 18 | + <nz-date-picker formControlName="billDateEnd"> </nz-date-picker> |
| 19 | + </shf-item> |
| 20 | + <shf-item label="客户简称"> |
| 21 | + <input nz-input formControlName="corpNameLike" /> |
| 22 | + </shf-item> |
| 23 | + <shf-item label="状态"> |
| 24 | + <shared-dict-select |
| 25 | + dictType="CorpCreditBillStatus" |
| 26 | + formControlName="status" |
| 27 | + > |
| 28 | + </shared-dict-select> |
| 29 | + </shf-item> |
| 30 | + <shf-item> |
| 31 | + <button |
| 32 | + nz-button |
| 33 | + nzType="primary" |
| 34 | + (click)="simpleTable.reset()" |
| 35 | + [disabled]="isSpinning$ | async" |
| 36 | + > |
| 37 | + <!-- 如果不是后台分页的 data 请改为 (click)="query()" --> |
| 38 | + 查询 |
| 39 | + </button> |
| 40 | + <button nz-button type="reset">重置</button> |
| 41 | + </shf-item> |
| 42 | + </div> |
| 43 | +</form> |
| 44 | + |
| 45 | +<nz-spin [nzTip]="[nzTip]" [nzSpinning]="isSpinning$ | async"> |
| 46 | + <simple-table |
| 47 | + class="table-text-center table-small" |
| 48 | + showSizeChanger |
| 49 | + showPagination |
| 50 | + showTotal |
| 51 | + showQuickJumper |
| 52 | + [bordered]="true" |
| 53 | + [frontPagination]="false" |
| 54 | + (change)="query()" |
| 55 | + [data]="data" |
| 56 | + [columns]="columns" |
| 57 | + > |
| 58 | + <!-- |
| 59 | + 如果不是后台分页的 data 请去除 [frontPagination]="false" (change)="query()" |
| 60 | + --> |
| 61 | + <ng-template st-row="billDate" let-i> |
| 62 | + <span> {{ i.billDate | date: 'yyyy-MM-dd' }} </span> |
| 63 | + </ng-template> |
| 64 | + <ng-template st-row="status" let-i> |
| 65 | + <span> {{ i.status | dictValueTransform: 'CorpCreditBillStatus' }} </span> |
| 66 | + </ng-template> |
| 67 | + </simple-table> |
| 68 | +</nz-spin> |
| 69 | +``` |
| 70 | + |
| 71 | +**_注意:_** |
| 72 | + |
| 73 | +- `shf-wrap="4" labelWidth="150"` 根据实际原型调整 |
| 74 | +- 统一使用 ReactiveForms 禁止使用 Template-driven forms |
| 75 | +- 若操作按钮过多,建议另起一行 用 nz-row 包裹 |
| 76 | + |
| 77 | +## 页面样式 |
| 78 | + |
| 79 | +为了保持系统的整体性和统一行,非特殊的查询展示页统一使用: `class="page-header-small"` `class="form-small"` `class="table-text-center table-small"` |
| 80 | +以上样式定义在 index.less 中 便于统一修改 |
| 81 | +**_注意_**:本样式只适用于后台页面,前台请自行定义 |
| 82 | + |
| 83 | +```less |
| 84 | +.page-header-small { |
| 85 | + padding: 6px 24px 0px 24px; |
| 86 | + margin-bottom: 6px; |
| 87 | +} |
| 88 | + |
| 89 | +.page-header-small.ad-ph .title, |
| 90 | +.page-header-small.ad-ph .action { |
| 91 | + margin-bottom: 6px; |
| 92 | +} |
| 93 | + |
| 94 | +.form-small { |
| 95 | + border: 1px solid #d9d9d9; |
| 96 | + border-radius: 4px; |
| 97 | + padding: 6px; |
| 98 | + margin-bottom: 6px; |
| 99 | + |
| 100 | + .ant-input { |
| 101 | + padding: 1px 7px; |
| 102 | + height: 24px; |
| 103 | + } |
| 104 | + |
| 105 | + .ant-form-item { |
| 106 | + margin-bottom: 0; |
| 107 | + } |
| 108 | + |
| 109 | + .ant-select-selection--multiple, |
| 110 | + .ant-select-selection--single { |
| 111 | + height: 24px; |
| 112 | + } |
| 113 | + |
| 114 | + .ant-select-selection__rendered { |
| 115 | + line-height: 22px; |
| 116 | + margin: 0 7px; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +.table-text-center { |
| 121 | + table th, |
| 122 | + table td { |
| 123 | + text-align: center !important; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +.table-small { |
| 128 | + table th, |
| 129 | + table td { |
| 130 | + padding: 2px; |
| 131 | + height: 47px; |
| 132 | + } |
| 133 | + table td { |
| 134 | + background-color: white; |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## 基本逻辑代码 |
| 140 | + |
| 141 | +```ts |
| 142 | +export class BillListComponent implements OnInit { |
| 143 | + isSpinning$ = new BehaviorSubject<boolean>(false); |
| 144 | + nzTip: string; |
| 145 | + form: FormGroup; |
| 146 | + |
| 147 | + @ViewChild(SimpleTableComponent) |
| 148 | + simpleTable: SimpleTableComponent; |
| 149 | + columns: SimpleTableColumn[] = [ |
| 150 | + { title: "出账日", render: "billDate" }, |
| 151 | + { title: "客户简称", index: "corpShortName" }, |
| 152 | + { title: "账单起始日", render: "billStartDate" }, |
| 153 | + { title: "是否逾期", render: "overdue" }, |
| 154 | + { title: "状态", render: "status" }, |
| 155 | + { |
| 156 | + title: "操作", |
| 157 | + buttons: [ |
| 158 | + { |
| 159 | + text: "详情", |
| 160 | + click: (record: BillSummaryResponse) => { |
| 161 | + this.log.debug(record); |
| 162 | + this.routerService.toDetail(record.billId); |
| 163 | + } |
| 164 | + } |
| 165 | + ] |
| 166 | + } |
| 167 | + ]; |
| 168 | + data: BillSummaryResponse[]; |
| 169 | + |
| 170 | + private log: Logger; |
| 171 | + constructor( |
| 172 | + loggerService: LoggerService, |
| 173 | + private fb: FormBuilder, |
| 174 | + private httpService: BillListHttpService, |
| 175 | + private routerService: BillListRouterService, |
| 176 | + private handleErrorService: HandleErrorService |
| 177 | + ) { |
| 178 | + this.log = loggerService.getLogger("BillListComponent"); |
| 179 | + } |
| 180 | + |
| 181 | + ngOnInit() { |
| 182 | + this.form = this.initForm(); |
| 183 | + // 默认加载数据 |
| 184 | + this.query(); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * 分页获取企业账单数据 |
| 189 | + * |
| 190 | + * @memberof BillListComponent |
| 191 | + */ |
| 192 | + query() { |
| 193 | + const methodName = "query"; |
| 194 | + const params = this.getParams(); |
| 195 | + this.showSpin("查询中,请稍候..."); |
| 196 | + this.httpService.getCreditBills(params).subscribe( |
| 197 | + res => { |
| 198 | + this.hideSpin(); |
| 199 | + if (res instanceof AppError) { |
| 200 | + this.handleErrorService.handleAppError(this.log, res, methodName); |
| 201 | + } else { |
| 202 | + this.simpleTable.total = res.totalElements; |
| 203 | + this.data = res.content; |
| 204 | + } |
| 205 | + }, |
| 206 | + err => { |
| 207 | + this.hideSpin(); |
| 208 | + this.handleErrorService.handleAppError(this.log, err, methodName); |
| 209 | + } |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * 组装查询的参数 |
| 215 | + * |
| 216 | + * @private |
| 217 | + * @returns {BillSearchRequest} |
| 218 | + * @memberof BillListComponent |
| 219 | + */ |
| 220 | + private getParams(): BillSearchRequest { |
| 221 | + const methodName = "getParams"; |
| 222 | + const params: BillSearchRequest = { |
| 223 | + ...this.form.value, |
| 224 | + ...this.formatDate(), |
| 225 | + pageNumber: this.simpleTable.pi, |
| 226 | + pageSize: this.simpleTable.ps |
| 227 | + }; |
| 228 | + this.log.debug(methodName, params); |
| 229 | + return params; |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * 起始时间 是 00:00:00 |
| 234 | + * 截止时间是 +1天的 00:00:00 |
| 235 | + * |
| 236 | + * @private |
| 237 | + * @returns {BillSearchRequest} |
| 238 | + * @memberof BillListComponent |
| 239 | + */ |
| 240 | + private formatDate(): BillSearchRequest { |
| 241 | + const formValue = this.form.value; |
| 242 | + |
| 243 | + const billDateStart = formValue.billDateStart |
| 244 | + ? DateUtils.startOfDay(formValue.billDateStart) |
| 245 | + : undefined; |
| 246 | + const billDateEnd = formValue.billDateEnd |
| 247 | + ? DateUtils.addDays(DateUtils.startOfDay(formValue.billDateEnd), 1) |
| 248 | + : undefined; |
| 249 | + |
| 250 | + return { |
| 251 | + billDateStart, |
| 252 | + billDateEnd |
| 253 | + }; |
| 254 | + } |
| 255 | + |
| 256 | + private initForm() { |
| 257 | + return this.fb.group({ |
| 258 | + billDateStart: [], |
| 259 | + billDateStart: [], |
| 260 | + corpNameLike: [], |
| 261 | + status: [] |
| 262 | + }); |
| 263 | + } |
| 264 | + |
| 265 | + private showSpin(nzTip: string) { |
| 266 | + this.nzTip = nzTip; |
| 267 | + this.isSpinning$.next(true); |
| 268 | + } |
| 269 | + |
| 270 | + private hideSpin() { |
| 271 | + this.isSpinning$.next(false); |
| 272 | + } |
| 273 | +} |
| 274 | +``` |
0 commit comments