4

我正在尝试使用https://github.com/nicolaskruchten/pivottable,基本上我想在表格中显示图像。到目前为止我所做的是;但它不会将图像显示为 img 标签,而是将其视为字符串

<body>
<script type="text/javascript">
$(function(){
    $("#output").pivotUI(
        [ 
            {product: "product1", image: "<img src='image1' alt='' height='42' width='42'>"}, 
            {product: "product2", image: "<img src='image2' alt='' height='42' width='42'>"}
        ]
    );
});
</script>

<p><a href="http://nicolas.kruchten.com/pivottable/examples/index.html">« back to examples</a></p>
<div id="output" style="margin: 10px;"></div>
</body>
4

1 回答 1

5

由于表格渲染器不支持 th 元素的 html 值,因此它明确设置了textContent您必须创建自己的渲染器的属性。你有两个选择:

1.根据Table渲染器代码创建渲染器,修改textContentinnerHTML. 我将使用 jsfiddle 片段,因为渲染功能非常大:http: //jsfiddle.net/u3pwa0tx/

2.重用现有的表格渲染器,它将html显示为纯文本,但在将其返回给要附加的父级之前,将所有文本移动到html。

编辑:我在主存储库https://github.com/nicolaskruchten/pivottable/pull/214上创建了一个拉取请求

$(function(){
    var rendererHtml = function(){
        var result = pivotTableRenderer2.apply(this,arguments);
        $(result).find('th').each(function(index,elem){
            var $elem = $(elem);
            $elem.html($elem.text());
        })
        return result;
    }

    $("#output").pivotUI(
        [ 
            {product: "product1", image: "<img src='image1' alt='' height='42' width='42'>"}, 
            {product: "product2", image: "<img src='image2' alt='' height='42' width='42'>"}
        ],{
            renderers:{
                'table2Renderer': rendererHtml
            }
        }
    );
 });
于 2014-09-23T20:17:48.560 回答