简体   繁体   English

EXTJS 4在组合框中呈现所选值的HTML

[英]EXTJS 4 render HTML of a selected value in a combobox

Hello I have the next problem, I want to render the html of my display value in a combobox, at the moment I load a store with the html ready, it renders the html when I show all of them, but when I select one, it show the html. 你好,我有下一个问题,我想在一个组合框中渲染我的显示值的html,当我加载一个准备好html的商店时,它在我显示所有这些时呈现html,但是当我选择一个时,它显示了HTML。

What can I do to render the html when the item is already selected? 当项目被选中时,我该怎么做才能呈现html?

Here are some images to help to explain the inconvenient: 以下是一些有助于解释不便之处的图片:

This is when Im going to select one 这是我要选择一个

http://i.stack.imgur.com/TcfRA.jpg http://i.stack.imgur.com/TcfRA.jpg

This is when I select one 这是我选择一个

http://i.stack.imgur.com/Kzi9r.jpg http://i.stack.imgur.com/Kzi9r.jpg

The Html that I'm rendering is the next one: 我正在渲染的Html是下一个:

<img class="io_img" src="/files/images/io-P.gif"/><div class="io_desc">hola</div></div>

Thanks in advance. 提前致谢。

PD: Sorry to no show the images, and just the links, but I don't have enough reputation to show images directly . PD:很抱歉没有显示图片,只显示链接,但我没有足够的声誉来直接显示图片。

This require few steps. 这需要很少的步骤。 Problem is that ComboBox has input field underneath, and inputs can't display html. 问题是ComboBox下面有输入字段,输入无法显示html。 So first step is to change template which replace input with div. 所以第一步是更改用div替换输入的模板。 Eg: 例如:

fieldSubTpl: [
    '<div class="{hiddenDataCls}" role="presentation"></div>',
    '<div id="{id}" type="{type}" ',
        '<tpl if="size">size="{size}" </tpl>',
        '<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
        'class="{fieldCls} {typeCls}" autocomplete="off"></div>',
    '<div id="{cmpId}-triggerWrap" class="{triggerWrapCls}" role="presentation">',
        '{triggerEl}',
        '<div class="{clearCls}" role="presentation"></div>',
    '</div>',
    {
        compiled: true,
        disableFormats: true
    }
]

Then change template which shows selected value: 然后更改显示所选值的模板:

displayTpl: Ext.create('Ext.XTemplate', [ 
    '<tpl for=".">',
    '<img src="http://phpbb3.pl/adm/images/icon_edit.gif" />',
    '{[typeof values === "string" ? values : values["title"]]}',
    '</tpl>'
])

Another thing is to create new list-item template. 另一件事是创建新的列表项模板。 Eg: 例如:

listConfig: {
    getInnerTpl: function() {
        return '<div class="search-item">' +
            '<h3><img src="http://phpbb3.pl/adm/images/icon_edit.gif" /><span>{[Ext.Date.format(values.lastPost, "M j, Y")]}<br />by {author}</span>{title}</h3>' +
            '{excerpt}' +
        '</div>';
    }
}

And the last thing - you must ensure that the value is setted correctly into div. 最后一件事 - 您必须确保将值正确设置为div。 For that you should override setRawValue method: 为此,您应该覆盖setRawValue方法:

setRawValue: function(value) {
    var me = this;
    value = Ext.value(value, '');
    me.rawValue = value;

    // Some Field subclasses may not render an inputEl
    if (me.inputEl) {
        // me.inputEl.dom.value = value;
        // use innerHTML
        me.inputEl.dom.innerHTML = value;
    }
    return value;
}

Notice that new template doesn't contain any input field, so value will not be submited. 请注意,新模板不包含任何input字段,因此不会提交值。 If you need use such combo with form, you should add hidden input somewhere in fieldSubTpl and set value for it in setRawValue . 如果你需要使用这样的组合与形式,你应该添加在什么地方隐藏的输入fieldSubTpl并设置值它setRawValue

Working sample: http://jsfiddle.net/lolo/8Xs5h/1/ 工作样本: http//jsfiddle.net/lolo/8Xs5h/1/

We have similar task to display selected color. 我们有类似的任务来显示所选的颜色。 Our solution is overriding of combobox template: 我们的解决方案是覆盖组合框模板:

var store = new Ext.data.SimpleStore({
    fields: ['num','id', 'color']
});

var tplColor = new Ext.XTemplate(
    '<tpl for="."><div id = "seriesColor_{num}" class="combo-result-item">',
       '<div  class="combo-texture" style="background-color:{color};">&nbsp;</div>',
    '</div></tpl>'
 );

new Ext.form.ComboBox({
    tpl: tplColor,
    store: store,
    ...
};

You can do something similar but use image instead of background-color. 你可以做类似的事情,但使用图像而不是背景颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM