简体   繁体   English

HTML表数据到javascript数组

[英]HTML table data to javascript array

I have a html table to show images displayed on my site in a carousel. 我有一个html表,用于显示轮播中显示在我网站上的图像。 The images have a position in which they are displayed and this is shown in the table. 图像具有显示位置,并且在表格中显示了这些位置。 One of the features of this table is that it uses the jQuery .sortable() function. 该表的功能之一是它使用jQuery .sortable()函数。 If an image is moved to the top of the table, the position of that image is changed to 0 and the subsequent positions are altered. 如果将图像移到表格的顶部,则该图像的位置将更改为0,随后的位置也会更改。 This is all working fine and I am able to capture the position in a variable. 一切正常,我能够捕获变量中的位置。 However, what I now need to do is jQuery POST the data in the table, so I can update the fields in my database with the new positions any any other information that has been updated. 但是,我现在需要做的是jQuery POST表中的数据,因此我可以使用新的位置更新数据库中的字段,并保留所有其他已更新的信息。

Ideally I need to be able to post an javascript associative array with the ID of the image as the key, and then the image information such as position and location in the sub array. 理想情况下,我需要能够发布一个以图像ID为键的javascript关联数组,然后发布图像信息,例如子数组中的位置和位置。

if it where php I imagine it would look something like this. 如果它在PHP我想它看起来像这样。

Array
(
    [45] => Array
        (
            [name] => Image 45
            [location] => img/Banner-45.jpg
            [url] => http://www.exampleurl2.com
            [position] => 0
        )

    [56] => Array
        (
            [name] => Image 56
            [location] => img/Image-56.jpg
            [url] => http://www.exampleurl2.com
            [position] => 1
        )

)

which I could loop through and update the values in the table. 我可以遍历并更新表中的值。 All my attempts to create an array with this similar format in javascript/jquery have failed so far. 到目前为止,我所有尝试在javascript / jquery中使用这种相似格式创建数组的尝试都失败了。

I have this loop which captures the details in jQuery 我有这个循环,它捕获了jQuery中的详细信息

 $("#banners tbody tr").each(function () {
        var id = $('td:first', $(this)).text();
        var name = $('td:nth(1)', $(this)).text();
        var pos = $('td:nth(2)', $(this)).text();
        var url = $('td:nth(3)', $(this)).text();
        var loc = $('td:nth(5)', $(this)).text();


});

I understand that this is not the most efficient method of capturing the data from the tables but I am relatively new to jQuery. 我知道这不是从表中捕获数据的最有效方法,但是我对jQuery还是比较陌生的。 Now I need to insert these into some form of associative array with the ID as the key within the loop. 现在,我需要将这些插入到某种形式的关联数组中,以ID作为循环内的键。 Any pointers in the right direction or a more effective method would be greatly appreciated. 任何朝着正确方向或更有效方法的指针将不胜感激。

You can use jquery map function to traverse all <tr> 's from the table and create data for rows, then traverse all <td> 's inside the current <tr> for the column data : 您可以使用jquery map函数遍历表中的所有<tr>并为行创建数据,然后遍历列数据的当前<tr>内部的所有<td>

var data = [];
var headers = [];
var tb = $('table.table');
var thead = tb.find('thead');
var tbody = tb.find('tbody');
thead.find('th').map(function(i,el){
   var $el = $(el);
   headers.push($el.attr('class'));
});
tbody.find('tr').map(function(i,el){
    var $el = $(el);
    var row = {};
    $el.find('td').map(function(i,el){
       var col = $(el).text();
       row[headers[i]] = col;
    });
    data.push(row);
});

sample jsfiddle : http://jsfiddle.net/v6ZLj/1/ 样本jsfiddle: http : //jsfiddle.net/v6ZLj/1/

There are no associate arrays in JavaScript so to speak. 可以这么说,JavaScript中没有关联数组。 In JavaScript arrays are one dimensional, you retrieve items based on their index within the array. 在JavaScript数组是一维的情况下,您将根据项目在数组中的索引来检索项目。 You are talking about objects or object literals. 您在谈论对象或对象文字。 I've coded an example and example structure for you below. 我在下面为您编写了一个示例和示例结构。

var saveObj = {}; //Setup a blank object for the data;

$("#banners tbody tr").each(function () {

   var id = $('td:first', $(this)).text();

   saveObj[id] = {
        name : $('td:nth(1)', $(this)).text(),
        pos : $('td:nth(2)', $(this)).text(),
        url : $('td:nth(3)', $(this)).text(),
        loc : $('td:nth(5)', $(this)).text()
    };
});

This sets up a blank object literal first called saveObj . 这将设置一个空白的对象常量,该常量首先称为saveObj This is your blank canvas so to speak. 可以这么说,这是您的空白画布。 Then within your loop you are grabbing the id as normal. 然后,在循环中,您将像正常情况一样获取ID。 This will be your key within the object and the value for the key will be another object literal which we create dynamically within the loop. 这将是您在对象内的键,而键的值将是我们在循环中动态创建的另一个对象文字。 using the same keys as your variables (name, pos, url, loc) with their data values in the same way you are grabbing them now. 使用与变量(名称,位置,URL,位置)相同的键及其数据值,就像现在抓取它们的方式一样。 Your structure will be like this 您的结构将如下所示

var saveObj = {
    "12" : {
       name : "YourName",
       pos : "YourPos",
       url : "YourUrl",
       loc : "YourLoc"
    },
    "16" : {
       name : "YourName2",
       pos : "YourPos2",
       url : "YourUrl2",
       loc : "YourLoc2"
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM