使用CSS或jQuery轉換表格單元內容

[英]Transforming tablecell content with CSS or jQuery


I have a html table similar to this:

我有一個類似於這個的html表:

<table>
  <tr>
    <td>1</td>
    <td>Some text...</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Some text...</td>
  </tr>
  <tr>
    <td>3</td>
    <td>Some text...</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Some text...</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Some text...</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Some text...</td>
  </tr>
  <tr>
    <td>3</td>
    <td>Some text...</td>
  </tr>
</table>

The cells in the first column contain a numeric value of 1, 2 or 3.

第一列中的單元格包含數值1,2或3。

I’m looking for a neat way of transforming this value into something more “graphical” using a client side approach such as CSS (preferably) or jQuery. For example, instead of “1” the cell content should be rendered as an icon, or a dot with a red color. Just setting the background color would also be ok.

我正在尋找一種巧妙的方法,使用客戶端方法(如CSS(最好)或jQuery)將此值轉換為更“圖形化”的東西。例如,代替“1”,單元格內容應該呈現為圖標,或者呈現為紅色的點。只需設置背景顏色也可以。

UPDATE:

Thanks for all the suggestions. It seems it was just the Each method from jQuery I was missing. :)

感謝所有的建議。看起來它只是來自jQuery的每個方法我都不見了。 :)

Here's my final code. I wrapped it in separate function which is called on document ready and after table updates.

這是我的最終代碼。我將它包裝在單獨的函數中,該函數在文檔就緒和表更新之后調用。

function fnTransformIcons() 
{ 
  var color;

  $("td.Icon").each(function() { 
  var value = $(this).html();

  switch (parseInt(value))
  { 
    case 0 : color = 'white'; break;
    case 1 : color = 'red'; break; 
    case 2 : color = 'green'; break; 
    case 3 : color = 'yellow'; break;
  } 
  $(this).css("backgroundColor", color); 
  });     
}

4 个解决方案

#1


3  

$(document).ready( function() {
    $("tr").each( function() {
        switch ($("td:first",this).text()) {
            case '1': color = 'red'; break;
            case '2': color = 'blue'; break;
            default: color = 'green';
        }
        $($("td:first",this).css("backgroundColor", color);
    });
});

Thanks Eikern for the tip.

感謝Eikern提示。

#2


1  

For jQuery, try

對於jQuery,試試吧

$("tr td:first").each(function(){
   // do transformations like $(this).html(...)
});

#3


1  

Yet another solution:

又一個解決方案:

$("tr td:first").each( function() {
    $(this).addClass('my_custom_class_'+$(this).text());
});

CSS:

.my_custom_class_1 { background: red; }
.my_custom_class_2 { background: green; }
/* etc. */

#4


1  

If you know it's going to be numeric, you could do something like:

如果您知道它將是數字,您可以執行以下操作:


$(function(){
   $('tr').each(function(){
    var cell = $(this).children().first(); //Get first td
    var new_contents = "";
    for (i=0; i <= parseInt(cell.text()); i++) {
      new_contents += '<span class="counter">&deg;</span>'; //or whatever counter character you like
    }
    cell.html(new_contents);
  });
});

This gives you a result that looks like:

這會給你一個看起來像這樣的結果:

°   Some text...
°°  Some text...
°°° Some text...
°   Some text...
°   Some text...
°°  Some text...
°°° Some text...

But of course, you could style it, change the counter character, use an image instead of °, etc.

但是,當然,您可以設置樣式,更改計數器字符,使用圖像而不是°等。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/02/05/86e1f5d0f4dcdf83595b853dbf3a72e6.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com