Sencha touch 从dataview获取索引值0任何列表项点击sencha touch

Sencha touch 从dataview获取索引值0任何列表项点击sencha touch,sencha-touch,sencha-touch-2,dataview,Sencha Touch,Sencha Touch 2,Dataview,我无法从dataview获取索引值: { xtype: 'list', itemId: 'catList', store: 'CategoryStore', scrollable: false, layout: 'fit', itemHeight: 20,

我无法从dataview获取索引值:

    {          
              xtype: 'list', 
              itemId: 'catList',
              store: 'CategoryStore',    

             scrollable: false,
              layout: 'fit',          
              itemHeight: 20,
              itemTpl: [
              '<div>',
              '<tpl for="data">',
              '<span >{category_name}</span> ',             
          '</tpl>',
          '</div>'],
listeners: {
   'itemtap': function(list, index, target, record, e, eOpts){
     console.log(record.get('cat_id'));
}
}
}
{
xtype:'列表',
itemId:“catList”,
商店:“分类商店”,
可滚动:false,
布局:“适合”,
身高:20,
第三方物流:[
'',
'',
“{category_name}”,
'',
''],
听众:{
“itemtap”:函数(列表、索引、目标、记录、e、eOpts){
console.log(record.get('cat_id'));
}
}
}
编辑: 如果我将数据静态放在存储上,它可以正常工作,但从服务器获取数据时不起作用:

其工作原理与列表中显示的类似:

      {    
          xtype: 'list', 
          itemId: 'catList',
          scrollable: false,

    data: [
    { category_name: 'A', cat_id: 1},
    { category_name: 'B', cat_id: 2},
    { category_name: 'C', cat_id: 3},
    { category_name: 'D', cat_id: 4},
    { category_name: 'E', cat_id: 5},


    ],
    loadingText: "Loading Words...",
    emptyText: '<div>{message}</div>',
    autoLoad:true,
    itemTpl:[
    '<tpl for=".">',
          '<span >{category_name}</span> ',             
      '</tpl>',
    ] 
  },
{
xtype:'列表',
itemId:“catList”,
可滚动:false,
数据:[
{类别名称:'A',类别id:1},
{类别名称:'B',类别id:2},
{类别名称:'C',类别id:3},
{类别名称:“D”,类别id:4},
{类别名称:'E',类别id:5},
],
加载文字:“加载文字…”,
emptyText:“{message}”,
自动加载:对,
第三方物流:[
'',
“{category_name}”,
'',
] 
},
在这里,我在不同的行上点击多次,但只得到索引0,这是为什么?为什么我在点击列表项的不同行时没有得到不同的索引值


我真的不知道你的代码出了什么问题,因为我对它进行了测试,结果很好。然而,有几件事是错误的

  • 您不需要itemTpl中的for循环,因为“itemTpl”已经存在 为您在数据数组中迭代。如果你是的话,你会需要它的 只使用“第三方物流”
  • 避免让听众进入你的视野。相反 在控制器中获取对列表的引用,并设置 那里的听众这是一种糟糕的做法,它打破了NVC模式。
下面是一个适用于我的te4st应用程序的小版本:

{
    xtype: 'list', 
    itemId: 'catList',
    scrollable: false,
    data: [
        { category_name: 'A', cat_id: 1},
        { category_name: 'B', cat_id: 2},
        { category_name: 'C', cat_id: 3},
        { category_name: 'D', cat_id: 4},
        { category_name: 'E', cat_id: 5},
    ],
    loadingText: "Loading Words...",
    emptyText: '<div>{message}</div>',
    autoLoad:true,
    itemTpl: '{category_name}',   
    listeners: {
        'itemtap': function(list, index, target, record, e, eOpts){
            //TODO: whatever..
        }
    }
}
{
xtype:'列表',
itemId:“catList”,
可滚动:false,
数据:[
{类别名称:'A',类别id:1},
{类别名称:'B',类别id:2},
{类别名称:'C',类别id:3},
{类别名称:“D”,类别id:4},
{类别名称:'E',类别id:5},
],
加载文字:“加载文字…”,
emptyText:“{message}”,
自动加载:对,
itemTpl:“{category_name}”,
听众:{
“itemtap”:函数(列表、索引、目标、记录、e、eOpts){
//托多:随便。。
}
}
}

我为自己做了完美的尝试,让我来分享我所做的

型号

Ext.define('Myapp.model.Category', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
            { name: 'cat_id', type: 'integer' },
            { name: 'category_name', type: 'string' },
            { name: 'created_date', type: 'string' },
            { name: 'order', type: 'integer' },
            { name: 'status', type: 'integer' },
            { name: 'deleted', type: 'integer' },
            { name: 'type', type: 'integer' }
        ]
    }
});
商店

Ext.define('Myapp.store.Category', {
    extend: 'Ext.data.Store',
    requires: [
        'Myapp.model.Category'
    ],
    config: {
        storeId: 'category',
         model: "Myapp.model.Category",
         proxy: {
            type: "ajax",
            url : "http://alucio.com.np/trunk/dev/sillydic/admin/api/word/categories/SDSILLYTOKEN/650773253e7f157a93c53d47a866204dedc7c363?_dc=1376475408437&page=1&start=0&limit=25",
            reader: {
                type: "json",
                rootProperty: "data"
            }
        },
        autoLoad: true
    }

});
列表

 {          
    xtype: 'list', 
    itemId: 'catList',
    store: Ext.create('Myapp.store.Category'),
    layout: 'fit',      
    itemHeight: 20,
    itemTpl: [
              '<div>',
              '{category_name}',             
              '</div>'],
            listeners: {
               itemtap: function(list, index, target, record, e, eOpts){
                 console.log(record.get('cat_id'));
            } 
     } 
  }
{
xtype:'列表',
itemId:“catList”,
store:Ext.create('Myapp.store.Category'),
布局:“适合”,
身高:20,
第三方物流:[
'',
“{category_name}”,
''],
听众:{
itemtap:功能(列表、索引、目标、记录、e、eOpts){
console.log(record.get('cat_id'));
} 
} 
}
输出

 {          
    xtype: 'list', 
    itemId: 'catList',
    store: Ext.create('Myapp.store.Category'),
    layout: 'fit',      
    itemHeight: 20,
    itemTpl: [
              '<div>',
              '{category_name}',             
              '</div>'],
            listeners: {
               itemtap: function(list, index, target, record, e, eOpts){
                 console.log(record.get('cat_id'));
            } 
     } 
  }

正如您所看到的,itemtap功能还可以打印正确的cat_id

更新

根据你的评论

{    
   xtype :'panel',
   items : [{
       xtype : 'toolbar',
       itemId: 'categoryName'  // Give itemId  
      },
      {
       xtype: 'list', 
       itemId: 'catList',
       height : '100%',
       store: Ext.create('GoodNews.store.Category'),
       layout: 'fit',      
       itemHeight: 20,
       itemTpl: [
         '<div>',
         '{category_name}',             
         '</div>'],
         listeners: {
           itemtap: function(list, index, target, record, e, eOpts){
             var catId = record.get('cat_id');
             var categoryName = record.get('category_name');
             // Set the title like this
             this.getParent().getComponent('categoryName').setTitle(categoryName);
           } 
         }                
       }]     

 }
{
xtype:“面板”,
项目:[{
xtype:'工具栏',
itemId:'categoryName'//给出itemId
},
{
xtype:'列表',
itemId:“catList”,
高度:“100%”,
store:Ext.create('GoodNews.store.Category'),
布局:“适合”,
身高:20,
第三方物流:[
'',
“{category_name}”,
''],
听众:{
itemtap:功能(列表、索引、目标、记录、e、eOpts){
var catId=record.get('cat_id');
var categoryName=record.get('category_name');
//这样设置标题
this.getParent().getComponent('categoryName').setTitle(categoryName);
} 
}                
}]     
}

你能给我们提供更多关于你试图在这里显示的数据类型的信息吗(比如json?)对我来说,听起来你只显示了一个项目,其中你重复了很多次,并在你的范围内显示了你的行。在这里,如果我点击类别名称,那么我想获取索引值,并想点击cat_id获得索引的帮助。最后,我打算根据cat_id获取数据。@Google_Android_爱好者看到我的更新请回答,但我知道这是可行的,但我的问题是,如何从存储中获取该值。请参阅->>商店:'CategoryStore'。我想去那家商店。请你能帮我如何从中获取。这是有效的。如果我从商店获得价值,我就无法获得价值。如果我这样做,它是有效的。我的数据显示这种类型,根据JSON:itemTpl:[“”,“
    ”,“{category_name},“
”,“,”,”],看看我的问题,我在问题的末尾添加了JSON。我展示的示例是使用存储或内联数据。正如我说的,你应该在你的控制器中设置侦听器。在itemtap回调方法中,“record”是存储区中的记录,因此您可以访问该记录的所有属性。如果这是你想要的商店,有两种方法1。在控制器中保留它的引用,2。回调方法中的do list.getStore()我没有在控制器中使用侦听器,它在视图中。但是我没有得到recode.get('cat_id')值。temTpl:[“”,“
    ”,“{category_name},“
”,“,”,”,],侦听器:{'itemtap':函数(list,index,target,record,e,eOpts){console.log(index);recode.get('cat_id');}非常感谢,