简体   繁体   English

根据单元格的值更改表的行类

[英]Change row class of table based on value of cell

Is it possible to change row class (using bootstrap) based of value of data cell? 是否可以根据数据单元格的值更改行类(使用引导程序)? Something like this: (here I hard coded class into row) 像这样的东西:(这里我将类硬编码为行) 在此处输入图片说明

Here is fragment of code 这是代码片段

 onEachFeature: function (feature, layer) {
    if (feature.properties) {
      var content = "<table class='table table-striped table-bordered table-condensed'>" + "<tr><th>Šifra trafostanice</th><td>" + feature.properties.ts_sifra + 
      "</td></tr>" + "<tr><th>Name</th><td>" + feature.properties.ts_naziv + "</td></tr>" +  
      "<tr><th>Code</th><td>" + feature.properties.sifra_lampe + "</td></tr>" +
      "<tr><th>Power</th><td>" + feature.properties.tip_lampe + "</td></tr>" +
      "<tr><th>Pole type</th><td>" + feature.properties.vrsta_stupa + "</td></tr>" +
      "<tr><th>Adress</th><td>" + feature.properties.adresa + "</td></tr>" +
      "<tr><th>Services</th><td>" + feature.properties.datum + "</td></tr>" 
      "</table>";.....

Using code above i get something like this: 使用上面的代码我得到这样的事情: 在此处输入图片说明

So, my question is is it possible to change class of row based of data value? 因此,我的问题是是否可以根据数据值更改行的类?

(Example if value of table row Services is 'Nema servisa' add class success else leave row unchanged) (例如,如果表行Services的值为'Nema servisa',则添加类成功,否则行保持不变)

Since you are generating this via JavaScript yes this is very doable. 因为是通过JavaScript生成的,所以这是非常可行的。

On that row just check to see if feature.properties.datum === 'Nema Servisa' . 在该行上,仅检查一下feature.properties.datum === 'Nema Servisa' If yes, add class="success" . 如果是,请添加class="success" If not, don't. 如果没有,那就不要。

onEachFeature: function (feature, layer) {
   if (feature.properties) {
     var content = "<table class='table table-striped table-bordered table-condensed'>" + "<tr><th>Šifra trafostanice</th><td>" + feature.properties.ts_sifra + 
     "</td></tr>" + "<tr><th>Name</th><td>" + feature.properties.ts_naziv + "</td></tr>" +  
     "<tr><th>Code</th><td>" + feature.properties.sifra_lampe + "</td></tr>" +
     "<tr><th>Power</th><td>" + feature.properties.tip_lampe + "</td></tr>" +
     "<tr><th>Pole type</th><td>" + feature.properties.vrsta_stupa + "</td></tr>" +
     "<tr><th>Adress</th><td>" + feature.properties.adresa + "</td></tr>" +
     "<tr" + (feature.properties.datum === 'Nema servisa' ? ' class="success"' : '') + "><th>Services</th><td>" + feature.properties.datum + "</td></tr>" 
     "</table>";.....

When creating the table, add a class="feature-property" to each td element. 创建表时,将class="feature-property"到每个td元素。 Then you can define this function and call it after the table is generated: 然后,您可以定义此函数并在生成表后调用它:

function highlightTableRow(match){
    $('.feature-property').each(function(){
        var value = $(this).html();
        if(value === match){
            $(this).addClass('success');  
        }
    });   
}

And match would be the data value you want to check.. in this case, Nema Servisa match将是您要检查的数据值。在这种情况下, Nema Servisa

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

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