简体   繁体   English

ExtJS 3.4:如何将自定义渲染器动态添加到PropertyGrid?

[英]ExtJS 3.4: How to add a custom renderer dynamically to a PropertyGrid?

I need to display an image in a PropertyGrid. 我需要在PropertyGrid中显示图像。 This is usually achieved using the customRenderers config, explicitly overriding the rendered for a specific field: 通常,这是使用customRenderers配置实现的,显式覆盖特定字段的呈现方式:

grid = new Ext.grid.PropertyGrid({
    customRenderers: {
        "picture": function(v)
        {
            return "<img src=\"" + feature.attributes["picture"] + "\" />";
        }
    }
});

But in my case the field on which to apply the custom rendering function is only known at run-time (I have to search for a string like "http:"). 但是在我的情况下,仅在运行时才知道要应用自定义渲染功能的字段(我必须搜索“ http:”之类的字符串)。 But adding the custom renderer dynamically at that time does no seem to work. 但是当时动态添加自定义渲染器似乎无效。

Is there any way to do this? 有什么办法吗? Thank you. 谢谢。

Here is how the dynamic property is found: 这是动态属性的查找方式:

for (var key in feature.attributes)
{   
    value = feature.attributes[key];
    if ((value != null) && (value.substring(0, 4) == "http"))   
        grid.customRenderers[key] = Function("v", "return \"<img src=\\\"" + value + "\\\" />\";"); 
}

Even though customRenderers is a config property, it is still accessible after the component is created. 即使customRenderers是config属性,在创建组件后仍然可以访问它。 You can dynamically add them after the grid is created. 您可以在创建网格后动态添加它们。

Have a look at this jsfiddle: http://jsfiddle.net/Wxx3M/1/ 看看这个jsfiddle: http : //jsfiddle.net/Wxx3M/1/

HTML : HTML

<p id="button-container"></p>
<div id="prop-grid"></div>

JS : JS

Ext.onReady(function(){

    var propsGrid = new Ext.grid.PropertyGrid({
        renderTo: 'prop-grid',
        width: 300,
        autoHeight: true,
        propertyNames: {
            tested: 'QA',
            borderWidth: 'Border Width'
        },
        source: {
            '(name)': 'Properties Grid',
            grouping: false,
            autoFitColumns: true,
            productionQuality: false,
            created: new Date(Date.parse('10/15/2006')),
            tested: false,
            version: 0.01,
            borderWidth: 1
        },
        viewConfig : {
            forceFit: true,
            scrollOffset: 2 // the grid will never have scrollbars
        }
    });

    // simulate updating the grid data via a button click
    new Ext.Button({
        renderTo: 'button-container',
        text: 'Update custom renderer',
        handler: function(){
            var targetProp = null;
            for (var key in feature.attributes)
            {   
                value = feature.attributes[key];
                if ((value != null) && (value.substring(0, 4) == "http"))
                    targetProp = key;   
            }
            propsGrid.customRenderers[targetProp] = function(v){
                return v ? 'XXX' : '';
            }
            propsGrid.getView().refresh();
        }
    });
});

Initially there is no custom renderers. 最初没有自定义渲染器。 If you click the button, a new dynamic custom renderer is added. 如果单击该按钮,则会添加一个新的动态自定义渲染器。

After trying a great deal of different strategies I finally got to something that functions: 在尝试了许多不同的策略之后,我终于了解了一些可以起作用的东西:

grid = new Ext.grid.PropertyGrid();
for (var key in feature.attributes)
{   
    value = feature.attributes[key];
    if ((value != null) && (value.substring(0, 4) == "http"))   
        grid.customRenderers[key] = Function("v", "return \"<img src=\\\"" + value + "\\\" />\";"); 
}

The feature.attributes is a dictionary containing the data to be displayed; feature.attributes是一个字典,其中包含要显示的数据。 a cycle iterates through each of its keys, searching for the fields that might contain image URLs. 一个循环遍历其每个键,搜索可能包含图像URL的字段。

There were two key elements that brought me to this solution: 有两个关键因素使我想到了这个解决方案:

  • Understanding that customRenderers can be used itself as a dictionary, thus dispensing the hard-coding of fields names. 了解customRenderers本身可以用作字典,从而免除了字段名称的硬编码。

  • The usage of Function as a means to encode the exact value to be rendered, this way avoiding any scope issues. 使用Function作为对要呈现的确切值进行编码的方法,从而避免了任何范围问题。

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

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