我正在尝试使用sencha touch 2.3
在Ext.dataview.component.ListItem
中建立列表(使用sencha architect)。我可以使用updateRecord
方法从存储中获取值,但是无法将其传递回主要组件。
这是我要实现的目标:
我想创建一个按钮列表,其文本将在商店中动态显示,如果用户单击它,则应显示带有年龄的警报。我有一家有名称和年龄的商店。我可以获取名称,但无法获取警报设置。我需要帮助-
这是我的dataView类
Ext.define('MyListItem.view.MyListItem', {
extend: 'Ext.dataview.component.ListItem',
alias: 'widget.mylistitem',
requires: [
'Ext.Button',
'Ext.MessageBox'
],
config: {
padding: 10,
layout: 'hbox',
items: [
{
xtype: 'button',
handler: function(button, e) {
var record = this.getRecord();
console.log("buttonTapped");
console.log(record);
Ext.Msg.alert(
record.get('name'), // the title of the alert
"The age of this person is: " + record.get('age') // the message of the alert
);
},
text: 'MyButton'
},
{
xtype: 'component',
flex: 1,
html: 'Sample component inside dataitem'
}
]
},
updateRecord: function(record) {
// Provide an implementation to update this container's child items
var me = this;
me.down('button').setText(record.get('name'));
}
});
这是我的商店:
Ext.define('MyListItem.store.MyDirectStore', {
extend: 'Ext.data.Store',
requires: [
'MyListItem.model.MyModel'
],
config: {
data: [
{
name: 'Adam',
age: '28'
},
{
name: 'Eve',
age: '27'
}
],
model: 'MyListItem.model.MyModel',
storeId: 'MyDirectStore'
}
});
最佳答案
我想您的问题是您无法像处理this.getRecord()
那样从处理程序函数中检索记录
您也可以使用年龄来更新按钮,然后检索它以显示警报,例如:
{
xtype: 'button',
handler: function(button, e) {
console.log("buttonTapped");
Ext.Msg.alert(
button.getText(), // the title of the alert
"The age of this person is: " + button.getAge(); // the message of the alert
);
},
text: 'MyButton',
age: null
},
和:
updateRecord: function(record) {
// Provide an implementation to update this container's child items
var button = this.down('button');
button.setText(record.get('name'));
button.setAge(record.get('age'));
}
更好的办法是将信息或对记录的引用存储在更合适的位置,例如
ListItem
本身?但这应该使它起作用。