How to select (and copy) a column name?
How to select (and copy) a column name?

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
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 newdata-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 adata-dt-column
attribute with the column index. Use that as part of the selector to find the specific column's order icon fororder.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
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!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
Makes sense - Thanks Allan! Looking forward to it. Thanks Kevin for the workaround in the meantime.
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 theorder.listener
for all the icons, including for the headers that span over multiple columns? Thanks!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 thedt-ordering-[asc|desc]
class sin't applied correctly. Let me look at it further or maybe @allan will have an idea.Kevin
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
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
ColumnControl sounds like a good extension. I think its been asked about multiple times.
Kevin