2011-04-05 129 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], { 
    //... 
});