2011-04-05 128 views
1

我想創造這種輸出的創建數組變量

var s1 = [['Sony',7],['Samsung',5],['LG',8]]; 

,這樣我可以用它來對我的圖表,就像途經我的AJAX的resutl可變

success: function(data){ 

    //code to extract the data value here 

    var s1= need to create the data here 

    $.jqplot('chart',[s1],{ blah blah blah 

} 

成功函數中的「數據」返回此表格佈局

<table id="tblResult"> 
    <tr class="tblRows"> 
     <td class="clsPhone">Sony</td><td class="clsRating">7</td> 
    </tr> 
    <tr class="tblRows"> 
     <td class="clsPhone">Samsung</td><td class="clsRating">5</td> 
    </tr> 
    <tr class="tblRows"> 
     <td class="clsPhone">LG</td><td class="clsRating">8</td> 
    </tr> 
</table> 

你能幫我創建這個邏輯嗎?

在此先感謝

編輯: 我正在尋找一個解決類似如下:

var s1; 
$(".tblRows").each(function(){ 
    // here I don't know exactly on what to do 
    //s1.push($(".clsPhone").text(),$(".clsRating").text())); 
}); 
// all I wanted is to make the resul s1=[['Sony',7],['Samsung',5],['LG',8]]; 

因爲jqplot需要這種參數的

s1=[['Sony',7],['Samsung',5],['LG',8]]; 
$.jqplot('chart',[s1],{ 
     renderer:$.jqplot.PieRenderer, 
     rendererOptions:{ 
      showDataLabels:true, 
      dataLabelThreshold:1 
     } 
    } 
}); 

所以我正在尋找一種方法來從數據中創建變量s1的值這可能嗎?

+0

你想從一個HTML表格創建數組,對嗎? – fabrik 2011-04-05 11:15:51

+1

你有可能修改服務器端返回的數據嗎? – SadullahCeran 2011-04-05 11:16:28

+0

那麼...輸入是什麼樣子? – strager 2011-04-05 11:19:51

回答

13
var s1 = []; 
$(".tblRows").each(function(){ 
    // create a temp array for this row 
    var row = []; 
    // add the phone and rating as array elements 
    row.push($(this).find('.clsPhone').text()); 
    row.push($(this).find('.clsRating').text()); 
    // add the temp array to the main array 
    s1.push(row); 
}); 
+2

我認爲你可能需要將$(data)傳遞給初始選擇器來使它工作$(「。tblRows」,$(data))....這是因爲傳入數據不一定在這個文件中 – 2011-04-05 11:56:09

+0

啊,當我使用變量名'data'時,我沒有看到它已經被使用了 - 我已經改變了我的答案,將它重命名爲'row ' – 2011-04-05 12:50:39

+0

邏輯對我有效......從數據中獲得選擇對我來說不是一個大問題:)謝謝亞當 – 2011-04-06 09:23:10

0

你可以做這樣的事情:

var row = []; 
$(".tblRows").each(function() { 
    row.push([$(this).find('.clsPhone').text(), 
       $(this).find('.clsRating').text()]); 
}); 
$.jqplot('chart', [row], { 
    //... 
});