How to select (and copy) a column name?

How to select (and copy) a column name?

mwoutsmwouts Posts: 20Questions: 3Answers: 0

Hi Allan, I would like to know if it is possible to reduce the size of the ordering button in the table header to allow the column names to be selected (e.g. double click as we do for text) and copied (e.g. Ctrl+C) ?

That is in the context of ITables, where we write Python code to display a table, then look at the table, and use the output to edit the Python code, often adding conditions that involve the columns names.

I looked into this in the past at https://github.com/mwouts/itables/issues/227, and found that setting orderable=False on the columns was one way to do it, but ideally I would prefer to be able to both sort and still copy the column name.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049
    edited April 2024 Answer ✓

    Are you using Datatables 2.0? If so it looks like you can use order.listener() to apply the listeners to the sort icons. Take a look at this example and you will see there is a new data-dt-order attribute that can be applied to the -tag th` to disable ordering or display the icon only. I built this example to show how it might work:
    https://live.datatables.net/febasito/1/edit

    The th now has a data-dt-column attribute with the column index. Use that as part of the selector to find the specific column's order icon for order.listener(). Use the browser's inspector to see the structure of the header.

    However if you have a previous version of Datatables then see the two threads linked to by Colin in this thread for ideas.

    Kevin

  • mwoutsmwouts Posts: 20Questions: 3Answers: 0

    Thank you Kevin! Yes the example achieves what I am looking for - I can double click on "Name" in the first column and it becomes selected. And I confirm that I am using DataTables 2.0.

    However I am not sure I can really use this in the context of ITables - I prefer to use as little JS as possible on my end. So, rather than setting data-dt-order="icon-only" on every column, and then setting a callback that restores the sorting, do you think I could ask for a global option on DataTables that would let the column text selectable and restrict the sorting to the icon? Thanks!

  • allanallan Posts: 64,230Questions: 1Answers: 10,599 Site admin

    It isn't something I'm planning to add to DataTables core - at least not yet. But I an planning a new extension that will do something basically like what you are looking for (it might require a new option in DataTables core, I'm not yet sure).

    At the moment, you'd need to use order.listener() on the element you want the sorting to activate on, as Kevin suggests.

    Allan

  • mwoutsmwouts Posts: 20Questions: 3Answers: 0

    Makes sense - Thanks Allan! Looking forward to it. Thanks Kevin for the workaround in the meantime.

  • mwoutsmwouts Posts: 20Questions: 3Answers: 0

    Hi @allan, I have not been able yet to deploy this recommendation on ITables but I would like to give it a fresh try. Here is my question - mostly caused by my limited Javascript abilities: is it possible to add the data-dt-order="icon-only" programmatically to all the headers of a DataTable object, and then to set the order.listener for all the icons, including for the headers that span over multiple columns? Thanks!

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049
    edited February 28

    I think this example does what you are asking for:
    https://live.datatables.net/febasito/11/edit

    Its based off this complex header example.

    EDIT: It doesn't work correctly with with colspan > 1. The sort icon doesn't change, ie the dt-ordering-[asc|desc] class sin't applied correctly. Let me look at it further or maybe @allan will have an idea.

    Kevin

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049

    I opened this thread to ask about how to make order.listener() work with complex headers. Will update my example when I get the answer.

    Kevin

  • allanallan Posts: 64,230Questions: 1Answers: 10,599 Site admin

    Thanks for this gents - great questions. I've added a note specifically about the order.listener() to Kevin's other thread, but I thought I'd fill a little gap here. I mentioned in the other thread that I'm working on DataTables 2.3 - a lot of that is being driven by a new extension I'm working on called "ColumnControl". It will offer buttons, drop downs and filtering for columns. One configuration would be to allow ordering on the icons only (in fact I was working on that specific case on Friday).

    ColumnControl and DataTables 2.3 are my main focus at the moment (minus support, and all the other stuff needed to run a business), so I'm hoping it won't be too far away before they are released. There is a lot still to be done though - particularly in the filtering department.

    Allan

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049

    ColumnControl sounds like a good extension. I think its been asked about multiple times.

    Kevin

Sign In or Register to comment.