2013-04-02 44 views
0

我有這樣一個表:僅當變量具有使用jquery的值時纔在html中顯示逗號?

<table class="table table-bordered"> 
     <thead> 
      <tr> 
      <th></th> 
      <th>Project Name</th> 
      <th>Tape-ID</th> 
      <th>Video Type</th> 
      <th>Date Created</th> 
      <th>Director</th> 
      <th>Camera Person</th> 
      <th>Editor</th> 
      <th>Tag</th> 
      </tr> 
     </thead> 
     <tbody> 
     {% load endless %} 

    {% paginate 8 a %} 
     {% for choice, i in a %} 
      <tr> 
      <td><input type="checkbox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" onchange="checkChecked()" /></td> 
      <td>{{ choice.name }}</td> 
      <td>{{ choice.tape_id }} </td> 
      <td>{{ choice.video_type }}</td> 
      <td>{{ i }}</td> 
      <td>{{ choice.director }}</td> 
      <td>{{ choice.cameraman }}</td> 
      <td>{{ choice.editor }}</td> 
      <td>{{ choice.tag1 }}, {{ choice.tag2 }}, {{ choice.tag3 }}, {{ choice.tag4 }}, 
      {{ choice.tag5 }}, {{ choice.tag6 }}, {{ choice.tag7 }}, {{ choice.tag8 }}, 
      {{ choice.tag9 }}, {{ choice.tag10 }} 


      </td> 
      </tr> 
     {% endfor %} 

     </tbody> 
     </table> 

這些可變{{ choice...}}可以或可以不具有價值。雖然是順序的。

water, pollution, hello, , , , , , 

我不想顯示逗號,如果沒有相應的價值:高達3變量可以。如果沒有相應的價值談到這樣3.有之後價值和有沒有價值的。我怎樣才能做到這一點?

+0

爲什麼用javascript的時候你可以在服務器端做到這一點? – Pankucins

回答

0

,如果你正在尋找一個jQuery的解決方案你可以做這樣的事情(看你標記的jQuery):

$(function() { 
    // get rows 
    $('table.table tbody tr').each(function() { 
     // get last cell in each row 
     var cell = $(this).find('td:last'); 
     // replace trailing spaces and commas 
     var text = cell.html().replace(/(,|\s)+$/, ''); 
     // update cell display 
     cell.html(text); 
    }); 
}); 

概念證明:http://jsfiddle.net/wPRNH/

0

得到那個結果s = s.replace(/[,\s]{2,}/,"");

後可以使用正則表達式
0

試試這個:

<td> 
    {% if choice.tag1 %} 
     {{ choice.tag1 }}, 
    {% endif %} 
    {% if choice.tag2 %} 
     {{ choice.tag2 }}, 
    {% endif %} 
    {% if choice.tag3 %} 
     {{ choice.tag3 }}, 
    {% endif %} 
    {% if choice.tag4 %} 
     {{ choice.tag4 }}, 
    {% endif %} 
    {% if choice.tag5 %} 
     {{ choice.tag5 }}, 
    {% endif %} 
    {% if choice.tag6 %} 
     {{ choice.tag6 }}, 
    {% endif %} 
    {% if choice.tag7 %} 
     {{ choice.tag7 }}, 
    {% endif %} 
    {% if choice.tag8 %} 
     {{ choice.tag8 }}, 
    {% endif %} 
    {% if choice.tag9 %} 
     {{ choice.tag9 }}, 
    {% endif %} 
    {% if choice.tag10 %} 
     {{ choice.tag10 }} 
    {% endif %} 

    </td> 

或者試試這個使用jQuery:

 $(document).ready(function() { 
     $("#add_commas .manupulate_me span:not(span:last-child)").each(function() { 
      if ($.trim($(this).html()) != "") { 
       $(this).append(","); 
      } 
     }); 
    }); 

和更改過的HTML:

<table id="add_commas"> 
<tr class="manupulate_me"> 
<td> 
    <span id="sp1">value1</span> 
    <span id="sp2">vale2</span> 
    <span id="sp3"> </span> 
    <span id="sp4"> </span> 
    <span id="sp5"> </span> 
    <span id="sp6">value6</span> 
    <span id="sp7">value7</span> 
    <span id="sp8"> </span> 
    <span id="sp9">value9</span> 
    <span id="sp10"> value10</span> 
    </td> 
</tr> 
</table> 
+0

輸出就像:水,污染,你好,---我想輸出爲水,污染,你好 – pynovice

+0

好的我已經更新了答案試試這個。 – Gerry

+0

我想實現使用,否則不使用jQuery。刪除最後一個逗號。 – pynovice

0

嘗試使用

{{#choice.tag1}} 
    {{ choice.tag1 }}{{#choice.tag2}},{{/choice.tag2}} 
{{/choice.tag1}} 

每次出現。最後一個將是:

{{#choice.tag10}} 
    {{ choice.tag10 }} 
{{/choice.tag10}} 
相關問題