总价值Sencha Touch
问题描述:
嗨我做了一个产品名称和价格列表是一个面板的项目面板有一个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
});
谁能告诉我该怎么做,因为我不知道
答
您可以使用本地存储的每个功能检查所有记录价格并计算它们的总和。我发布一个简单的例子,告诉你如何做到这一点:
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”变量中。之后,您可以拿起停放的工具栏并将总和值设置为标题。
希望这会有所帮助。