Knockout可观察数组排序无法正常工作

时间:2018-06-13 10:11:59

标签: knockout.js

我尝试使用常用方法对Knockout数组进行排序,但它不起作用。我想按totalTTC值排序。以下是视图模型中的元素:

appViewModel.orders = ko.computed(() -> (
  orders = []
  # Check if seller of this shoppingCartLine exists
  appViewModel.shoppingCartLines().forEach((shoppingCartLine) ->
  existingOrder = null
  orders.forEach((order) ->
    if order.seller? and order.seller.id() ==    shoppingCartLine.ad.ownerJson.id()
      existingOrder = order
  )
  # If it exits add it to its shoppingCartLines array
  if existingOrder?
    existingOrder.shoppingCartLines.push(shoppingCartLine)
    return
  # Otherwise create a new order from this shoppingCartLine
  else
    seller = shoppingCartLine.ad.ownerJson
    shoppingCartLines = ko.observableArray([shoppingCartLine])
    order = new Order(seller, shoppingCartLines, appViewModel.freeShippingFeesThreshold)
    orders.push(order)
    return
  )
  return orders
))

以下是我的观点:

<td data-bind="foreach: {data: orders().sort(function (o, r) { return o.totalTTC() > r.totalTTC() ? 1 : -1 }), as: 'order'}">

有人看到了什么问题吗?感谢

0 个答案:

没有答案