Sorting numbers with # in front, and inside an A tag

Sorting numbers with # in front, and inside an A tag

FringoFringo Posts: 3Questions: 1Answers: 0
edited August 2018 in Free community support

Hi all,

What is the best way to go about having a column with values like #1, #2, #3 etc ?
The content in the td is also inside an A tag. E.g.

<td>
<a class="record-open" data-itemID="1000" href="#">#1</a>
</td>

etc.

I've tried setting column type to 'html-num' to no avail. I've also tried removing the # from the td and using 'render' to apply a function with the # added. Neither option works. The column is still sorted like a string.

Any ideas?

Cheers

Answers

  • kthorngrenkthorngren Posts: 21,302Questions: 26Answers: 4,947

    One option is to use Orthogonal Data to extract the numbers from the a tag for the sort type.

    Kevin

  • FringoFringo Posts: 3Questions: 1Answers: 0

    I see... I think I looked at that but didn't understand it the first time around. I've now used this:

        {
            "targets": 1,
            "render": function ( data, type, row, meta ) {
                if(type == 'sort')
                {
                    var rgx = />#([0-9]+)</;
                    data = data.match(rgx)[1];
                }
                return data;
            },
            type: "num"
        }
    

    It seems to do the trick. Let me know if you think there's a better way.

    Cheers

  • FringoFringo Posts: 3Questions: 1Answers: 0

    The above solution aside, it is surprising Datatables doesn't detect this automatically. It is a fairly simple format and I'm guessing its quite commonly used.

  • kthorngrenkthorngren Posts: 21,302Questions: 26Answers: 4,947

    You could also try using the Natural sorting plugin.

    Maybe this doc will help:
    https://datatables.net/manual/data/renderers

    Kevin

This discussion has been closed.