rowId option as a function

rowId option as a function

rduncecbrduncecb Posts: 125Questions: 2Answers: 28

Due to having multiple tables in the DOM at one time, each requiring rows have DOM id attributes I can use rowId to give the tr nodes id attributes. However, id values across tables may clash so it's useful prefix them by using specifying rowId as a function, such as:

                rowId: function(data) {
                    return "countryPrefix_" + data.id;
                },

However, the documentation for rowId makes no reference to using a function. Is this an official part of the DataTable API?

Obviously it's extremely useful but it would be an issue if it was to suddenly disappear from being unofficial/undocumented and I shouldn't have been using it in the first place ;)

This question has an accepted answers - jump to answer

Answers

  • jazjaz Posts: 16Questions: 3Answers: 0

    I don't know if there is a specific way to do it using the rowId option, but you could create a specific field in your data that has the prefix added and use that for the rowId value.

    Data:

    {
        'id' : 10
        'id_with_prefix' : 'countryPrefix_10'
    }
    

    Set rowId:

        rowId : data.id_with_prefix
    
  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    That's an option, I can easily intercept the JSON data and use

    data.map(function(d) { d.prefixedId = 'countryPrefix_' + d.id } )
    

    to add the new node to each array element but one I was hoping to avoid by using rowId as a function.

    Using a function for rowId works nicely so my question really boils down to whether it's an official feature and won't disappear without notice. If it's official it would be nice to be in the documentation and would be my preferred method, if not it's official the option above is probably safer.

  • allanallan Posts: 62,602Questions: 1Answers: 10,291 Site admin
    Answer ✓

    Using it as a function works because it basically uses the same method that columns.data uses to get and set data - i.e. everything listed there, including functions, should work, but it isn't something that I've explicitly tested, which is why it isn't documented.

    I don't foresee that being removed in future. If anything it is more likely to documented and correctly supported.

    Allan

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    Excellent, thanks @allan , and thanks for the speedy response!

  • KarloKarlo Posts: 34Questions: 10Answers: 0
    edited February 2018

    I'm also using rowId as a function, very useful for me.

This discussion has been closed.