在 Knockout.js 中,如何在映射后根据复选框字段对 viewModel 项进行排序
数据示例,其中 VoucherRedeemed
是一个 checkbox
值:
{
VoucherTitle: "title 2",
VoucherRedeemed: false,
},{
VoucherTitle: "title 4",
VoucherRedeemed: true,
},{
VoucherTitle: "title 6",
VoucherRedeemed: false,
},{
VoucherTitle: "title 9",
VoucherRedeemed: true,
}
映射后,如何根据复选框值对 viewModel.voucher 进行排序:
viewModel.voucher = ko.mapping.fromJS(voucherData.voucher)
我已经了解了对可观察值进行排序数组,但未能与复选框值排序的映射场景联系起来。 (http://knockoutjs.com/documentation/observableArrays.html)
有关如何排序的任何示例ko.mapping.fromJS 之后的数组将不胜感激!
Data example, where VoucherRedeemed
is a checkbox
value:
{
VoucherTitle: "title 2",
VoucherRedeemed: false,
},{
VoucherTitle: "title 4",
VoucherRedeemed: true,
},{
VoucherTitle: "title 6",
VoucherRedeemed: false,
},{
VoucherTitle: "title 9",
VoucherRedeemed: true,
}
How do I sort the viewModel.voucher based on the checkbox value, after mapping:
viewModel.voucher = ko.mapping.fromJS(voucherData.voucher)
I've had a look at sorting observable arrays, but did not manage to tie up to the mapping scenario for checkbox value sorting. (http://knockoutjs.com/documentation/observableArrays.html)
Any example of how to sort an array after ko.mapping.fromJS will be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
填充视图模型后,您可以进行排序。
看看我在这里整理的小提琴淘汰排序示例
您需要在可观察到的对象上调用排序方法,传入一个功能对您想要的属性进行排序。
You can sort once you have populated the view model.
Have a look at the fiddle I have put together here Knockout Sort Example
You need to call the sort method on the observable passing in a function to sort on the property you want.
我估计。
在将凭证数据映射到 viewModel 之前对其进行排序。一旦进入视图模型,不确定是否可以或容易进行任何排序。
I figured.
Sort the voucherData before mapping it into the viewModel. Once it is in the viewModel, not sure if it is possible or easy to do any sorting.