How to programatically add class to row using it´s current ordering position?

How to programatically add class to row using it´s current ordering position?

Leo BrunoLeo Bruno Posts: 19Questions: 3Answers: 0

Hi:

I´m trying to add the "selected" class to the first row, uppon filtering the records.
But no matter which option I use, the affected row is always the first one on the dataset.

The goal is to autoselect the first row uppon filtering.

The filtering method is being called from an outside function.

I don´t use the select extension.

Here is how I´m doing:

    function handleSearchChange(event){         
      table.DataTable().search(String(event.target.value)).draw()
      table.DataTable().$('tr.selected').removeClass('selected') 
      table.DataTable().row(0, {order:  'current'} ).nodes().to$().addClass('selected')
    }

Can someone help?

Thank´s in advance.

Answers

  • Leo BrunoLeo Bruno Posts: 19Questions: 3Answers: 0

    I changed the code to the following, and realized that this one is more appropriate.

    But, I can´t get the first filtered row to get selected.

        function handleSearchChange(event){         
          table.DataTable().search(String(event.target.value)).draw()
          table.DataTable().$('tr.selected').removeClass('selected') 
          table.DataTable().row({order:  'applied'} ).nodes().to$().addClass('selected')
        }
    
  • Leo BrunoLeo Bruno Posts: 19Questions: 3Answers: 0

    Done it!

        function handleSearchChange(event){         
          table.DataTable().search(String(event.target.value)).draw()
          table.DataTable().$('tr.selected').removeClass('selected') 
          table.DataTable().row({search:'applied'} ).nodes().to$().addClass('selected')
        }
    

    This library is amazing !!!

    The only thing it can´t do is to make rain.

    srrsrsrsrsrs

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Plenty of rain here in Scotland. I never thought to add such a feature ;)

    I'd suggest you cache the table.DataTable() response btw. It will make things a little faster. Other than that - nice one. Thanks for posting back with your solution.

    Allan

Sign In or Register to comment.