highlight and delete first row in datatable

highlight and delete first row in datatable

denizdianadenizdiana Posts: 15Questions: 8Answers: 0

Hello everyone,
I want to highlight only first row in datatable and when i click button i want to delete first row so after i delete, the second row should become first row and it should be highlighted. How can i do that? Thank you so much.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    You can use the Select Extension to select the row if you like. Or you can use your own highlighting technique.

    Use row().delete() to delete the row.

    The key is being able to select the first row in the table. The row-selector is used to select the appropriate row. To get the first row you would use a jQuery selector like this: row(':eq(0)').

    Kevin

  • denizdianadenizdiana Posts: 15Questions: 8Answers: 0
    I have a table like this;
    var dtable = $('#table').DataTable(
                {
    
                 data : data.events,
    
        columns: [
    
          { data: 'name',  title: 'Person Name'  },
          { data: 'action', title: 'Event'  },
          { data: 'date', title: 'Date' }
    

    ]

                });
    

    And i use this method but it doesn't work. :(

    $( dtable.row(':eq(0)')).addClass( 'highlight' );

    My css method is this :

          td.highlight {
        background-color: whitesmoke !important;
          }
    
  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    You need to use row().node() like this:
    `$( dtable.row(':eq(0)').node()).addClass( 'highlight' );

    td.highlight won't work because you are applying the class to the row not cells. Highlighting Datatables rows is a bit tricky due to its styling. See CSS tab of this example:
    http://live.datatables.net/sinazeca/1/edit

    Kevin

  • denizdianadenizdiana Posts: 15Questions: 8Answers: 0

    Now, i have a problem with delete function :/
    dtable.row( 0 ).delete();
    this method doesnt work and it gives me "Uncaught TypeError: dtable.row(...).delete is not a function" error. What am i doing wrong?

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited April 2020 Answer ✓

    I'm full of mistakes today :smile:

    I gave the wrong link to show the highlighting:
    http://live.datatables.net/sinazeca/2/edit

    Also the API is row().remove() not delete(). See the example for remove().

    Kevin

This discussion has been closed.