Display of up/down buttons in table row reordering

Jessica C

I am using this jsfiddle: http://jsfiddle.net/vaDkF/828/ (without the top and bottom option) to create a reordering table.

$(document).ready(function(){
    $(".up,.down").click(function(){
        var row = $(this).parents("tr:first");
        if ($(this).is(".up")) {
            row.insertBefore(row.prev());
        } else  {
            row.insertAfter(row.next());
        } 
       
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>
        <td>One</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
    <tr>
        <td>Two</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
    <tr>
        <td>Three</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
    <tr>
        <td>Four</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
    <tr>
        <td>Five</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
</table>

I would like to know if it is possible to have the up button disappear (display none) if it is the first row in the table, and the down button disappear if it is the last row in the table.

Thanks!

DaniP

You can use CSS for this, with first-child and last-child selectors:

tr:first-child .up, tr:last-child .down {
  display:none;
}

$(document).ready(function() {
  $(".up,.down").click(function() {
    var row = $(this).parents("tr:first");
    if ($(this).is(".up")) {
      row.insertBefore(row.prev());
    } else {
      row.insertAfter(row.next());
    }
  });
});
tr:first-child .up,
tr:last-child .down {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>One</td>
    <td>
      <a href="#" class="up">Up</a>
      <a href="#" class="down">Down</a>
    </td>
  </tr>
  <tr>
    <td>Two</td>
    <td>
      <a href="#" class="up">Up</a>
      <a href="#" class="down">Down</a>
    </td>
  </tr>
  <tr>
    <td>Three</td>
    <td>
      <a href="#" class="up">Up</a>
      <a href="#" class="down">Down</a>
    </td>
  </tr>
  <tr>
    <td>Four</td>
    <td>
      <a href="#" class="up">Up</a>
      <a href="#" class="down">Down</a>
    </td>
  </tr>
  <tr>
    <td>Five</td>
    <td>
      <a href="#" class="up">Up</a>
      <a href="#" class="down">Down</a>
    </td>
  </tr>
</table>

Updated Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleting a Row in a rowReorder Table Resets Reordering

From Dev

Gather a row of tableB from buttons row of table A

From Dev

Bootstrap table row clickable with buttons in row

From Dev

display image in table row

From Dev

Using buttons to add new row to Rails table

From Dev

Dynamically creating buttons that remove a row from a table

From Dev

How to align buttons to text in a table row in HTML

From Dev

How to display weeks in a table with previous and next buttons

From Dev

display details of a specific row of a table

From Dev

Display only part of table row

From Dev

MySql - Reordering the rows of a table

From Dev

Excel - reordering table

From Dev

How to forces 3 buttons to display in the same row evenly on all screensize?

From Dev

CSS multi-row table without "display: table-row"

From Dev

Nested display:table-row in a display:table-cell

From Dev

Nested display:table-row in a display:table-cell

From Dev

update a position field by reordering a table

From Dev

Display row names in a data.table object

From Dev

display table row only when if statement is correct

From Dev

mvc display different table row in dropdownlist

From Dev

hover the entire row of the div display:table properties

From Dev

Display Multidimensional array into table row individually in CodeIgniter

From Dev

Display rowIndex for dropdown in each row of the table

From Dev

Display table row width doesn't fix

From Dev

toggle display on hover table row with same class

From Dev

Android Display Selected Table Row in Dialog

From Dev

mvc display different table row in dropdownlist

From Dev

hover the entire row of the div display:table properties

From Dev

Display two table in two columns in a row on adding

Related Related

  1. 1

    Deleting a Row in a rowReorder Table Resets Reordering

  2. 2

    Gather a row of tableB from buttons row of table A

  3. 3

    Bootstrap table row clickable with buttons in row

  4. 4

    display image in table row

  5. 5

    Using buttons to add new row to Rails table

  6. 6

    Dynamically creating buttons that remove a row from a table

  7. 7

    How to align buttons to text in a table row in HTML

  8. 8

    How to display weeks in a table with previous and next buttons

  9. 9

    display details of a specific row of a table

  10. 10

    Display only part of table row

  11. 11

    MySql - Reordering the rows of a table

  12. 12

    Excel - reordering table

  13. 13

    How to forces 3 buttons to display in the same row evenly on all screensize?

  14. 14

    CSS multi-row table without "display: table-row"

  15. 15

    Nested display:table-row in a display:table-cell

  16. 16

    Nested display:table-row in a display:table-cell

  17. 17

    update a position field by reordering a table

  18. 18

    Display row names in a data.table object

  19. 19

    display table row only when if statement is correct

  20. 20

    mvc display different table row in dropdownlist

  21. 21

    hover the entire row of the div display:table properties

  22. 22

    Display Multidimensional array into table row individually in CodeIgniter

  23. 23

    Display rowIndex for dropdown in each row of the table

  24. 24

    Display table row width doesn't fix

  25. 25

    toggle display on hover table row with same class

  26. 26

    Android Display Selected Table Row in Dialog

  27. 27

    mvc display different table row in dropdownlist

  28. 28

    hover the entire row of the div display:table properties

  29. 29

    Display two table in two columns in a row on adding

HotTag

Archive