2014-09-26 50 views
0

我有一個表像這樣的藥店把所有表中的行到數組的數組中的jQuery

Pharmacy Name | Pharmacy ID | Call | SMS | Email | P | Total 
------------------------------------------------------------ 
Test 1  | 123456  | 1 | 2 | 3 | 4 | 10 
------------------------------------------------------------ 
Test 2  | 123457  | 1 | 2 | 3 | 4 | 10 
----------------------------------------------------------- 

我想這個表導出到Excel工作表中的出站使用。當點擊一個按鈕並將這些數據傳遞給我的控制器時,我將如何去解決這個問題?

到目前爲止,我有

$("tr").each(function() { 

    alert($("td").text()); 
}); 

但它給我所有的TDS。

我希望能夠將每行的值存儲到一個對象中。

+0

有幾個插件HTML表格轉換爲csv,用你喜歡的搜索引擎找到他們 – charlietfl 2014-09-26 17:20:18

回答

1

我想你要找的是什麼:

$('tr').map(function(i, tr) { 
    return $(tr).find('td').map(function(j, td) { 
    return $(td).text(); 
    }); 
}); 

編輯: 如何怪異。 Jquery似乎把它們弄平了。

var parentArr = []; 
$('tr').each(function() { 
    var children = []; 
    $(this).find('td').each(function(){ 
    children.push($(this).text()); 
    }); 
    parentArr.push(children); 
}); 

這絕對可以使用ecmascript 5或下劃線/ lodash方法稍微優雅些。

+0

感謝這正是我期待的,我現在想創建一個數組數組。基本上我的輸出上面會有一個2個對象的數組。我發佈了我的下面 – user3562751 2014-09-26 17:46:56

+0

酷!非常感謝! – user3562751 2014-09-26 18:02:07

0

這裏是我迄今爲止

var items; 
    var data = []; 

    $('tr').map(function (i, tr) { 
     items = []; 
     return $(tr).find('td').map(function (j, td) { 

      items[j] = $(td).text(); 
      data[i].push(items[j]); 
      return $(td).text(); 
     }); 

     alert(data); 
    });