import { Component, OnInit, TemplateRef, ViewChild, inject } from '@angular/core';
import { of, delay } from 'rxjs';
import { STChange, STColumn, STColumnFilter, STColumnFilterHandle, STData, STModule } from '@yelon/abc/st';
import { dateTimePickerUtil } from '@yelon/util/date-time';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
selector: 'components-st-custom-data',
template: `
<div class="mb-md">
<button (click)="st.clear()" nz-button>Clear Data</button>
<button (click)="st.reload()" nz-button>Reload Data</button>
<button (click)="st.clearStatus(); st.reload()" nz-button>Clear Status</button>
<button (click)="st.setRow(0, { className: 'text-success' })" nz-button>Update Row ClassName</button>
</div>
<st #st [data]="users" [columns]="columns" (change)="change($event)" />
<ng-template #customFilter let-f let-col="col" let-handle="handle">
<div class="p-lg">
<p>Custom body</p>
<button nz-button nzType="primary" (click)="close(f, handle, true)">confirm</button>
<button nz-button (click)="close(f, handle, false)">reset</button>
</div>
</ng-template>
`,
standalone: true,
imports: [STModule, NzButtonModule]
})
export class ComponentsStCustomDataComponent implements OnInit {
private readonly msg = inject(NzMessageService);
users: STData[] = [];
@ViewChild('customFilter', { static: true }) readonly customFilter!: TemplateRef<{
$implicit: STColumnFilter;
col: STColumn;
handle: STColumnFilterHandle;
}>;
columns: STColumn[] = [];
ngOnInit(): void {
this.columns = [
{
title: '编号',
index: 'id',
type: 'checkbox',
selections: [
{
text: '小于25岁',
select: data => data.forEach(item => (item.checked = item.age < 25))
},
{
text: '大于25岁',
select: data => data.forEach(item => (item.checked = item.age >= 25))
}
]
},
{
title: '姓名',
index: 'name',
sort: {
compare: (a, b) => a.name.length - b.name.length
},
filter: {
type: 'keyword',
placeholder: '输入后按回车搜索',
fn: (filter, record) => !filter.value || record.name.indexOf(filter.value) !== -1
}
},
{
title: '年龄',
index: 'age',
sort: {
compare: (a, b) => a.age - b.age
},
filter: {
type: 'number',
placeholder: '范围 10 ~ 100 之间',
number: {
min: 10,
max: 100
},
fn: (filter, record) => (filter.value != null ? record.age >= +filter.value : true)
}
},
{
title: '状态',
type: 'badge',
index: 'status',
badge: {
1: { text: 'Success', color: 'success' },
2: { text: 'Error', color: 'error' },
3: { text: 'Processing', color: 'processing' },
4: { text: 'Default', color: 'default' },
5: { text: 'Warning', color: 'warning' }
},
filter: {
menus: [
{ text: 'Success', value: 1 },
{ text: 'Error', value: 2 }
],
fn: (filter, record) => record.age >= filter.value[0] && record.age <= filter.value[1],
multiple: true
}
},
{
title: 'Date',
index: 'created',
type: 'date',
filter: {
type: 'date',
date: {
mode: 'date',
showToday: false,
disabledDate: dateTimePickerUtil.disabledAfterDate()
},
fn: () => true
}
},
{
title: 'Custom',
index: 'name',
filter: {
type: 'custom',
icon: { type: 'search', theme: 'outline' },
custom: this.customFilter,
showOPArea: false,
fn: (filter, record) => !filter.value || record.name.indexOf(filter.value) !== -1
}
}
];
const data = Array(100)
.fill({})
.map((_, idx) => ({
id: idx + 1,
name: `name ${idx + 1}`,
age: Math.ceil(Math.random() * 10) + 20,
status: Math.floor(Math.random() * 5) + 1,
created: new Date()
}));
of(data)
.pipe(delay(500))
.subscribe(res => (this.users = res));
}
close(f: STColumnFilter, handle: STColumnFilterHandle, result: boolean): void {
this.msg.info(`Process result: ${result}${result ? '(only name 2)' : ''}`);
f.menus = [{ value: result ? 'name 2' : null }];
handle.close(result);
}
change(e: STChange): void {
console.log(e);
}
}