-
8 Nov 2011 1:21 AM #1
Unanswered: How to get selected index or object of a List?
Unanswered: How to get selected index or object of a List?
Hi everyone,
I try to learn Sencha, and I have difficulties in learning it, because I'm stupid I guess.
I'm implementing a simple RSS reader. I create a PHP code to parse RSS feeds so I can use this PHP script (which has the same domain as my app) with ajax proxy.
I'm able to show the list of news. I want to make a behaviour when user press one of the news, browser will open the URL of the selected news. The problem is I cannot get the index or object of selected item.
Ho to do this in Sencha? It's pretty confusing because I guess I will have a method called getSelectedIndex() but I don't. Please help.
Thanks in advance.
-
8 Nov 2011 2:42 AM #2
In the list init I do this:
further on in the code I use this:Code:init: function () { this.control( { assignmentslist: { itemtap: this.onViewItemTap } }) },
The code above is mostly inspired by the SenchaCon demo at http://senchacon.senchafy.comCode:onViewItemTap: function (c, b) { var a = c.getStore().getAt(b); this.application.fireEvent("navigate", { view: "AssignmentDetail", record: a }) }
To clarify: b is the index of the tapped item, c is the list, a is the tapped record. I hope this helped you.
-
8 Nov 2011 8:08 AM #3
Don't ever call yourself stupid. If you were able to make it this far you definitely are not. The API docs aren't quite up to date for ST2.
To handle the itemtap event:
In your controller's init method (assuming you're using one)
Then inside your controller you declare a function:Code:this.control({ 'listxtype or selector': { itemtap: this.listItemTapHandler } );
If you're not using a controller, in your list config you have to add a listenerCode:listItemTapHandler:function(list, index, item, evt) { // to get the item from store // getListStore refers to a reference inside your controller for the store. this.getListStore().getAt(index); // or you can reference the store from the list object list.getStore().getAt(index); }
Hope this helpsCode:listeners: { itemtap: function(list,index,item, evt) { .. } }
-
9 Nov 2011 7:51 PM #4
Thanks Stan229,
I added these lines to my List
And now it works like a charm.Code:listeners: { itemtap: function(list, index, item, evt) { alert('Index: '+index); } }
I've described that I try to create an RSS reader app. So the data is fetched by AJAX. Using this knowledge of how to get the list index, I still confused about how to make the browser open the URL of selected item on the list?
Here is my Store:
And here is my Model:Code:var newsStore = Ext.create('Ext.data.Store', { model: 'News', proxy: { type: 'ajax', url: 'feeds.php', reader: { type: 'json', root: 'news' } }, autoLoad: true });
What I need to do is something like this:Code:Ext.define('News', { extend: 'Ext.data.Model', fields: [ { name: 'title', type: 'string' }, { name: 'image', type: 'string' }, { name: 'summary', type: 'string' }, { name: 'link', type: 'string' } ] });
navigate(newsStore[index].link);
Is there any way to do this?


Reply With Quote