Sorting/Ordering columns using column name/data/class

Sorting/Ordering columns using column name/data/class

robertrishrobertrish Posts: 6Questions: 1Answers: 0

Hello!

I've recently implemented a DataTable using column data.

Ex.

columns: [
                {   "data": "ids",
                    "name": "ids",
                    "className": "dtc_ids",
                    "width": "35px",
                    "orderable": false,
                    "searchable": false,
                    "visible": false
                }
]

My questions are:

  • Is there a way to use the name, data or class of the column in order to set the default column sort.

Standard implementation:
order: ['8', 'desc']

Desired Implementation:
order: ['ids', 'desc']

Or:

columns: [
                {   "data": "ids",
                    "name": "ids",
                    "className": "dtc_ids",
                    "width": "35px",
                    "orderable": false,
                    "searchable": false,
                    "visible": false,
                    "defaultOrder": true,
                    "sortOrder": 'desc'
                }
]
  • Is there a way to send the name, data or class to the server instead of an index when clicking on the headers of the table to sort them?

I'm seeing the following in the POST when sorting the column:
sColumns: ids
iSortCol_0: 0
sSortDir_0: desc

  • Is there a way to set column visibility using the name of the column instead of using its' class?

datatable.columns(['ids']).visible(true);

instead of

datatable.columns(['.dtc_ids']).visible(true);

Thank you very much for you help!
Rob

Replies

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    You might want to look at columnDefs, where "targets" enables multiple columns to be
    nominated for the same conditions.

    https://datatables.net/reference/option/columnDefs.targets

  • allanallan Posts: 63,688Questions: 1Answers: 10,500 Site admin

    Is there a way to use the name, data or class of the column in order to set the default column sort.

    No - not at this time. Sorry. That is something I hope to implement in future.

    Is there a way to send the name, data or class to the server instead of an index when clicking on the headers of the table to sort them?

    No - it is index based. You can send a name using the columns.name option, but it won't alter the indexes.

    Is there a way to set column visibility using the name of the column instead of using its' class?

    Yes - exactly as you have done in fact. You don't need the array, just give it the string. See columns().

    Allan

  • robertrishrobertrish Posts: 6Questions: 1Answers: 0
    edited June 2015

    Thank you, @Allan.

    @tangerine,

    I've tried using columnDefs for setting my parameters, but I wasn't able to use name, data or classes for them.

    My DataTable doesn't have any content defined:

    <table ="groupTable"></table>
    

    Everything is defined through the columns array. And since the table doesn't have any classes on its' <TH>'s, I can't set any parameters to them on load.

    I can adjust the visibility of the table after it's completely loaded, but not before that:

    campaignAdsTable.columns(['.dtc_preview','.dtc_creativeId','.dtc_adName','.dtc_adType','.dtc_dimension','.dtc_targetUrl']).visible(true);
    

    Would you have an example on using columnDefs using names, data or classes in this scenario?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited June 2015

    My apologies, I misread your question.

    Desired Implementation: order: ['ids', 'desc']
    

    I mentally transposed your 'ids' into column indices

    targets[1,2,3,4],
    ....
    

    which I now realise was not what you intended.

This discussion has been closed.