javascript
  • jquery
  • twitter-bootstrap-3
  • bootstrap-modal
  • 2016-03-15 155 views 0 likes 
    0

    是否可以根據數據單元的值更改行類(使用引導程序)? 事情是這樣的: (這裏我硬編碼類成排) enter image description here根據單元格的值更改表格的行類

    這裏是代碼

    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>";..... 
    

    使用代碼上面,我得到這樣的片段: enter image description here

    所以,我的問題是可以根據數據值更改行的類別嗎?

    (例如果錶行服務的價值「的Nema servisa」添加類 成功別的假行保持不變)

    回答

    1

    既然你是通過JavaScript產生這是這是非常可行的。

    在該行上,檢查是否feature.properties.datum === 'Nema Servisa'。如果是,請添加class="success"。如果沒有,不要。

    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>";..... 
    
    +0

    是的,這有效的,簡潔答案, 謝謝!我甚至調整了一下,添加這行代碼feature.properties.datum ==='Nema servisa'? 'class =「danger」':''|| feature.properties.datum!='Nema servisa'? 'class =「success」':'' – Svinjica

    1

    在創建表格時,爲每個td元素添加class="feature-property"。 然後你可以定義這個函數並調用它生成的表格後:

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

    而且match將要檢查的數據值。在這種情況下,Nema Servisa

    +0

    Thx for answer as well! :) – Svinjica

    相關問題