【问题标题】:How to add extra element in data view while using tpl使用 tpl 时如何在数据视图中添加额外元素
【发布时间】:2021-12-08 08:03:19
【问题描述】:

使用 tpl 时如何在数据视图中添加额外元素。

我正在使用带有 itemselector 的 tpl。现在我在不是来自商店的地方添加了一些额外的 div。如何添加?

这是我的数据视图

onPanelLoad : function( panel , width , height , eOpts){
    var mypanel = panel.query('#somepanel')[0],
        imageStore = Ext.create('Ext.data.Store', {
            id:'imagesStore',
            model: Ext.define('Image', {
                extend: 'Ext.data.Model',
                fields: [
                    { name:'src', type:'string' },
                    { name:'caption', type:'string' }
                ]
            }),
            data: [{
                src: 'http://www.sencha.com/img/20110215-feat-drawing.png',
                caption: 'Drawing & Charts'
            }, {
                src: 'http://www.sencha.com/img/20110215-feat-data.png',
                caption: 'Advanced Data'
            }, {
                src: 'http://www.sencha.com/img/20110215-feat-html5.png',
                caption: 'Overhauled Theme'
            }]
        });

        var imageTpl = new Ext.XTemplate(
        
/* My Try  
        '<tpl for=".">',
            '<div class="item box-wrap">',
                   
                '<div class="content-box app-category" style="width:160px; height:160px;">',
                    '<div class="content-item">',
                        '<img src="{src}" />',
                    '</div>', 
                '</div>',
            '</div>',
*/

         '<tpl for=".">',
             '<div style="margin-bottom: 10px;" class="thumb-wrap">',
                 '<img src="{src}" />',
                 '<br/><span>{caption}</span>',
             '</div>',
        '</tpl>'
    ); 

      if(mypanel)
          mypanel.add([{
              layout:'vbox',
              items:[{
                  xtype : 'dataview',
                  store: Ext.data.StoreManager.lookup('imagesStore'),
                  tpl: imageTpl,
                  itemSelector: 'div.thumb-wrap',
                  emptyText: 'No images available',
             }]
        }]);
    

这是我要添加的内容。

'<tpl for=".">',
    '<div class="item box-wrap">',
        '<div class="content-box app-category" style="width:160px; height:160px;">',
            '<div class="content-item">',
                '<img src="{src}" />',
            '</div>', 
        '</div>',
    '</div>',

当我尝试时,我得到 Uncaught TypeError: Cannot read property 'internalId' of undefined

有什么解决办法吗?

【问题讨论】:

  • 我很确定这不是你应该的行为方式。您添加了赏金,我更新了答案以符合您的其他要求,但您从未回答或授予赏金..
  • @Dinkheller 你的回答对我不起作用。随它去吧。我又开始赏金了,会在 24 小时前分配给你。当你回答并删除“0 Be Aware David”这个词时,请表现得很好。

标签: javascript extjs extjs6


【解决方案1】:

请看fiddle,没有问题。

我添加了两种类型的 itemTpl。

请检查您是否正在阅读项目,否则您可能会得到重复的 ID。

我通过视图内的项目添加了数据视图,并在商店的加载事件中尝试了它。两者都有效。

请注意我在 app.css 中添加了一些样式,这不应该是您的问题

更多信息 您可以将其他数据加载到使用数据模型创建记录的商店。 我更新了小提琴。

【讨论】:

  • 为 ans.在这个小提琴中,您正在从商店加载三个数据,即使在我的情况下也可以正常工作。我的问题是如何添加一个不是来自服务器的额外元素。额外的数据不在存储中。
  • 我需要在来自商店的所有三张图片之前再添加一张图片。我在第 20 行使用。但没有运气@Din
  • 我更新了小提琴,包括一个将数据添加到数据视图的按钮
  • 这很完美,但是如果我在加载商店时需要添加什么。我没有按钮。
  • 加载完所有内容后我可以使用哪种内置方法。目前我正在使用 boxready 来加载我的商店。
猜你喜欢
  • 2017-12-21
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
相关资源
最近更新 更多