data-order with columns.data

data-order with columns.data

mlumlu Posts: 2Questions: 1Answers: 0

Hi,

I'm wondering if it's possible to use the data-order html attribute with columns.data? I want to be able to to set a map for the columns, but also use the data-order for sorting, like for timestamps, but this doesn't seem to work.

I'm initiating my data table like this:

      new DataTable("#table", {
             columnControl: ['order', ['search']],
             order: order,
             ordering: {
                 indicators: false,
                 handler: false
             },
             columns: [{ data: 'createdAt'},{ data: 'names'},{ data: 'emails'},{ data: 'phones'},{ data: 'year'}]
      });

with my row looking something like this:

<tr id="entry-{{.ID}}">
    <td data-order="1234567890">Mon, Jan 1, 2000, 1PM</td>
    ...
</tr>

but it orders by the value between the td tags, instead of data-order.

When I remove the columns field from the new DataTable instance it works, so I'm just wondering how you would do the same with the columns?

Answers

  • kthorngrenkthorngren Posts: 22,239Questions: 26Answers: 5,116

    I built a simple test case and it behaves like you mentioned. I would have thought it would work too. @allan will need to take a look.

    https://live.datatables.net/gukumava/1/edit

    Kevin

  • allanallan Posts: 64,920Questions: 1Answers: 10,752 Site admin
    edited August 18

    Unfortunately it isn't quite so simple. The way the data attributes work is that DataTables will read the text from the cell and the data properties into an object, and that object gets used for the orthogonal data points.

    This is the code for that.

    If you want to read it into an object, you need to reconstruct basically the same thing: https://live.datatables.net/gukumava/2/edit .

    Unfortunately it isn't particularly flexible in the naming. It needs to be called @data-order to read the order attribute. But, as shown in the example above, you can shove that into a property, of a different object from the cell's text. Or you can keep them together, which is what DataTables core does.

    Allan

  • mlumlu Posts: 2Questions: 1Answers: 0
    edited August 18

    that's helpful, thanks! Just curious where do you get the nameSort label before the data-order? Do you just make it up, ie can put whatever you want there? Or does it have to be the _ label + Sort?

  • allanallan Posts: 64,920Questions: 1Answers: 10,752 Site admin

    Yup, made up. That's where it would write the value to in the object that is created for the row's data.

    I assume that this is so you can use the data object via the API after initialisation?

    Allan

Sign In or Register to comment.