Skip to content Skip to sidebar Skip to footer

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

Solution 1:

Since you are generating this via JavaScript yes this is very doable.

On that row just check to see if feature.properties.datum === 'Nema Servisa'. If yes, add class="success". If not, don't.

onEachFeature: function (feature, layer) {
   if (feature.properties) {
     var content = "<tableclass='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>";.....

Solution 2:

When creating the table, add a class="feature-property" to each td element. Then you can define this function and call it after the table is generated:

functionhighlightTableRow(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

Post a Comment for "Change Row Class Of Table Based On Value Of Cell"