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

Data example, where VoucherRedeemed is a checkbox value:{VoucherTitle: "title 2",VoucherRedee

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744985920a4604606.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信