Sort by rendered text rather than data

Sort by rendered text rather than data

gc978gc978 Posts: 3Questions: 1Answers: 0

We are using datatables in a Meteor.js webapp with mongodb as the backend for the data. I have a table that displays user information from the Users table in mongodb. Some of the data fields of the user document are just the "_id" values from other tables. I use that data to perform a query and retrieve the text I want to display for that field in the datatable. This works as expected using the following setup:

{
    data: "User",
    title: "User",
    render: function(data, type, full, meta) {
        if (typeof data != "undefined") {
            var User = Users.findOne({
                _id: data
            });
            if (User) {
                var name = User.firstName + " " + User.lastName;
                return name;
            }
            else {
                return data;
            }
        }
        else {
            return data;
        }
    }
}

The issue that I am having is that when I sort the rendered column, it is sorting by the actual "data" and not the resulting displayed "name".

Are there any suggestions on how to achieve this? I've seen examples on how to sort by the actual "data", which lead me to believe datatables should sort by the rendered text by default but that does not appear to be the case.

Thanks in advance for any assistance that can be offered.

Regards

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    edited October 2021

    You will want to use Orthogonal data to sort by something other than the original data. Something like this:

        render: function(data, type, full, meta) {
          if ( type == 'sort' ) { // If the operation sort then use the rendered data
            if (typeof data != "undefined") {
                var User = Users.findOne({
                    _id: data
                });
                if (User) {
                    var name = User.firstName + " " + User.lastName;
                    return name;
                }
                else {
                    return data;
                }
            }
            else {
                return data;
            }
          }
          return data;  // return `data` for all other operations
        }
    

    It might need tweaking to meet your needs or to fix syntax errors. You could also use if ( type == 'sort' || type == 'order' ) { to order by the rendered data.

    Kevin

  • gc978gc978 Posts: 3Questions: 1Answers: 0

    Thanks. I did look at using Orthogonal data but in the end, I want to use the rendered text for all data operations (display, sort, filter and type).

    Knowing that I do not know everything, I tried both if if ( type == 'sort' ) { and ( type == 'sort' || type == 'order' ) {

    But each resulted in displaying the actual data rather than the rendered text and it still sorted by the data as well.

    I also tried if ( type == 'sort' || type == 'display' ) { but that brought me back to where I started.

    I'll keep searching but would be more than happy to try any and all other suggestions.

    Thank you

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    Answer ✓

    Here is a simplified test case of what you show above:
    http://live.datatables.net/silugiko/1/edit

    it seems to work without orthogonal data. Please update the test case to show the problem you are having so we can help debug.

    Kevin

  • gc978gc978 Posts: 3Questions: 1Answers: 0

    Thanks for the example, which clearly works. While it is not working for me in my use case, it's clear that it's not the code that I am using but more likely that I am using a MeteorJS wrapper for datatables. I will follow up if I find a workaround should anyone else using Tabular runs into this issue.

    Thanks again!

Sign In or Register to comment.