最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In Knockout.js, how to sort viewModel item based on checkbox field after mapping - Stack Overflow

programmeradmin1浏览0评论

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. (.html)

Any example of how to sort an array after ko.mapping.fromJS will be appreciated!

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./documentation/observableArrays.html)

Any example of how to sort an array after ko.mapping.fromJS will be appreciated!

Share Improve this question edited Jan 27, 2012 at 6:18 bcm asked Jan 27, 2012 at 4:51 bcmbcm 5,50010 gold badges60 silver badges92 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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.

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论