【问题标题】:Call a function in an ExtJS XTemplate在 ExtJS XTemplate 中调用函数
【发布时间】:2011-02-06 09:31:30
【问题描述】:

我熟悉使用函数来确定使用 xtemplate 的特定条件,但不知道如何在没有条件 if 语句的情况下直接调用函数。

例如,我的代码想要将一些字符附加到我在 xtemplate 中使用的字符串。我认为最好的方法是在渲染 xtemplate 时附加字符。

var myTpl = new Ext.XTemplate(
  '<tpl for=".">',

    '<tpl if="this.isThumbnailed(thumbnailed) == true">',

      '<img src=this.getThumbUrl(rawThumbUrl)/>', //this call to function does not work, also tried variations of this.

    '</tpl>',

  '</tpl>',

 {
  isThumbnailed : function(thumbnailed) {
    return ...;
  },
  getThumbUrl : function(rawThumbUrl) {
    //... //this function does not get called.
    return ...;
  }

 }
)

【问题讨论】:

    标签: javascript extjs


    【解决方案1】:

    查看XTemplate constructor API 文档。那里有很多很好的例子。引用:

    {[ ... ]} 之间的任何内容都被视为要在模板范围内执行的代码。

    所以您应该能够执行以下操作:

    '<img src={[this.getThumbUrl(rawThumbUrl)]} />',
    

    【讨论】:

    • 发现,如果函数是全局的,那么 '' 就可以了
    • 我正在尝试同样的方法,但它在 IE 11 中不起作用。你知道吗?
    【解决方案2】:

    要调用作用域中定义的函数,需要使用语法:

    {[this.functionName(values.valueName)]}
    

    根据您的情况,您可以致电:

    '<img src="{[this.getThumbUrl(values.rawThumbUrl)]}"/>',
    

    如果您想使用在模板上下文之外定义的函数,请从函数调用中删除 this.

    【讨论】:

    • 谢谢,注意“values”是具有模板当前记录属性的变量。例如 values.birthDate 等。
    【解决方案3】:

    前几天我自己试图弄清楚这一点,并遇到了一个让点击事件正常工作的解决方案。简短的回答是,您需要在模板渲染后使用 .defer 函数来设置事件监听器。

    这是我找到的示例:

    var resultTpl = new Ext.XTemplate(
        '<tpl for=".">',
            '<div class="search-item">',
                '<a id={[this.getLinkId()]} href="violation.aspx?violationid={slvha}">',
                    '<img src="images/icons/home.gif" style="float:left;padding-right:2px">{number}&nbsp;{street}',
                '</a>',
                '<p>Owners:&nbsp;{owners}',
                '<br/>Flag Code:&nbsp;{flag}',
                '<br/>Number of Violations:&nbsp;[{summary}]</p>',
            '</div>',
        '</tpl>', {
        getLinkId: function(values) {
            var result = Ext.id();
            this.addListener.defer(1, this, [result]);
            return result;
        },
        addListener: function(id) {
            Ext.get(id).on('click', function(e){e.stopEvent(); alert('link ' + id + ' clicked');})
        }
    });
    

    Source

    【讨论】:

    • 对于版本 5 及以后的版本,addListener 可以写成:Ext.Function.defer(this.addListener, 1, this,[result]);
    【解决方案4】:

    您可以:

    • 直接引用值,例如{myValue}
    • 将其传递给函数,例如{[this.myFunction(values.myValue)]}

    下面是一个适用于所有版本的 ExtJS 的示例。

    var data = [{
        id: 1,
        subdata: { x: 10, y: 20 }
    }, {
        id: 2,
        subdata: { x: 20, y: 30 }
    }];
    
    var tpl = new Ext.XTemplate([
        '<ul>',
        '<tpl for=".">',
            '<li>ID: <span class="raw">{id}</span> or ',
                '<span class="fmt">{["#" + this.pad(values.id, 3, "0")]}</span></li>',
            '<ul>',
            '<tpl for="subdata">',
                '<li>x: <span class="raw">{x}</span> or ',
                    '<span class="fmt">{[this.frmtUnits(values.x)]}</span></li>',
                '<li>y: <span class="raw">{y}</span> or ',
                    '<span class="fmt">{[this.frmtUnits(values.y)]}</span></li>',
            '</tpl>',
            '</ul>',
        '</tpl>',
        '</ul>',
        {
        	pad : function(s, n, c) {
                if (typeof s !== 'string') s = '' + s;
                return Math.abs(n) <= s.length ? s : this.pad(n > 0 ? c + s : s + c, n, c);
            },
            frmtUnits : function(v) {
                return v + 'px';
            }
        }
    ]);
    
    Ext.onReady(function () {
        new Ext.Panel({
            autoHeight: true,
            html: tpl.apply(data),
            layout: 'fit',
            style: { padding: '2px', margin: '2px' },
            renderTo: Ext.getBody()
        });
    });
    ul li {
        line-height: 1.67em;
    }
    li span {
        font-weight: bold;
    }
    li span.raw {
        color: #D44;
    }
    li span.fmt {
        color: #44D;
    }
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all-debug.js"&gt;&lt;/script&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多