2011-05-23 89 views
0

嗨我做了一個產品名稱和價格列表是一個面板的項目面板有一個dockeditem這是一個工具欄。我想要工具欄內的價格總和,以便人們可以看到總價格。總價值Sencha Touch

我的代碼:

new Ext.Panel({ 
     layout: 'fit', 
     flex: 1, 
     items: [ 
      new Ext.List({ 
      layout: 'fit', 
      title: 'Winkelwagen', 
      store: NestedListDemo.Winkelwagen_Store, 
      flex: 1, 
      itemTpl : '{title} €{prijs}' 
     })], 
     dockedItems: [{ 
      xtype: 'toolbar', 
      dock: 'bottom', 
      title: 'Totaal' 
      }] 
     }) 

我的店:

NestedListDemo.Winkelwagen_Store = new Ext.data.JsonStore({ 

model: 'ListProductItem', 
storeId: 'WinkelwagenStore', 
data: [ 

], 
autoLoad: true 
}); 

誰能告訴我該怎麼做,因爲我不知道

回答

0

您可以使用本地存儲的每個功能檢查所有記錄價格並計算它們的總和。我發佈一個簡單的例子,告訴你如何做到這一點:

Ext.setup({ 
onReady: function() { 

    //Model registration 
    Ext.regModel('Item', { 
     fields: [ 
      {name: 'title', type: 'string'}, 
      {name: 'prijs', type: 'int'} 
     ] 
    }); 

    //Registration of the Store 
    Ext.regStore('MyStore', { 
     model: 'Item', 
     data: [ 
      {title: 'Item 1', prijs: 100}, 
      {title: 'Item 2', prijs: 200}, 
      {title: 'Item 3', prijs: 700}, 
     ], 
     autoLoad: true 
    }); 

    //Definition of the main list 
    var list = new Ext.List({ 
     itemTpl : '{title} - {prijs}', 
     store: 'MyStore' 
    }); 

    //Definition of the container panel 
    var panel = new Ext.Panel({ 
     fullscreen: true, 
     items: list, 
     dockedItems: [{ 
      xtype: 'toolbar', 
      itemId: 'mainToolbar', 
      items: [{ 
       xtype: 'button', 
       ui: 'action', 
       text: 'Sum', 
       handler: function(){ 

        //Get the Store 
        var store = list.getStore(); 

        //Declaring a sum variable 
        var sum = 0; 

        //Calculate the sum 
        store.each(function(item){ 
         sum += item.get('prijs'); 
        },this); 

        //Show the sum value 
        panel.getDockedComponent('mainToolbar').setTitle('The SUM is: ' + sum); 

       } 
      }] 
     }] 
    }); 
} 

});

正如你所看到的,store.each函數在每條記錄之間迭代,這樣你就可以得到字段「prijs」並將它加到你的「sum」變量中。之後,您可以拿起停放的工具欄並將總和值設置爲標題。

希望這會有所幫助。