如何从列表中计算总计

问题描述:

我正在手机上创建一个POS应用程序,我有一个问题。我如何从数据库中的项目列表中计算总数? 这里是我的代码如何从列表中计算总计

订单detail.dxview

POSApp.OrderDetail = function (params, viewInfo) { 
    "use strict"; 

    var id = params.id, 
     order = new POSApp.OrderViewModel(), 
     isReady = $.Deferred(), 
     // Item List 
     shouldReload = false, 
     dataSourceObservable = ko.observable(), 
     dataSource; 

    function handleViewShown() { 
     POSApp.db.Orders.byKey(id).done(function (data) { 
      order.fromJS(data); 
      isReady.resolve(); 
     }); 

     // Item List 
     if (!dataSourceObservable()) { 
      dataSourceObservable(dataSource); 
      dataSource.load().always(function() { 
       isReady.resolve(); 
      }); 
     } 
     else if (shouldReload) { 
      refreshList(); 
     } 
     // Item List 
    } 

    // Item List 
    function handleViewDisposing() { 
     POSApp.db.OrderDetails.off("modified", handleOrderDetailsModification); 
    } 

    function handleOrderDetailsModification() { 
     shouldReload = true; 
    } 
    // Item List 

    dataSource = new DevExpress.data.DataSource({ 
     store: POSApp.db.OrderDetails, 
     map: function (item) { 
      return new POSApp.OrderDetailViewModel(item); 
     }, 
     expand: ["Item"], 
     sort: { field: "OrderDetailId", desc: false }, 
     filter: ["OrderId", parseInt(id)] 
    }); 

    POSApp.db.OrderDetails.on("modified", handleOrderDetailsModification); 

    var viewModel = { 
     grandTotal: ko.observable(total), 
     handleDelete: function() { 
      DevExpress.ui.dialog.confirm("Are you sure you want to delete this item?", "Delete item").then(function (result) { 
       if (result) 
        handleConfirmDelete(); 
      }); 
     }, 
     handleConfirmDelete: function() { 
      POSApp.db.Orders.remove(id).done(function() { 
       if (viewInfo.canBack) { 
        POSApp.app.navigate("Orders", { target: "back" }); 
       } 
       else { 
        POSApp.app.navigate("Blank", { target: "current" }); 
       } 
      }); 
     }, 

     //Item List 
     refreshList: function() { 
      shouldReload = false; 
      dataSource.pageIndex(0); 
      dataSource.load(); 
     }, 
     //Item List 

     // Return 
     id: id, 
     order: order, 
     viewShown: handleViewShown, 
     isReady: isReady.promise(), 
     // Item List 
     dataSource: dataSourceObservable, 
     viewDisposing: handleViewDisposing, 
     // Item List 
     // Return 
    }; 

    return viewModel; 
}; 

订单detail.js

<div data-options="dxView : { name: 'OrderDetail', title: 'Order' } " > 
    <div data-bind="dxCommand: { onExecute: '#OrderEdit/{id}', direction: 'none', id: 'edit', title: 'Edit', icon: 'edit' }"></div> 
    <div data-bind="dxCommand: { onExecute: handleDelete, id: 'delete', title: 'Delete', icon: 'remove' }"></div> 
    <div data-options="dxContent : { targetPlaceholder: 'content' } " class="dx-detail-view dx-content-background" data-bind="dxDeferRendering: { showLoadIndicator: true, staggerItemSelector: 'dx-fieldset-header,.dx-field', animation: 'detail-item-rendered', renderWhen: isReady }" > 
     <div data-bind="dxScrollView: { }"> 
      <div class="dx-fieldset"> 
       <div class="dx-fieldset-header" data-bind="text: order.PhoneNumber"></div> 
       <div class="dx-field"> 
        <div class="dx-field-label">Order id</div> 
        <div class="dx-field-value-static" data-bind="text: order.OrderId"></div> 
       </div> 
       <div class="dx-field"> 
        <div class="dx-field-label">Phone number</div> 
        <div class="dx-field-value-static" data-bind="text: order.PhoneNumber"></div> 
       </div> 
       <div class="dx-field"> 
        <div class="button-info" data-bind="dxButton: { text: 'Add Item', onClick: '#AddItem/{id}', icon: 'add', type: 'success' }"></div> 
        <!-- Item List --> 
        <div data-bind="dxList: { dataSource: dataSource, pullRefreshEnabled: true }"> 
         <div data-bind="dxAction: '#OrderDetailDetails/{OrderDetailId}'" data-options="dxTemplate : { name: 'item' } "> 
          <!--<div class="list-item" data-bind="text: Item().ItemName"></div> 
          <div class="list-item" style="float:right;" data-bind="text: Amount"></div>--> 
          <div class="item-name" data-bind="text: Item().ItemName"></div> 
          <div class="item-amount" data-bind="text: Amount"></div> 
          <div class="clear-both"></div> 
         </div> 
        </div> 
       </div> 
       <div class="dx-field"> 
        <div class="dx-field-label">Grand total</div> 
        <!--<div class="dx-field-value-static" data-bind="text: order.GrandTotal"></div>--> 
        <div class="dx-field-value-static" data-bind="text: grandTotal"></div> 
       </div> 
      </div> 
      <div data-options="dxContentPlaceholder : { name: 'view-footer', animation: 'none' } " ></div> 
     </div> 
    </div> 
</div> 

我已经通过类名和使用get元素尝试它仍然不起作用。

+0

总计应该是多少? – afuous

+0

这是总额或金额的总和,这里是我的插图和我的项目看起来像http://imgur.com/18V1xjZ @afuous –

在此,源是可观察到的敲除阵列和dxList数据源。总计值存储在“总计”变量中,这是根据“来源”计算出的可观察值。所以,一旦“源”发生变化,“总数”也会重新计算。

var grandTotal = ko.observable(0); 
dataSource = new DevExpress.data.DataSource({ 
    // ... 
    onChanged: function() { 
     grandTotal(0); 
     var items = dataSource.items(); 
     for (var i = 0; i < items.length; i++) { 
      grandTotal(grandTotal() + items[i].Amount()); 
     } 
    } 
}); 


return { 
    // ... 
    grandTotal: grandTotal 
}; 

我已经尝试通过类名称使用获取元素,它仍然无法正常工作。

您不应该尝试从您的视图中获取数据;它已经在你的viewmodel中!

This documentation page告诉我,通过调用items方法,您可以从DataSource实例中获得一系列物品。

从数据源的map功能,您text: Amount数据绑定,我想每一个项目可能有一个Amount财产持有一个整数。

grandTotal可以是一个计算,增加了这些值一起,每当dataSourceObservable变化:

grandTotal: ko.computed(function() { 
    var total = 0; 
    var currentDS = dataSourceObservable(); 

    if (currentDS) { 
    var currentItems = currentDS.items(); 
    total = currentItems.reduce(function(sum, item) { 
     return sum + item.Amount; 
    }, total); 
    } 

    return total; 
}); 
+0

计算没有奏效,所以它给了我0 –

+0

你必须描述什么是发生一点对我来说能够帮助你更好。是否定义了'dataSourceObservable'? '.items()'返回什么? – user3297291

+0

if语句中的currentDS是未定义的,因为.items()也未定义 –