ExtJS无法正确显示Ext.panel.Panel的html

时间:2018-02-01 13:08:20

标签: javascript html extjs panel xtemplate

我从Ext.grid.Panel继承了一个类,并使用此类中的方法显示panel,其中包含两个项:listtoolbar类和{{1}工作人员。

Ext.XTemplate

无法显示面板的项目; Ext.define('MyApp.view.BGrid', { extend: 'MyApp.view.AGrid', //This class extends from 'Ext.grid.Panel' requires: [ ... 'Ext.layout.container.VBox', 'MyApp.view.base.ListToolbar' ], getDashBoardTpl: function () { var me = this; return [ { xtype: 'panel', // html: 'so what?', // Displays the text; See -Img 01 // html: me.dashTpl(), // Displays as [object object]; See -Img 02 layout: {type: 'vbox', align: 'stretch', pack: 'center'}, items: [ { xtype: 'listtoolbar', //Display nothing! flex: 1 }, { xtype: 'panel', name: 'dashtpl', flex: 1, // html: 'so WHat?' //Display nothing! html: me.dashTpl() //Display nothing! } ] } ] }, listtoolbar' html。而不是它在dashtpl标签使用上面板时起作用。在第一个面板中使用html函数时,只需显示[Object] [Object]

这是dashTpl();

dashTpl

这可能是什么原因?谢谢你的建议。

以下是Img 01Img 02

1 个答案:

答案 0 :(得分:1)

@Nuri,在您的代码中,您使用html内的Ext.XTemplate设置panel,这不是使用html配置的正确方法。

  

如果您想在panel中使用html配置,则需要设置tpl,例如以下示例

html:'<table class="x-date-table"><tbody><tr><th>SNo.</th><th>Date</th><th>Event</th><th>Venue Details</th></tr><tr><td>1</td><td>12 February</td><td>Waltz with Strauss</td><td>Main Hall</td></tr><tr><td>2</td><td>24 March</td><td>The Obelisks</td><td>West Wing</td></tr><tr><td>3</td><td>15 February</td><td>Kolidoscop</td><td>Reception Hall</td></tr><tr><td>4</td><td>24 March</td><td>The data center</td><td>UDM</td></tr></tbody></table>'
  

如果您想在panel中使用tpl/Ext.XTemplate配置,则需要设置tpl,例如以下示例

tpl: new Ext.XTemplate(
    //start table
    '<table class="x-date-table">',
    //Header of my table
    '<tr>',
    '<th>SNo.</th>',
    '<th>Date</th>',
    '<th>Event</th>',
    '<th>Venue Details</th>',
    '</tr>',
    //Looping insdie tpl for creating multiple row depend on data
    '<tpl for=".">', //Start loop tpl
    '<tr>',
    '<td>{#}</td>',
    '<td>{date}</td>',
    '<td>{event}</td>',
    '<td>{venue}</td>',
    '</tr>',
    '</tpl>', //End loop tpl
    '</table>' //Close table
)

在此 FIDDLE 中,我使用与上述相同的方法创建了一个演示。我希望这可以帮助您或指导您实现您的要求。

CODE SNIPPET

Ext.application({
    name: 'Fiddle',

    launch: function () {
        //Array of data for testing/example
        var data = [{
            date: '12 February',
            event: 'Waltz with Strauss',
            venue: 'Main Hall'
        }, {
            date: '24 March',
            event: 'The Obelisks',
            venue: 'West Wing'
        }, {
            date: '15 February',
            event: 'Kolidoscop',
            venue: 'Reception Hall'
        }, {
            date: '24 March',
            event: 'The data center',
            venue: 'UDM'
        }];
        //Panel show data with XTemplate
        Ext.create('Ext.panel.Panel', {
            renderTo: Ext.getBody(),
            fullscreen: true,
            title: 'Example of XTemplate in Panel',
            tpl: new Ext.XTemplate(
                //start table
                '<table class="x-date-table">',
                //Header of my table
                '<tr>',
                '<th>SNo.</th>',
                '<th>Date</th>',
                '<th>Event</th>',
                '<th>Venue Details</th>',
                '</tr>',
                //Looping insdie tpl for creating multiple row depend on data
                '<tpl for=".">', //Start loop tpl
                '<tr>',
                '<td>{#}</td>',
                '<td>{date}</td>',
                '<td>{event}</td>',
                '<td>{venue}</td>',
                '</tr>',
                '</tpl>', //End loop tpl
                '</table>' //Close table
            ),
            data: data
        });

        /*This function will create html base on data and return
         * @param {Array} arr contain of data/records
         * @return {String/HTML}
         */
        function doGetHtml(arr) {
            let html = `<table class="x-date-table"><tr><th>SNo.</th><th>Date</th><th>Event</th><th>Venue Details</th></tr>`;

            arr.forEach((item, index) => {
                html += `<tr><td>${index+1}</td><td>${item.date}</td><td>${item.event}</td><td>${item.venue}</td></tr>`;
            });
            return `${html}</table>`
        }

        //Panel show data with HTMl
        Ext.create('Ext.panel.Panel', {
            renderTo: Ext.getBody(),
            margin: '20 0 0 0',
            fullscreen: true,
            title: 'Example of HMTL in Panel',
            html: doGetHtml(data)
        });
    }
});
相关问题