繁体   English   中英

如何使用json2html在结果表的行上突出显示?

[英]how to highlight on line of the result table using json2html?

我正在使用Json2HTML jquery插件json2html从JSON数据构建表,这是当前状态: http : //jsfiddle.net/hamiltonlima/7u8v9wdq/我想要的是在处理过程中比较currentID,以便可以添加“ .active”上课,有什么建议吗?

HTML

    <div class="container">
    <p>
        <table id="placar" class="table table-condensed  table-bordered">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Luchador</th>
                    <th>K</th>
                    <th>D</th>
                    <th>Pontos</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
</div>

Javascript

    var data = [{
    'order': '1',
        'name': 'nheco',
        'k': '3',
        'd': '0',
        'score': '121',
        'id': '540'
}, {
    'order': '2',
        'name': 'boingo',
        'k': '15',
        'd': '3',
        'score': '122',
        'id': '541'
}, {
    'order': '3',
        'name': 'Oswaldo',
        'k': '0',
        'd': '23',
        'score': '123',
        'id': '542'
}];

var currentID = 541;

var transform = {
    tag: 'tr',
    children: [{
        "tag": "td",
            "html": "${order}"
    }, {
        "tag": "td",
            "html": "${name}"
    }, {
        "tag": "td",
            "html": "${k}"
    }, {
        "tag": "td",
            "html": "${d}"
    }, {
        "tag": "td",
            "html": "${score}"
    }]
};

$('#placar > tbody ').json2html(data, transform);

通过标签#json2html进行搜索,发现了另一个使我想到该问题的问题: 当使用基于数据值的json2html转换数据时,如何应用不同的样式?

只需将一个函数添加到class属性即可处理它,数据的每一行都可以在函数中使用“ this”

所以是这样的东西...

class : function(){
        if( this.id == currentID ){
            return 'info';
        } else {
            return '';
        }
    },

完整的示例在这里: http : //jsfiddle.net/hamiltonlima/7u8v9wdq/15/

编辑

不幸的是,json2html中的replace = true选项没有按预期工作。我编写了另一种解决方案,为每行添加id,并更新值。 在这里看看http://jsfiddle.net/hamiltonlima/oqo9otq0/

暂无
暂无

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

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