Skip to content

Commit

Permalink
fix(abc:st): fix can't render when data changed (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Nov 24, 2021
1 parent b9ea6cd commit 8151a53
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
23 changes: 15 additions & 8 deletions packages/abc/st/demo/checkbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ Use `change` event get selected data.

```ts
import { Component } from '@angular/core';

import { STChange, STColumn, STData } from '@delon/abc/st';

@Component({
selector: 'app-demo',
template: `<st
[data]="url"
[columns]="columns"
[req]="{ params: params }"
[res]="{ process: dataProcess }"
(change)="change($event)"
></st>`,
template: ` <div class="mb-md">
<button nz-button (click)="st.checkAll(true)">All</button>
<button nz-button (click)="st.clearCheck()">Clean</button>
</div>
<st
#st
[data]="url"
[columns]="columns"
[req]="{ params: params }"
[res]="{ process: dataProcess }"
(change)="change($event)"
></st>`
})
export class DemoComponent {
url = `/users?total=100`;
Expand All @@ -35,14 +41,15 @@ export class DemoComponent {
{ title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' },
{ title: '邮箱', index: 'email' },
{ title: '电话', index: 'phone' },
{ title: '注册时间', type: 'date', index: 'registered' },
{ title: '注册时间', type: 'date', index: 'registered' }
];
change(e: STChange): void {
console.log('change', e);
}
dataProcess(data: STData[]): STData[] {
return data.map((i, index) => {
i.disabled = index === 0;
if (index === 1) i.checked = true;
return i;
});
}
Expand Down
24 changes: 15 additions & 9 deletions packages/abc/st/demo/radio.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@ Use `change` event get selected data.

```ts
import { Component } from '@angular/core';

import { STChange, STColumn, STData } from '@delon/abc/st';

@Component({
selector: 'app-demo',
template: `<st
[data]="url"
[columns]="columns"
[req]="{ params: params }"
[res]="{ process: dataChange }"
(change)="change($event)"
></st>`,
template: ` <div class="mb-md">
<button nz-button (click)="st.setRow(1, { checked: true })">Radio second</button>
<button nz-button (click)="st.clearRadio()">Clean</button>
</div>
<st
#st
[data]="url"
[columns]="columns"
[req]="{ params: params }"
[res]="{ process: dataChange }"
(change)="change($event)"
></st>`
})
export class DemoComponent {
url = `/users?total=300`;
params = { a: 1, b: 2 };
columns: STColumn[] = [
{ title: '编号', index: 'id', type: 'radio' },
{ title: '编号', index: 'id', type: 'radio', width: 70 },
{ title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' },
{ title: '邮箱', index: 'email' },
{ title: '电话', index: 'phone' },
{ title: '注册时间', type: 'date', index: 'registered' },
{ title: '注册时间', type: 'date', index: 'registered' }
];

change(ret: STChange): void {
Expand Down
8 changes: 4 additions & 4 deletions packages/abc/st/st-td.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DrawerHelper, ModalHelper } from '@delon/theme';
import { deepMergeKey } from '@delon/util/other';
import { NzSafeAny } from 'ng-zorro-antd/core/types';

import { STComponent } from '.';
import { STComponent } from './st.component';
import { STColumnButton, STData } from './st.interfaces';
import { _STColumn, _STTdNotify, _STTdNotifyType } from './st.types';

Expand Down Expand Up @@ -83,7 +83,7 @@ import { _STColumn, _STTdNotify, _STTdNotifyType } from './st.types';
nz-radio
[nzDisabled]="i.disabled"
[ngModel]="i.checked"
(ngModelChange)="_radio($event)"
(ngModelChange)="_radio()"
></label>
<a
*ngSwitchCase="'link'"
Expand Down Expand Up @@ -171,9 +171,9 @@ export class STTdComponent {
this.report('checkbox');
}

_radio(checked: boolean): void {
_radio(): void {
this.data.filter(w => !w.disabled).forEach(i => (i.checked = false));
this.i.checked = checked;
this.i.checked = true;
this.report('radio');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/abc/st/st.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[nzDisabled]="_allCheckedDisabled"
[(ngModel)]="_allChecked"
[nzIndeterminate]="_indeterminate"
(ngModelChange)="_checkAll()"
(ngModelChange)="checkAll()"
[class.ant-table-selection-select-all-custom]="custom"
></label>
</ng-template>
Expand Down
19 changes: 12 additions & 7 deletions packages/abc/st/st.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
return this;
}

private refreshData(): this {
this._data = [...this._data];
return this.cd();
}

renderTotal(total: string, range: string[]): string {
return this.totalTpl
? this.totalTpl.replace('{{total}}', total).replace('{{range[0]}}', range[0]).replace('{{range[1]}}', range[1])
Expand Down Expand Up @@ -562,7 +567,7 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
})
);

return this.cd();
return this.refreshData();
}

/**
Expand All @@ -588,8 +593,7 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
this.resetColumns({ emitReload: options.emitReload });
return this;
}
this.cdr.detectChanges();
return this;
return this.refreshData();
}

// #endregion
Expand Down Expand Up @@ -647,7 +651,7 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {

/** 清除所有 `checkbox` */
clearCheck(): this {
return this._checkAll(false);
return this.checkAll(false);
}

private _refCheck(): this {
Expand All @@ -660,10 +664,10 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
return this.cd();
}

_checkAll(checked?: boolean): this {
checkAll(checked?: boolean): this {
checked = typeof checked === 'undefined' ? this._allChecked : checked;
this._data.filter(w => !w.disabled).forEach(i => (i.checked = checked));
return this._refCheck()._checkNotify();
return this._refCheck()._checkNotify().refreshData();
}

_rowSelection(row: STColumnSelection): this {
Expand All @@ -685,7 +689,7 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
clearRadio(): this {
this._data.filter(w => w.checked).forEach(item => (item.checked = false));
this.changeEmit('radio', null);
return this;
return this.refreshData();
}

// #endregion
Expand All @@ -697,6 +701,7 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
break;
case 'radio':
this.changeEmit('radio', ev.item);
this.refreshData();
break;
}
}
Expand Down

0 comments on commit 8151a53

Please sign in to comment.