Copy multiple Tds - javascript

I have a table with 3 columns and 6 rows. I want to copy the contents of some of them into a newly created column, but insert them in the middle. So that it looks like that:
With this aprroach:
$("table td:nth-child(2) [id$=2]").each(function(i) {
var $newCell = $(this).wrap('<td></td').parent();
var $newRow = $("table td:nth-child(2) [id$=1]").eq(i).parents('tr');
$(this).parents('tr').remove();
$newRow.append($newCell);
});
$("table td:nth-child(2) [id$=3]").each(function(i) {
var $newCell = $(this).wrap('<td></td').parent();
var $newRow = $("table td:nth-child(2) [id$=1]").eq(i).parents('tr');
$(this).parents('tr').remove();
$newRow.append($newCell);
});
I get that result:
So the new columns should be inserted between the very left column and the column with Abc and not at the very right.
FIDDLE.

To insert something in the middle of the table what you can do is use either before() or after(). For example, we want to insert our new cell before the Abc item so instead of inserting it on the end you can use the below code to insert it before Abc (being the last td):
// Find the last element, and add the new cell before it.
$newRow.find("td:last").before($newCell);
Fiddle Here
If you would like to be more specific, you can specify the element by it's text in the selector. So in this case you can state before a td element containing the text 'Abc':
$newRow.find("td:contains('Abc')").before($newCell);

Related

How to loop through each table row and swap two rows using javascript

Is there an inbuilt function or how do I create one on my own? I have tried what was done with swapping two cells with seemingly appropriate changes. But it didn't work.
This is what was written for swapping two cells. I want to swap rows and they're not necessarily consecutive. They can have multiple rows between them.
$('table tbody tr td:nth-child(3)').each(function() {
let $td = $(this);
$td.insertBefore($td.prev());
});
You could iterate over rows this time:
$('table tbody tr:nth-child(3)').each(function() {
let $tr = $(this);
$tr.insertBefore($tr.prev());
});
The following will place the third row (index: 2) before the second (index: 1):
// prepare a sample table:
$("table tbody").html([...new Array(5)].map((_,i)=>`<tr><td>${i}</td><td>some further text and a number ${i*i*i}</td></tr>`).join("\n"))
let $tr=$('table tbody tr:nth-child(3)')
$tr.insertBefore($tr.prev());
td {padding:6px; border:1px grey solid}
tr:nth-child(odd) {background-color:#ddd}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table><tbody></tbody></table>
Unlike in the case for columns there is no iteration required here, as the row-exchange will happen only once!

How to remove empty th and td from thead and tbody from the HTML table which has no id, jQuery or Javascript

How do we remove th from thead and td from tbody from the table which has no id element attached to it?
Please find the image for more reference.
I could not reuse any of the Javascript/jQuery functions available to remove empty td or th, since the table element has no id attached to it.
I won't be able to add any id for the table since it is generated by JSF, also only one HTML table is available in the page I am trying to delete the empty th and td.
Try this: you can use parent div selector to find the table and then its header and td elements to check if these elements are empty and delete it
$(function(){
$("div.ui-datatable-tablewrapper table[role=grid] thead tr th").each(function(){
var text = $(this).text();
if(!text && !$(this).find('input').length)
{
$(this).remove();
}
});
$("div.ui-datatable-tablewrapper table[role=grid] tbody tr td").each(function(){
var text = $(this).text();
if(!text && !$(this).find('input').length)
{
$(this).remove();
}
});
});

delete rows between tables with identical tr id

I have two tables and with same tr ids and content (for some reason)!
When I click a check a box in table1 I should be able to delete that row in both table1 and table2 etc. How can I achieve this?
I can delete from table1 using
table1.on('click','tr .lowBox:checked',function(){
$(this).closest('tr').remove();
}
How do I delete row from table2.
thanks!
table1.on('click','tr .lowBox:checked',function(){
$(this).closest('tr').remove(); //send to var to perform as below
$("table2").closest('tr').remove(); //If you're traversing UP
$("table2").find('tr').remove(); //If you're traversing DOWN
//Inside your "click" event, you can traverse any part of the DOM
//regardless of where you entered the document with your click event
//slightly more robust, you could do this..
/*or as fed variables...e
var $item1 = $(this).closest('tr');
var $item2 = $("table2").closest('tr');
var $rmTwo = function(item1,item2){
$(item1).remove();
$(item2).remove();
}
//Then execute your repeatable function, using the two tr's
$rmTwo($item1,$item2);
// should remove both, and you can play
//with your jQuery to get the correct elements
//or alter them if you change your code structure.
}
This would be the closest I can get you without seeing any of your HTML, and under the assumption that you're using jQuery on your page.
As others have commented, you shouldn't have duplicate IDs. Instead you could use classes, or generate IDs that are unique (for example, by prefixing with the table id). However, if you must do it this way, here's what you could do:
table1.on('click','tr .lowBox:checked',function(){
var row = $(this).closest('tr');
table2.children("#" + row[0].id).remove();
row.remove();
}
If you switch to table-unique classes for each row:
table1.on('click','tr .lowBox:checked',function(){
var row = $(this).closest('tr');
table2.children("." + row[0].className).remove();
row.remove();
}
This solution makes a few assumptions about the structure of your HTML. I can update it if you post a more detailed sample of your HTML.
I solved this with:
table1DT=var $('#table1').dataTable({});
table2DT=var $('#table2').dataTable({});
table1DT.on('click','tr .lowBox:checked',function(){
var row= $(this).closest('tr');
//do some thing with row variable
var d=row.attr('id');
var nRow = $('#table2 tbody tr[id='+d+']')[0];
table2DT.fnDeleteRow(nRow);
table1DT.fnDeleteRow(row);
}
so checking the table1 check box would delete that particular row in table1 and table2 etc.

How to get TD from next row with same index as current TD

I have a large HTML table where all rows of the body have the same structure.
Within this table there are editable TDs (which have the class "editable" and contain a contenteditable div) and non-editable TDs (which dont't have the class "editable" and do not contain a div).
Now I am trying to get the TD from the next row that has the same index as the current (closest) TD.
The below code gives me the correct index of my current TD within its row (and looking only at editable TDs).
Can someone tell me how I can get the equivalent TD of the next row ?
My jQuery:
$(document).keydown(function(e) {
var current = $(e.target);
var editables = $(current).closest('tr').find('td.editable');
var count = editables.length;
alert( editables.index($(current).closest('td')) ); // for testing
// ...
});
Instead of the alert I am looking for something like the following:
$(current).closest('tr').next('tr').find( /* the td with class editable AND the index matching the above */ );
Example:
If I am currently on the 4th editable TD in a row I would then need the 4th editable TD in the next row.
Try using :eq() like
$(document).keydown(function(e){
var current = $(e.target);
var editables = current.closest('tr').find('td.editable');
var count = editables.length;
var index = editables.index(current.closest('td'));
current.closest('tr').next('tr').find('td:eq('+index+')');
});
As commented above, you can use current instead of $(current)

How do I insert a new TR into the MIDDLE of a HTML table using JQuery?

I know how to append a new row to a table using JQuery:
var newRow = $("<tr>..."</tr>");
$("#mytable tbody").append(newRow);
The question is how do I create a new row that precedes some existing row.
var newRow = $("<tr>...</tr>");
$("#idOfRowToInsertAfter").after(newRow);
The key is knowing the id of the row you want to insert the new row after, or at least coming up with some selector syntax that will get you that row.
jQuery docs on after()
where_you_want_it.before(newRow)
or
newRow.insertBefore(where_you_want_it)
-- MarkusQ
Rather than this:
$("#mytable tbody").append(newRow);
you are going to want to do something like this:
$("#id_of_existing_row").after(newRow);
With:
var newTr = $('<tr>[...]</tr>');
You can…
Insert it after (or before if you so choose) another row for which you know an ID (or whatever other property):
$('#<id of the tr you want to insert the new row after>').after(newTr)
Insert it after a particular row index (indices are 0-based, not 1-based):
$($('table#<id> tr')[<index>]).after(newTr)
…or as you mentioned, the absolute middle is possible:
var existingTrs = $('table#<id> tr')
$(existingTrs[parseInt(existingTrs.length / 2)]).after(newTr)
If for example u place an insert image into your table this will be something like this :
Your last cell in your table :
<td> <img class=\"insertRow\" src=\"/images/imgInsertRow.jpg\" /> </td>
Your jquery code :
$('table td img.insertRow').click(function(){
var newRow=$('<tr>........</tr>');
$(this).parent().parent().after(newRow);
});

Categories