brackets-negative plug-in with dynamic columns

brackets-negative plug-in with dynamic columns

LukasLLukasL Posts: 26Questions: 10Answers: 0
edited April 2020 in Free community support

I have a DataTable for which I generate dynamic headers before its initialization. This means that I cannot predict column datatypes, so I rely on DataTables identifying the column type based on the data to get the sorting to work.

Some columns that are returned have currency data (Example format: $234,392.01). Sometimes, the data is surrounded by parentheses to represent a negative value: ($234,392.01). Because of the parentheses, DataTables identifies this column as a string and sorts it incorrectly.

I've tried using brackets-negative plug-in, but it didn't really do anything. However, I am not doing anything extra beyond just pasting the plug-in in my code.

Is there anything additional I need to do to use brackets-negative plug-in? If this is a limitation caused by dynamic columns, is there another way I could solve this issue?

And just to be clear, if there are no parentheses in the column, DataTables sorts the currency data correctly.

Thank you very much.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,774

    The plugin works here:
    http://live.datatables.net/cazaqije/1/edit

    You probably have some data in that column that doesn't math the format the plugin is looking for. You can do some debugging of the plugin code to see if all the values match what its looking for.

    The dynamic columns shouldn't affect this as Datatables does type detection of the actual data as described in the columns.type docs.

    If you need help with this please provide a link to your page or a test case replicating the problem so we can see the exact data that is not working.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • LukasLLukasL Posts: 26Questions: 10Answers: 0
    edited April 2020

    Thank you for the response, kthorngren. I have made a test case below. I have also tried debugging the plug-in, but I couldn't find any unexpected behavior.

    The plug-in does not work here:
    http://live.datatables.net/quwucunu/46/edit

    EDIT: I noticed that removing cells with null values fixes it. However, I would need it so work with null values. But if I have some values null in your test case (http://live.datatables.net/cazaqije/1/edit), it still works.

  • LukasLLukasL Posts: 26Questions: 10Answers: 0

    Another thing I found in http://live.datatables.net/quwucunu/46/edit is that if you leave the nulls but just remove the parentheses in the one value, it works.

  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,774
    edited April 2020 Answer ✓

    But if I have some values null in your test case

    Not sure what you put in for null values. Was it just an empty string? There is a difference between "" and null.

    I removed the CDN used to include the plugin and copied the code into your example here:
    http://live.datatables.net/quwucunu/49/edit

    The brackets plugin doesn't support null values as is. It can be added which I did in the example. I placed a couple of console.log statements so you can see what it happening. I added an if statement to return the column type as currency if the data is null. Then in the ordering part of the plugin I updated the if (data === '') to include data === null. This sets the value to 0 for sorting which may not be what you want. You can change it to return whatever you want to force these rows to the top or bottom.

    Kevin

  • LukasLLukasL Posts: 26Questions: 10Answers: 0
    edited April 2020

    Thank you, Kevin! This is what I wanted and I'm grateful for all your help.

    I have replaced the zero with Number.NEGATIVE_INFINITY so null and '' are always considered as lowest values.

This discussion has been closed.