2012-11-29 48 views
0

從數據庫打印的動態數據/表格。需要得到多少次球隊勝,結束,失去。超新的jQuery,並不知道如何開始編碼(?!)。我正在考慮首先獲得切爾西出現的位置,然後比較分數,使用陣列存儲W,T,L並在最後打印結果(?)。在分數表上獲取贏,綁定,失敗結果

這是表的樣子:

<table> 
<tr id="12"> 
    <td class="238 ekipi_1">Chelsea</td> 
    <td class="231 ekipi_2">ManU</td> 
    <td class="goals_238">4</td> 
    <td class="goals_231">2</td> 
</tr> 

<tr id="13"> 
    <td class="242 ekipi_1">Reading</td> 
    <td class="238 ekipi_2">Chelsea</td> 
    <td class="goals_242">3</td> 
    <td class="goals_238">3</td> 
</tr> 

<tr id="16"> 
    <td class="251 ekipi_1">Swansea</td> 
    <td class="238 ekipi_2">Chelsea</td> 
    <td class="goals_251">5</td> 
    <td class="goals_238">3</td> 
</tr> 
</table> 

其結果將是:

  W T L 

Chelsea | 1 | 1 | 1 | 
Reading | 0 | 1 | 0 | 
ManU | 0 | 0 | 1 | 

了先機任何提示將不勝感激。

+1

或者可能用服務器端的數據來渲染它,而不是試圖解析dom? – mpm

+0

該表不會像這樣。它會有像切爾西這樣的行ManU | 4 | 2'和'Reading |切爾西| 3 | 3' –

回答

1

來讀取原始數據,並建立可以做到這一點的對象..

var teams = { 
    "Chelsea": {"W": 0, "T": 0, "L": 0}, 
    "ManU": {"W": 0, "T": 0, "L": 0}, 
    "Reading": {"W": 0, "T": 0, "L": 0}, 
    "Swansea": {"W": 0, "T": 0, "L": 0} 
}; 

$('table tr').each(function() { 
    var $this = $(this); 
    var team1 = $this.find('td:eq(0)').text(); 
    var team2 = $this.find('td:eq(1)').text(); 
    var score1 = $this.find('td:eq(2)').text(); 
    var score2 = $this.find('td:eq(3)').text(); 

    if(+score1 === +score2){ 
     teams[team1]["T"] = +teams[team1]["T"] +1; 
     teams[team2]["T"] = +teams[team2]["T"]+1; 
    } 
    else if(+score1 > +score2){ 
     teams[team1]["W"] = +teams[team1]["W"]+1; 
     teams[team2]["L"] = +teams[team2]["L"]+1; 
    } 
    else{ 
     teams[team1]["L"] = +teams[team1]["L"]+1; 
     teams[team2]["W"] = +teams[team2]["W"]+1; 
    }   
}); 

// Print the data 

$.each(teams, function(key,value){ 
    var $clone = $('#template'); 
    $clone.find('.team').html(key); 
    $clone.find('.score:eq(0)').html(value["W"]); 
    $clone.find('.score:eq(1)').html(value["T"]); 
    $clone.find('.score:eq(2)').html(value["L"]); 
    $('#output').append($clone.html()); 
}); 

Check Fiddle

您可以閱讀團隊對象,並打印出來,一旦你完成。

+0

感謝Sushanth /因爲我是jQuery的新手,我會如何從我的問題(例如W | T | L)打印表格作爲例子//爲了成爲一個完整的noob而道歉。 –

+0

這是另一個[fiddle](http://jsfiddle.net/N2Wbx/1/),打印出表格。 – czarchaic

+0

@OctoberEleven ..檢查更新的帖子和小提琴 –