jQuery DataTables RowReorder - disable row from being sorted/reordered

jQuery DataTables RowReorder - disable row from being sorted/reordered

radavisradavis Posts: 5Questions: 1Answers: 0

I want to be able to disable a row from being reordered.

The behavior that I want is that when I am dragging rows to be reordered, I want the row to stay at the bottom position and never be reordered.

I was able to set it up such that this row cannot be selected for reordering; however, other rows can be placed underneath.

I also tried to set things up where I remove the row from the table while reordering happens; however, this causes some issues where some of the rows disappear.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    One option may be to use the plugin described here:
    https://datatables.net/blog/2016-12-22

    Assuming you are using numeric indexes for the rowReorder you might be able to set the index of the row you want at the bottom to be something like -1. Then use the plugin order -1 to the bottom. Here is a simple example:
    http://live.datatables.net/nanaqowi/1/edit

    Kevin

  • radavisradavis Posts: 5Questions: 1Answers: 0

    Thanks for answering my question!

    In your example, it looks like the reordering is disabled. It seems to work when I sort based on the sequence; however, it doesn't force the last row to the bottom...

    Will look more into the plugin you mentioned.

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    In your example, it looks like the reordering is disabled.

    I can reorder by dragging the index column. The indexes themselves will always stay in the same order but the rest of the row will move. Likely you will have this column hidden.

    however, it doesn't force the last row to the bottom

    Garrett Winters is the row that I defined as the last row by using the index of -1. It stays at the bottom through the use of the plugin. However if you drag that row then another will end up with the -1 index. I didn't take the time to disable that row from reordering as you mentioned you have done this.

    This is just an example. Your config might be different and the behavior is different. If you have difficulties please provide a test case or update mine to show the problem.

    I updated mine to hide the index column. Might make more sense.
    http://live.datatables.net/nanaqowi/5/edit

    Kevin

  • radavisradavis Posts: 5Questions: 1Answers: 0

    Hi Kevin,

    Thank you for your help!

    I looked at the latest live datatables link you posted. I basically want that 'Garrett Winters' row to always be the last row while the rest can be reordered.

    I think I have a way forward with this, but haven't implemented it yet. Will let you know what I discover in the next couple of days.

    Kind Regards,
    Raj

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    Something like this maybe?
    http://live.datatables.net/nanaqowi/7/edit

    It uses rowReorder.selector to select the reorder elements by a class. The columns.className is used to apply that class to all cells in the Name column. Then uses columns.createdCell to remove the class if the name is Garret Winters.

    Kevin

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    One way to force the position of a row to the top or bottom of a table is to use the absolute sorting plug-in.

    That in combination with the options Kevin suggested should make it possible to do what you are looking for. That said, you'd still be able to drag and drop rows below the row you've got fixed to the bottom of the table - there is no way to address that at the moment I'm afraid, other than to put the fixed row into the table footer.

    Regards,
    Allan

  • radavisradavis Posts: 5Questions: 1Answers: 0

    Just to clarify, what I am looking to do is have the last row always be the last row regardless of reordering.

    It seems like I can't achieve what I am looking to do with the rowReorder and absolute sorting plug-ins combined, it seems as though reordering will always allow rows to go beneath the last row even with the absolute sorting plugin applied... in any case, I am opting to just not have the row present for reordering.

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765
    edited December 2019 Answer ✓

    you'd still be able to drag and drop rows below the row you've got fixed to the bottom of the table

    I didn't think of that :smile: An interesting feature would be to define rows that are droppable. While not ideal I did find a workaround:
    http://live.datatables.net/nanaqowi/9/edit

    It uses the row-reordered to find out if the bottom row is reordered. If so it swaps the indexes of the bottom row and the dropped row. You can toggle the visibility of column 0 to see what is happening. Plus there are a few console.log statements.

    Haven't extensively tested this but it seems to work.

    in any case, I am opting to just not have the row present for reordering.

    Another option may be to place that row in the footer and remove it from the Datatable data.

    Kevin

  • radavisradavis Posts: 5Questions: 1Answers: 0

    Kevin,

    Another option may be to place that row in the footer and remove it from the Datatable data.

    I tried this and it still would be reorderable; however, the example you included in your last response is more or less what I was looking for. I am still moving forward to have the table without the row, this seems to be the path of least resistance.

    Kind Regards,
    Raj

This discussion has been closed.