Ma He
Ma He

Reputation: 11

Jquery how to move up/down X rows the selected row

I have a HTML table with a checkbox on each row The goal is to make a UP or DOWN (button on the header of the table) for the rows that are checked.

But instead of move up/down one rows, I would like to move up/down 5 rows

I have finished the Up+1 and Down-1 but i dont know how to implement the Up+5 and Down-5 script

Here is my code

<button id="up">Up +1</button><button id="down">Down-1</button>
<button id="up5">Up +5</button><button id="down5">Down-5</button>

<table id="rowclick" class="table table-striped table-bordered table-hover 
flip-content">
<thead class="flip-header">
<tr>
<th colspan="2">
Rank                                                
</th>
<th>
ID                                              
</th>
<th>
Product                                             
</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
<tr class="odd gradeX parent parent-hide" id="item-2119">
<td>
16                                                      
</td>
<td>
2119                                                        
</td>
<td>
test                                                        
</td>
<td class="center">
<input type="checkbox">
</td>
</tr>
<tr class="odd gradeX parent parent-hide" id="item-2120">
<td>
17                                                      
</td>
<td>
2120                                                        
</td>
<td>
test1                                                   
</td>
<td class="center">
<input type="checkbox">
</td>
</tr>
<tr class="odd gradeX parent parent-hide" id="item-2121">
<td>
18                                                      
</td>
<td>
2121                                                        
</td>
<td>
Test2                                                       
</td>
<td class="center">
<input type="checkbox">
</td>
</tr>
<tr class="odd gradeX parent parent-hide" id="item-2122">
<td>
19                                                      
</td>
<td>
2122                                                        
</td>
<td>
Test 3                                                      
</td>
<td class="center">
<input type="checkbox">
</td>
</tr>
<tr class="odd gradeX parent parent-hide" id="item-2123">
<td>
20                                                      
</td>
<td>
2123                                                        
</td>
<td>
test4                                                       
</td>
<td class="center">
<input type="checkbox">
</td>
</tr>
<tr class="odd gradeX parent parent-hide" id="item-2464">
<td>
99999                                                       
</td>
<td>
2464                                                        
</td>
<td>
test 5                                                      
</td>
<td class="center">
</td>
</tr>

</tbody>
</table>


<script type="text/javascript">

$(document).ready(function(){
//Up-Down1
$('#up').on('click',function(event){
event.preventDefault();
$('#rowclick').find('tr').each(function(){

if($(this).find('input[type=checkbox]').is(':checked')){

var current = $(this).closest('tr')
var previous = current.prev('tr');
if(previous.length !== 0){
current.insertBefore(previous);
}
}
});
});


$('#down').on('click',function(e){
e.preventDefault();
$($('#rowclick').find('tr').get().reverse()).each(function(){

if($(this).find('input[type=checkbox]').is(':checked')){
var current1 = $(this).closest('tr')
var next = current1.next('tr');
if(next.length !== 0){
current1.insertAfter(next);
}
}
});
});
//End Up-Down1

//Up-Down5
$('#up5').on('click',function(event){
event.preventDefault();
$('#rowclick').find('tr').each(function(){

if($(this).find('input[type=checkbox]').is(':checked')){

var current = $(this).closest('tr')
var previous = current.prev('tr');
if(previous.length !== 0){
//current.insertBefore($("#rowclick").find("tbody").find("tr:nth(3)"));
current.insertBefore($('tr:eq(3)'));
}
}
});
});
});


</script>

Thanks you very much for your help

Upvotes: 1

Views: 1890

Answers (1)

Cybernetic
Cybernetic

Reputation: 13334

Table:

<table border='1'>
<th>RANK</th><th>ID</th><th>PRODUCT</th>
<tr><td>16</td><td>2119 test</td><td><input class='ch' type='checkbox'></td></tr>
<tr><td>17</td><td>2120 test1</td><td><input class='ch' type='checkbox'></td></tr>
<tr><td>18</td><td>2121 test2</td><td><input class='ch' type='checkbox'></td></tr>
<tr><td>19</td><td>2122 test3</td><td><input class='ch' type='checkbox'></td></tr>
<tr><td>20</td><td>2123 test4</td><td><input class='ch' type='checkbox'></td></tr>
<tr><td>99999</td><td>2464 test5</td><td><input class='ch' type='checkbox'></td></tr>
</table>

The following will allow you to move up 1, down 1, up 5, down 5:

$(document).on('click', '#up, #down, #up5, #down5', function() {
    button_choice = $(this).attr('id'); 
    $('.ch').each(function() {
       if($(this).is(':checked')) {
          row = $(this).closest("tr")
          if(button_choice == 'up') {
            row.insertBefore(row.prev())
          }
          if(button_choice == 'down') {
            row.insertAfter(row.next())
          }
          if(button_choice == 'up5') {
            for(i=0;i<4;i++) { row.insertBefore(row.prev()) }
          } 
          if(button_choice == 'down5') {
            for(i=0;i<4;i++) { row.insertAfter(row.next()) }
          }  
       }
})

enter image description here

*******EDIT********

Here is the code adapted to allow for multiple-selected rows, and to ensure you cannot shift above the headers of the table:

$(document).on('click', '#up, #down, #up5, #down5', function() {
    button_choice = $(this).attr('id'); 
    $('.ch:checked').each(function() {

          row = $(this).closest("tr");

          row.css('background', 'hotpink');
          checked_length = $('.ch:checked').length;

          if(button_choice == 'up') {
            if(row.index() > 1) {
            row.insertBefore(row.prev())
            }
          }
          if(button_choice == 'down') {
            for(i=0;i<checked_length;i++) { row.insertAfter(row.next()) }
          }
          if(button_choice == 'up5') {
            for(i=0;i<checked_length+3;i++) { if(row.index() > 1) { row.insertBefore(row.prev()) } }
          } 
          if(button_choice == 'down5') {
            for(i=0;i<checked_length+4;i++) { row.insertAfter(row.next()) }
          }  
})
}) 

enter image description here

Here is the FIDDLE

Upvotes: 2

Related Questions