如何使用jQuery推断更改表变量

我有这个表HTML代码:

这也是:

 

Weight in Motif

Segment & Stocks

Name of stock

100.0
test3
55.8 stock id3 Indian-Oil-Corporation-Ltd.
44.2 stock id4 Power-Finance-Corporation-Ltd.
100.0
test
55.8 stock id3 Indian-Oil-Corporation-Ltd.
44.2 stock id4 Power-Finance-Corporation-Ltd.
100.0
test2
55.8 stock id3 Indian-Oil-Corporation-Ltd.
44.2 stock id4 Power-Finance-Corporation-Ltd.

基本上这两个都是从erb文件生成的HTML代码。现在使用的第一个代码变量是这样的:

 h=[ { "folder" => "test3", "weight" => "100.0", "stocks" => [{ "id" => "stock id3", "name" => "Indian Oil Corporation Ltd.", "weight" => "55.8"}, { "id" => "stock id4", "name" => "Power Finance Corporation Ltd.", "weight" => "44.2" } ]}, { "folder" => "test", "weight" => "100.0", "stocks" => [{ "id" => "stock id3", "name" => "Indian Oil Corporation Ltd.", "weight"=> "55.8"}, {"id" => "stock id4", "name" => "Power Finance Corporation Ltd.", "weight"=> "44.2"}] }, { "folder" => "test2", "weight" => "100.0", "stocks" => [{ "id" => "stock id3", "name" => "Indian Oil Corporation Ltd.", "weight"=> "55.8"}, {"id" => "stock id4", "name" => "Power Finance Corporation Ltd.", "weight"=> "44.2" }] } ] 

而在我创建的第二个代码中,使用拖放更改了表,现在我如何获得更新的h变量,这将是:

 h=[ { "folder" => "test3", "weight" => "100.0", "stocks" => [{"id" => "stock id3", "name" => "Indian Oil Corporation Ltd.", "weight" => "55.8"}, {"id" => "stock id4", "name" => "Power Finance Corporation Ltd.", "weight" => "44.2"}, {"id" => "stock id3", "name" => "Indian Oil Corporation Ltd.", "weight" => "55.8"}, {"id" => "stock id4", "name" => "Power Finance Corporation Ltd.", "weight" => "44.2"}, {"id" => "stock id3", "name" => "Indian Oil Corporation Ltd.", "weight" => "55.8"}, {"id" => "stock id4", "name" => "Power Finance Corporation Ltd.", "weight" => "44.2"}] }, { "folder" => "test", "weight" => "100.0", "stocks" => [], { "folder" => "test2", "weight" => "100.0", "stocks" => [] } ] 

使用Javascript或其他东西(APP是ruby on rails)。 我完全不知道javascript plz给出了详细的解释。

使用jQuery你可以做这样的事情。

 (function ($) { retrieveTable = function () { // define an output table variable var h = []; // and an empty variable for the current folder var current_folder = {}; // define tables that contain the keys for your values inside the table keys = { titles: ['weight', 'folder', 'stocks'], stocks: ['weight', 'id', 'name'] }; // select all rows in your table and iterate through them $('#portfolios tr').each(function (index, row) { // make sure you don't cacth the row in thead // (another solution would be to have an actual tbody // and iterate over it directly $("#portfolios tbody tr")) if (!$(row).parents('thead').length > 0) { // check if the row is a title if ($(row).attr('id') == 'titles') { // if it is a title, that means that we are in a new folder // set the current_folder variable and add it to the table current_folder = {}; h.push(current_folder); // and fill it with the info from this first row $(row).children('th').each(function (iindex,title) { current_folder[keys.titles[iindex]] = cleanText($(title).text()); }); // define an array as the value for the 'stocks' key current_folder['stocks'] = []; } else { // if the 'tr' is not a 'titles', then it must be a 'Stocks' // you can fill it with the info with the table // before adding it to the current_folder['stocks'] array current_stock = {}; // your 'th' correspond to weight and id and 'tr' to stock name $(row).children('th, td').each(function (iindex,attribut) { current_stock[keys.stocks[iindex]] = cleanText($(attribut).text()); }); // then you can add the stock to the 'stocks' array current_folder['stocks'].push(current_stock); }; }; }); return h; }; cleanText = function (strg) { return strg.replace(/^\s+/,'').replace(/\s+$/,''); }; }(jQuery)); 

然后,当您检索到retrieveTable()对象时,将其发送回Rails服务器,将其解析为JSON以使其成为ruby Hash =>,它提供的格式与您从中开始的格式相同,并进行修改来自客户端的用户。

要与Rails端集成,请在此处查看我对第一个问题的回答:将HTMl表值反转为ruby变量

Weight in Motif

Segment & Stocks

Name of stock

100.0
test3
55.8 stock id3 Indian-Oil-Corporation-Ltd.
44.2 stock id4 Power-Finance-Corporation-Ltd.
100.0
test
55.8 stock id3 Indian-Oil-Corporation-Ltd.
44.2 stock id4 Power-Finance-Corporation-Ltd.
100.0
test2
55.8 stock id3 Indian-Oil-Corporation-Ltd.
44.2 stock id4 Power-Finance-Corporation-Ltd.