Javascript 如何以角度过滤表(使用管道比较转换的数据)?
我正在尝试使用搜索框和自定义管道以角度过滤表。 表中使用管道显示了一些值,如:Javascript 如何以角度过滤表(使用管道比较转换的数据)?,javascript,angular,filter,html-table,ng-bootstrap,Javascript,Angular,Filter,Html Table,Ng Bootstrap,我正在尝试使用搜索框和自定义管道以角度过滤表。 表中使用管道显示了一些值,如: {{order.tot | currency: 'EUR':'symbol':'.2-2':'it' }} {{order.datePayment | date: 'dd/MM/yyyy hh:mm:ss' }} 此处显示自定义管道: export class SearchFilterOrdersPipe implements PipeTransform { transform(orders, searc
{{order.tot | currency: 'EUR':'symbol':'.2-2':'it' }}
{{order.datePayment | date: 'dd/MM/yyyy hh:mm:ss' }}
此处显示自定义管道:
export class SearchFilterOrdersPipe implements PipeTransform {
transform(orders, searchValue: string): unknown {
if (!orders || !searchValue) {
return orders;
}
searchValue = searchValue.toLocaleLowerCase();
return orders.filter(order =>
order.idOrder.toString().includes(searchValue) ||
order.email.toLocaleLowerCase().includes(searchValue) ||
order.invoice.toLocaleLowerCase().includes(searchValue) ||
// order.datePayment.toString().includes(searchValue) ||
order.paypalPaymentId.toLocaleLowerCase().includes(searchValue) ||
// order.tot.toString().includes(searchValue) ||
order.status.includes(searchValue)
)
}
}
对于表中的所有字符串值,我没有任何问题。但“tot”和“datePayment”最初的格式是:
- 订单日期付款日期:2021-01-13T09:27:11.000Z
- 订单总数:100或15.50或20
THX.html中的管道仅用于显示,因此您无法在管道中访问它。可以注入管道并再次转换它们:
export class SearchFilterOrdersPipe implements PipeTransform {
constructor(private date: DatePipe, private curreny: CurrencyPipe) {
}
transform(orders, searchValue: string): unknown {
if (!orders || !searchValue) {
return orders;
}
searchValue = searchValue.toLocaleLowerCase();
return orders.filter(order =>
this.date.transform(order.datePayment, 'dd/MM/yyyy hh:mm:ss').includes(searchValue) ||
this.currency.transform(order.tot, 'EUR', 'symbol', '.2-2', 'it').includes(searchValue) ||
...
)
}
}
Thx@Felix,它可以工作,但我还必须在app.module.ts中添加“提供者”DatePipe和CurrencyPipe(可能对其他人有帮助)