Jquery move table rows to top
Asked Answered
D

3

5

First off, I stored all my trs with a function, and then I selected part of the trs with this, opening them:

// tr = all my stored trs
tr.find("input[value='Selecteren']").click();
// This .click() function changes the input value to "Aanvragen"

Now I want to move all the clicked tr's to the top of my table body.

//$("#village_troup_list tbody")

Getting all the tds is quite simple:

tr.find("input[value='Aanvragen']").closest('tr').each(function() {
 //Move every tr
})

But how do I move them?

Html structure:

http://jsfiddle.net/4PFf8/1/

Dealing answered 29/1, 2014 at 15:5 Comment(2)
Can you show your HTML structure? jQuery does not include a move function. You'll need to use clone(), appendTo() and remove() in some order (depending upon your mark up).Offshore
$("table").find("tbody").find("tr:first").insertBefore("trs to move") try this.Par
O
8
 $("#village_troup_list tbody").prepend(tr.find("input[value='Aanvragen']").closest('tr'));

This works because every tr is viewed as a single tr, and not as a number of trs. So it moves them instead of cloning :)

Ontine answered 29/1, 2014 at 15:40 Comment(1)
thanks this worked. can do this with animation?Elly
D
6

You can just use .prependTo to move elements to the top of the table. Here is a fiddle illustrating: http://jsfiddle.net/5uc9H/

Disesteem answered 29/1, 2014 at 15:14 Comment(0)
G
1

Statically move up:

onclick="$(this).parents('tr:first').insertBefore($(this).parents('tr:first').prev())"

Statically move down:

onclick="$(this).parents('tr:first').insertAfter($(this).parents('tr:first').next())"
Gunpaper answered 15/12, 2015 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.