2016-03-21 48 views
0

我想连接字符串以将它传递给数据表,我从servlet获取数据,当我把静态字符串的作品,但我不知道如何处理它使用循环,因为它是动态数据如何使用javascript呈现数据集中的dataSet

function getMsgHandler(responseTxt, statusTxt, xhr) { 
    alert(responseTxt); 
    if (statusTxt === "success") { 
     products = responseTxt; 
     var s = ""; 
     for (i = 0; i < products.length; i++) { 
      if (i === 0) { 
       s = s + '[' + products[i].productID + ', "' + products[i].name + '", "' + products[i].description + '", "' + products[i].img + '", ' + products[i].price + ' ]'; 
      } else { 
       s = s + ', [' + products[i].productID + ', "' + products[i].name + '", "' + products[i].description + '", "' + products[i].img + '", ' + products[i].price + ']'; 
      } 
     } 
     dataSet = '[ ' + s + ' ]'; 
     $('#example').DataTable({ 
      data: dataSet 
     }); 
    } 
} 

我希望它是这样的格式

var dataSet = [ 
    ["m", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800"], 
    ["Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000"], 
    ["Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "6224", "2012/03/29", "$433,060"], 
    ["Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700"] 
]; 
+0

余吨如果你向我们展示你拥有的数据以及你想要的东西,那么它会容易得多。如果您控制服务器,直接输出正确的格式可能会更容易。 – adeneo

+0

你想要的格式是一个数组数组。你应该从这个角度来看待它,而不是一个括号括起来的字符串。 –

回答

0

使用下面的代码来代替:

function getMsgHandler(responseTxt, statusTxt, xhr) { 
    if (statusTxt === "success") { 
     $('#example').DataTable({ 
      data: responseTxt, 
      columns: [ 
       { data: 'productID' }, 
       { data: 'name' }, 
       { data: 'description' }, 
       { data: 'img' }, 
       { data: 'price' } 
      ] 
     }); 
    } 
} 
+0

它没有与我一起工作! –

+0

@AyaRadwan,请发布'responseTxt'的内容。 –