Sunday 21 July 2013

Display Panel data on list item tap in Sencha Touch 2

As requested by one of my reader, they need to display the list and panel component in HBox layout. By tapping on any items in the list, their corresponding data needs to be displayed in the panel component in Sencha Touch 2. Let's see the implementation


 Create a Model Class

 First, let' create a list model class with two fields name and text.

 Ext.define('MyApp.model.ListModel', {
    extend: 'Ext.data.Model',
    alias: 'model.ListModel',

    config: {
        fields: [{
            name: 'name',
            type: 'string'
        }, {
            name: 'text',
            type: 'string'
        }]
    }
});

 Create a Store Class

 Now, we are going to create a store and assign the above created model class to the store using store configuration. then, we are going to set some static model data instance to the store using data configuration.

Ext.define('MyApp.store.ListStore', {
    extend: 'Ext.data.Store',
    alias: 'store.ListStore',

    requires: [
        'MyApp.model.ListModel'
    ],

    config: {
        data: [{
            name: 'Sencha Architect',
            text: 'The ultimate HTML5 visual app builder, Sencha Architect empowers your team to design, develop, and deploy apps from a single, integrated environment. Finally, designers and developers can work in lock-step to create amazing applications'
        }, {
            name: 'Sencha Animator',
            text: 'With the power of Sencha Animator, you can build cutting edge interactive CSS3 animations and embed them to your website, iBook, or App. Animate text and images with smooth transitions, design buttons with gradients, and make your animations highly interactive. All backed by the strength of web standards.'
        }, {
            name: 'Sencha Cloud',
            text: 'Sencha.io is a complete cloud platform for building mobile web apps. With services like User, Data, Channel, Image and Deployment, Sencha.io makes it easy to create shared experiences in the cloud.The Sencha.io cloud is currently in beta and open to all developers!.'
        }],

        model: 'MyApp.model.ListModel',
        storeId: 'ListStore'
    }
});


Add List and Panel Component

 Now, we are going to display both list and panel component horizontally using HBox layout. These list and panel components are added inside the container. In list component, itemTpl configuration is used to display record in HTML template. here we are displaying record name . Similarly in panel component, itemTpl configuration is used to display record in HTML template. here we are displaying record text .

 Ext.define('MyApp.view.DemoContainer', {
    extend: 'Ext.Container',
    alias: 'widget.DemoContainer',

    config: {

        itemId: 'democontainer',
        layout: {
            type: 'hbox'
        },

        items: [{
            xtype: 'list',
            flex: 1,
            itemId: 'demolist',
            layout: {
                type: 'fit'
            },

            itemTpl: [
                '<div>{name}</div>'
            ],
            store: 'ListStore'
        }, {
            xtype: 'panel',
            flex: 2,
            itemId: 'demopanel',
            tpl: [
                '{text}'
            ],

            layout: {
                type: 'fit'
            }
        }],

        listeners: [{
            fn: 'onDemolistItemTap',
            event: 'itemtap',
            delegate: '#demolist'
        }]
    },

    onDemolistItemTap: function (dataview, index, target, record, e, options) {

        //console.log(record);
        var container = dataview.up('#democontainer');

        var panel = container.down('#demopanel');

        panel.setData({
            text: record.data.text
        });
    }
});


  Each component is having an itemId configuration which is used to access the component in CSS query format.  On tapping on any items in the list, itemtap event will be trigged and onDemolistItemTap() method will be fired. These mapping are added using the listeners configuration.

  Inside onDemolistItemTap() method, we are accessing the child items of the container using itemId configuration of the component.Finally setData() is used to assign the list record data to the panel component.

 Following is the output, when i run the above code in Google Chrome.


 Hope, you enjoyed this Post.

Download the Source Code

7 comments:

  1. Hi,
    This tutorial helped me. Can we load viewport in same code on itemtap.

    ReplyDelete
    Replies
    1. Yes, you can try something like this

      mycontainer = Ext.create('MyApp.view.MyContainer');
      //add component to view port
      Ext.Viewport.add(mycontainer);
      //display the component in browser
      Ext.Viewport.setActiveItem(mycontainer);

      Thanks
      Suresh A

      Delete
  2. How to find index value, according to tapped list item.In your code, if clicked sencha architect then 0 and if clicked sencha cloud then 2. why get these index value.

    ReplyDelete
    Replies
    1. Hi Kshetri,
      in order to get the item tap index value from list/dataview, you need to use dataview.getActiveItem().

      From Above Code, inside onDemolistItemTap() function 2nd parameter 'index' will contain the tapped index value.

      Thanks and Regards,
      Suresh Kumar A

      Delete
  3. Suppose if I dont want this master details type UI and if the second ,i mean the details view is pushed to the view and if I have a v box layout in that details view and how can i set the data coming from this list layout and assign to those values to two tpl's i have created in each v box??
    Any thoughts ??

    ReplyDelete
    Replies
    1. Hi Jithz,

      You need to attach 'itemtap' event with method using listener component for list. Inside the method , you have list item details as model records. Try to get the Details view using Ext.componentquery.query(), then assign the model record data to the view.

      Hope these information very helpful to you.

      Thanks and Regards,
      Suresh Ariya

      Delete