4

How can I generate a regular html <table> on a Panel in Sencha Touch 2?

The data for each row could be from a store. It's not very "mobile" like a List, but I'd like to have a few detail Panels on my tablet app contain several sections like this:

header #1
<table>
 <tr><td>one</td>td>two</td></tr>
 <tr><td>foo</td>td>bar</td></tr>
</table>

header #2
<table>
 <tr><td>abc</td>td>xyz</td></tr>
 <tr><td>cat</td>td>dog</td></tr>
</table>

The basic Panel definition should look something like this:

Ext.define('Kitchensink.view.HtmlTable', {
    extend: 'Ext.tab.Panel',
    config: {
        activeItem: 1,
        tabBar: {
            docked: 'top',
            layout: {
                pack: 'center'
            }
        },
        items: [{
                title: 'Tab 1',
                items: [{
                    html: '<h2 class="section">Section #1</h2>'
                }, {
                    html: '&lt;table class="style1">'
                }],
            }, {
                title: 'Tab 2',
                items: [{
                    html: '<h2 class="section">Section #3</h2>'
                }, {
                    html: '&lt;table class="style1">'
                }],
            }
        }]
})
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44

1 Answers1

9

The easiest way would be simply create a Ext.Component and give it the tpl configuration. Then you can use the data configuration to update the data in that component.

Here is an example.

Firstly, create your own component which extends container (because you will probably want it scrollable, and only containers are scrollable) and then give it a tpl. This tpl will use XTemplate to loop through the data you give to dynamically create the table for you.

Ext.define('TableComponent', {
    extend: 'Ext.Container',

    config: {
        tpl: Ext.create('Ext.XTemplate',
            '<tpl for=".">',
                '<div>{title}</div>',
                '<table>',
                    '<tpl for="rows">',
                    '<tr>',
                        '<tpl for="columns">',
                        '<td>{html}</td>',
                        '</tpl>',
                    '</tr>',
                    '</tpl>',
                '</table>',
            '</tpl>'
        )
    }
});

Now, inside your application you can use that component and give it some fake data - like so:

Ext.setup({
    onReady: function() {
        var table = Ext.create('TableComponent', {
            data: [
                {
                    title: 'Header 1',
                    rows: [
                        {
                            columns: [
                                { html: 'column 1' },
                                { html: 'column 2' },
                                { html: 'column 3' }
                            ]
                        },
                        {
                            columns: [
                                { html: 'column 1' },
                                { html: 'column 2' },
                                { html: 'column 3' }
                            ]
                        }
                    ]
                },
                {
                    title: 'Header 2',
                    rows: [
                        {
                            columns: [
                                { html: 'column 1' },
                                { html: 'column 2' },
                                { html: 'column 3' }
                            ]
                        }
                    ]
                }
            ]
        });

        Ext.Viewport.add(table);
    }
});
rdougan
  • 7,217
  • 2
  • 34
  • 63
  • This saved me hours of fiddling around! Thank you! – davidcollom Sep 06 '12 at 11:53
  • @rdougan, Thanks for this answer, it's exactly what I need for my app. If I were to use the custom TableComponent in an MVC structure, where would I need to put the file and how would I load the class? – Chris Schmitz Jun 22 '14 at 14:48