简体   繁体   English

如何在表中追加列:jquery

[英]How to append columns in the table: jquery

Summary: I have a template which contains two add buttons and two tables. 简介:我有一个模板,其中包含两个添加按钮和两个表。 When user clicks add button rows should append to the respective table. 当用户单击添加按钮行时,应附加到相应的表。 Example: 例:

    <div class="reports">
   <div class="panel">
    <table class="A">
     <thead>
      <td>A</td>
      <td>B</td>
      <td>C</td>
      <td>D</td>
      <td>E</td>
      <td>F</td>
     </thead>
     <tbody></tbody>
    </table>
   </div>
   <button class="add" type="button">Add</button>
  </div>

and jquery is 和jquery是

$('.reports').on('click','.add',function(){
    var path = $(this).parents('.reports').find(.A tbody);
    $(
            '<tr>'+
            '<td class="remove-row" role="button" aria-label="Remove Region"></td>'+
            '<td>'+value1+'</td>'+
            '<td><button>'+value2+'</button></td>'+
            '<td>'+value3+'</td>'+
            '<td>'+value4+'</td>'+
            '<td class="ordering" aria-label="Re-order"></td>'+
            '</tr>'
        ).appendTo(path);
});

I want to append first two columns and do something with the value2 and append value3,value4,lastcolumn. 我想附加前两列并使用value2执行某些操作并附加value3,value4,lastcolumn。 example: 例:

$('.reports').on('click','.add',function(){
        var path = $(this).parents('.reports').find('.A tbody');
        $('<tr>'+'<td class="remove-row" role="button" aria-label="Remove Region"></td>'+
        '<td>'+value1+'</td>').appendTo(path to first two columns);
        $('<td><button>'+value2+'</button></td>').appendTo(path to third column);
        $('<td>'+value3+'</td>'+'<td>'+value4+'</td>'+
         '<td class="ordering" aria-label="Re-order"></td>'+'</tr>').appendTo(path to last 3 columns);

    });

I looking for the logic here. 我在这里寻找逻辑。

Whilst I'm not totally sure of your aim, you can use insertCell() in order to add the 'columns' you need. 虽然我不完全确定你的目标,但你可以使用insertCell()来添加你需要的“列”。 Once you have this you can then populate as you need 一旦你有了这个,你就可以根据需要填充

https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/insertCell https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/insertCell

I am not exactly sure what you are asking, but in order to add a column in the middle of a table, you can use the following code/function. 我不确定你在问什么,但是为了在表的中间添加一列,你可以使用下面的代码/函数。 To add a row, use your existing function, just make sure to add the quotes inside your .find call where you are setting path, or it will fail. 要添加一行,请使用现有功能,只需确保在设置路径的.find调用中添加引号,否则它将失败。 The code below assumes you want to add a column after the second column (index of 1) 下面的代码假定您要在第二列之后添加一列(索引为1)

$('.reports').on('click', '.add', function () {
    $('.reports').find('.A tr').each(function () {
        $(this).find('td:eq(1)').after('<td>test</td>');
    });
});

 $('.reports').on('click', '.add', function () { $('.reports').find('.A tr').each(function () { $(this).find('td:eq(1)').after('<td>test</td>'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="reports"> <div class="panel"> <table class="A"> <thead> <tr> <td>A</td> <td>B</td> <td>C</td> <td>D</td> <td>E</td> <td>F</td> </tr> <tr> <td>A</td> <td>B</td> <td>C</td> <td>D</td> <td>E</td> <td>F</td> </tr> </thead> <tbody></tbody> </table> </div> <button class="add" type="button">Add</button> </div> 

I am not sure what you are upto but Try adding the condition before append and push element to appendTo(). 我不确定你是做什么但尝试在append和push元素添加到appendTo()之前添加条件。 Or you can use jQuery Chaining. 或者你可以使用jQuery Chaining。

$('.reports').on('click','.add',function(){
    if (someInput = "Map"){
       var value2 += //do Something;
    }else{
       var value2 += someInput;
    }
    var path = $(this).parents('.reports').find(.A tbody);
    var newRow = $(
            '<tr>'+
            '<td class="remove-row" role="button" aria-label="Remove Region"></td>'+
            '<td>'+value1+'</td>'+
            '<td><button>'+value2+'</button></td>'+
            '<td>'+value3+'</td>'+
            '<td>'+value4+'</td>'+
            '<td class="ordering" aria-label="Re-order"></td>'+
            '</tr>'
        );
      newRow.appendTo(path)
      //You can do here whatever you want because the table is in DOM now          
});

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

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