0

I am trying to figure out a way to solve an issue with a DataTable set in a Repeater. Depending on the user viewing the page, the number of column changes. For instance User1 will see 8 columns while user2 will see 7 columns in the table.

Datatables lets me choose the default ordering of the table with the order functionality:

var table = $('#example'String).DataTable();

// Sort by column 8 and then re-draw

table
    .order( [ 8, 'asc' ] )

The problem is that whenever I change the Visibility of the 1st column for User2:

column0.Visible = False

...the number of column changes, so the correct index to do the odering has change from index 8 to index 7.

I have tried to use the default ordering by <td> ID but it will only accept Colummn Index.

I have tried to use a negative Index to start from the right but it doesn't work.

I also tried to find a way to hardcode the table columns manually without changing the order to the end user.

It would be simple if I could change the order of my columns but they need to be in that specific order on screen.

Anyone familiar with Datatables that can help me with this. It would be highly appreciated :)

1 Answer 1

1

I can't see your datatables Init code, but I accomplish this by naming the columns on init and then using this to find the column index:

In init:

table = $("table").DataTable({
           "columns": [
                    {"name": "Row1"}
                    {"name": "Row2"}
                     ]
         });

Then, you can get the index of the row like this:

var row1Index = table.column('Row1:name').index();

And use the index for your filtering:

table.order([row1Index, 'asc']);

It should allow your indexes to be variables based on the initialization.

See the name documentation here: https://datatables.net/reference/option/columns.name

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.