简体   繁体   English

使用动态ip从html表获取数据并在javascript对象中填充数据

[英]Getting data from html table with dynamic ip and filling the data in javascript object

I have a page that creates multiple tables dynamically 'all with static table class name', but each table does have a unique id name. 我有一个动态创建多个表的页面'全部使用静态表类名',但每个表都有一个唯一的id名称。

Users can edit each table and then save the changes on submit. 用户可以编辑每个表,然后在提交时保存更改。 What is required is that the data in the amended table will be selected and sent to the server. 所需要的是将修改表中的数据并将其发送到服务器。 I implemented the code using a single table with the class name, which worked perfectly, below is the code: 我使用一个带有类名的表来实现代码,这个代码完美无缺,下面是代码:

    var rows = [].slice.call($('.TableStaticClassName')[0].rows);

    var keys = [].map.call(rows.shift().cells, function (e) {
        return e.textContent.replace(/\s/g, '');
    });

    var result = rows.map(function (row) {
        return [].reduce.call(row.cells, function (o, e, i) {
            o[keys[i]] = e.textContent;
            return o;
        }, {})
    });

The problem is when I tried to use this code when having multiple tables. 问题是我有多个表时尝试使用此代码。 What i have now looks like this for example: 我现在看起来像这样的例子:

<table class="TableStaticClassName" id="12345"></table>
<table class="TableStaticClassName" id="23456"></table>
<table class="TableStaticClassName" id="34567"></table>

When I have multiple tables I wont be able to use the table class name since it will select the data from all the tables. 当我有多个表时,我将无法使用表类名,因为它将从所有表中选择数据。 So when i try to select the data from a single table using the table id with code like the below 因此,当我尝试使用表格id从单个表格中选择数据时,代码如下所示

var x = document.getElementById("12345").rows[0].cells.length;

or even the example I use on single tables like 甚至是我在单个表上使用的例子

var rows = [].slice.call($('#12345')[0].rows);

I get an error 'TypeError: undefined is not an object' and I'm not able to read the row content of the changed table. 我得到一个错误'TypeError:undefined不是一个对象',我无法读取更改的表的行内容。 I tried different solutions and implementations, but they all ended up giving me the same error. 我尝试了不同的解决方案和实现,但它们最终都给了我同样的错误。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you in advance 先感谢您

I actually found a solution that worked, so I'm going to post it in case someone had the same problem in the future. 我实际上找到了一个有效的解决方案,所以我将发布它以防将来有人遇到同样的问题。 The solution was to combine both the id along with the class name, below is the code snippet. 解决方案是将id和类名组合在一起,下面是代码片段。

var rows = [].slice.call($('#'+dynamicGeneratedIdVariable+'.TableStaticClassName')[0].rows);

        var keys = [].map.call(rows.shift().cells, function (e) {
            return e.textContent.replace(/\s/g, '');
        });

        var result = rows.map(function (row) {
            return [].reduce.call(row.cells, function (o, e, i) {
                o[keys[i]] = e.textContent;
                return o;
            }, {})
        });

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

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