简体   繁体   English

jQuery-从PHP将多个列追加到表

[英]JQuery - append multiple columns to a table from PHP

I've looked at several questions such as this one: Add table row in jQuery and this one: jQuery add HTML table column But my situation is a bit different. 我已经看过几个问题,例如: 在jQuery中添加表行,以及这个问题: jQuery添加HTML表列但是我的情况有些不同。

I have an existing table and I want to add multiple columns to the end. 我有一个现有的表,我想在最后添加多个列。 I've tried: 我试过了:

$('#myTable tr:last').after
$('#myTable > tbody:last').append
$("#myTable > tbody").append
$('#myTable > tbody:first').append
$('#myTable').find('tbody:last').append

I think this is because what I'm bringing in are separate rows created by PHP. 我认为这是因为我要引入的是由PHP创建的单独的行。

I do some calculations and echo back html rows like this: (keep in mind the comments are there to show you the format. I don't actually echo back those comments) 我进行一些计算并像这样回显html行:(请注意,注释在那里向您展示了格式。我实际上并没有回显那些注释)

<!--<tr><td>11133150.5</td><td>-6027209.5</td><td>31</td></tr><tr><td>11133678.8</td><td>-6027083.6</td><td>31</td></tr><tr><td>11133027.7</td><td>-6027256.7</td><td>31</td></tr><tr><td>11133136.9</td><td>-6027178.9</td><td>31</td></tr><tr><td>11133175.1</td><td>-6027538.3</td><td>31</td></tr><tr><td>11132970.5</td><td>-6027393.1</td><td>31</td></tr><tr><td>11133117.4</td><td>-6026786.8</td><td>31</td></tr><tr><td>11133507.2</td><td>-6027230.5</td><td>31</td></tr><tr><td>11133733.8</td><td>-6027372.2</td><td>31</td></tr><tr><td>11133444.4</td><td>-6026785.8</td><td>31</td></tr><tr><td>11133953.7</td><td>-6027232.9</td><td>31</td></tr><tr><td>11133073.2</td><td>-6027232.7</td><td>31</td></tr><tr><td>11134175.4</td><td>-6027043.2</td><td>31</td></tr><tr><td>11133973.5</td><td>-6026862.6</td><td>31</td></tr><tr><td>11133548.3</td><td>-6026404.2</td><td>31</td></tr><tr><td>11133327.9</td><td>-6026073.9</td><td>31</td></tr><tr><td>11133105.4</td><td>-6026271.5</td><td>31</td></tr><tr><td>11133154.9</td><td>-6027287.6</td><td>31</td></tr><tr><td>11133179</td><td>-6027305.4</td><td>31</td></tr><tr><td>11133223.6</td><td>-6027247.9</td><td>31</td></tr><tr><td>11133128.7</td><td>-6027094.8</td><td>31</td></tr><tr><td>11133010.9</td><td>-6027277.4</td><td>31</td></tr><tr><td>11133241.8</td><td>-6027367.1</td><td>31</td></tr><tr><td>11133361.6</td><td>-6027437.1</td><td>31</td></tr><tr><td>11133298.1</td><td>-6027167.3</td><td>31</td></tr><tr><td>0</td><td>166021.4</td><td>31</td></tr>--!>

When I try to add them to the existing table (of the same length) I get it on the end of the table (underneath it) or I've had it nested inside, but all I want is for it to append to the end of the table as new columns. 当我尝试将它们添加到现有表(长度相同)时,我将其放在表的末尾(在表的下面),或者将其嵌套在其中,但是我只想将其追加到末尾表的新列。

I'm not sure if this is the best way to do this, or if there's something else I'm missing. 我不确定这是否是执行此操作的最佳方法,或者我是否缺少其他内容。

Update To help anyone help me figure this out I made a fiddle: Fiddle Update帮助谁能帮我算出这个我做了一个小提琴: 小提琴

Thanks for the help. 谢谢您的帮助。

If you're trying to add columns, you need to append new td's to the tr's. 如果要添加列,则需要将新的td附加到tr上。 You can't have those new tr tags. 您不能拥有这些新的tr标签。 You'll have to bring each "row" (set of tds) of new data in separately, and append each set of tds to the correct tr. 您必须分别引入新数据的每个“行”(tds组),并将每组tds附加到正确的tr。 Your problem is all your trs are automatically making new rows. 您的问题是所有trs都会自动创建新行。

I don't know how much control you have over all of this, but what I've tried to do in the past is keep my data as actual data that mimics the same row/col format of an html table, meaning json that looks something like this: 我不知道您对这一切有多少控制权,但是我过去尝试做的是将我的数据保留为模仿html表的相同行/列格式的实际数据,即看起来像json的像这样的东西:

[
   [ 1, 2, 3, 4, 5, 6 ],
   [ 1, 2, 3, 4, 5, 6 ],
   [ 1, 2, 3, 4, 5, 6 ]
]

And then write a jquery script that can convert this into an html table whenever you tell it to (basically something using nested "for" loops). 然后编写一个jquery脚本,只要您告诉它就可以将其转换为html表(基本上是使用嵌套的“ for”循环的东西)。 Then you can just bring the data in, and update it as you need. 然后,您可以将数据导入并根据需要进行更新。

But for how you're doing it, it is defintely tough. 但是对于您的操作方式,这确实很困难。 You could maybe take your new html, tr tags and all, put it in a new, hidden table somewhere in your DOM, and then try and parse that and move the tds in chunk by chunk. 您可能会使用新的html,tr标签和所有标签,将其放在DOM中新的隐藏表中,然后尝试对其进行解析并将tds逐块移动。 That's all I can think of for a pure jQuery solution. 对于纯jQuery解决方案,这就是我能想到的全部。

Your code doesn't work as intended because you are inserting an entire table into the first occuring <tr> element into your first table. 您的代码无法按预期工作,因为您要将整个表插入第一个出现的第一个<tr>元素。 In order to loop through all rows correctly, you will have to first process your incoming HTML. 为了正确遍历所有行,您将必须首先处理传入的HTML。 Here's the strategy: 这是策略:

  1. Convert your table2 into a jQuery object. table2转换为jQuery对象。 However, remember that since jQuery .find() cannot search for sibling elements at the top level (which is <tr> in your case), we have to wrap it first. 但是,请记住,由于jQuery .find()无法在顶层 (在您的情况下为<tr> .find()中搜索同级元素,因此我们必须先将其包装。
  2. Wrap your original table2 HTML string by simply adding <table></table> around it. 只需在其周围添加<table></table>即可包装原始的table2 HTML字符串。 .wrap() does not work here because of the same reason stated above, so we do a simple string concatenation. 由于与上述原因相同, .wrap()在这里不起作用,因此我们进行了简单的字符串连接。
  3. Now we have your jQuery object ready to use. 现在,我们可以使用您的jQuery对象了。 We use .find() to fish out all the <tr> , loop through each of them—find the cell in each table row, and append them to your existing table row by index. 我们使用.find()提取所有<tr> ,循环遍历每个<tr> -查找每个表行中的单元格,然后将它们按索引附加到您现有的表行中。 Since your first row is simply a header, we offset the index by 1 (using i+1 ). 由于您的第一行只是标题,因此我们将索引偏移1(使用i+1 )。

Without further ado, here is the code: 事不宜迟,这里是代码:

// Wrap your second table with <table>, only because jQuery cannot find sibling elements (i.e. at the same level)
var $t2 = $('<table>'+table2+'</table>');
$t2.find('tr').each(function(i) {
    var $row = $('#test table tbody tr').eq(i+1);
    $(this).find('td').appendTo($row);
});

And the working fiddle: http://jsfiddle.net/teddyrised/wkezt8o6/1/ 和工作的小提琴: http : //jsfiddle.net/teddyrised/wkezt8o6/1/

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

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