Immediately invoked function for column title no longer works w/ 2.0

Immediately invoked function for column title no longer works w/ 2.0

Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10
edited February 16 in DataTables

First -- CONGRATULATIONS on 2.0!!! I'm excited to see what this update offers!

Anyway, not sure if this would be properly called a "bug", but in the past this would have worked to return an icon to prepend to the column title:

        {
            data: 'field',
            title: function () { return some_icon_function('icon') + ' some column title' }
        }

Now the column title is literally:

function () { return some_icon_function('icon') + ' some column title' }

And this does the same:

        {
            data: 'field',
            title: ()=> some_icon_function('icon') + ' some column title'
        }

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,441Questions: 1Answers: 10,459 Site admin
    Answer ✓

    Thank you :) I've been writing up a release blog post and hope to drop that on Monday - it should give a clearer idea of the major new bits.

    Interesting about this issue. That it worked before is a total fluke - it hasn't been tested for that (it is only tested for a string, as is documented: columns.title). I had a little look at it, and that it works as a function is an artifact of jQuery's .html function. If it gets a function, it will execute it before writing it to the HTML. However, the titles were changed for v2 and I don't use $().html() for it any more - just write directly to it.

    Interestingly, I was always getting a .replace error when testing - does that not happen for you: https://live.datatables.net/letigiji/1/edit ?

    There is a work around... Make your function immediately invoked (you mentioned that it is, but it isn't since the function is being passed as a value). Execute it immediately and the result is assigned to columns.title:

    {
        data: 'field',
        title: function () { return fn('icon') + ' title' }()
    }
    

    https://live.datatables.net/letigiji/2/edit

    The only difference is the () at the end. Since this part of the DataTables init is fully synchronous, I don't think there will be any change in behaviour.

    Or do:

    {
        data: 'field',
        title: some_icon_function('icon') + ' some column title'
    }
    

    Allan

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10
    edited February 16

    Thanks, @allen -- I'm a hobbyist trying to keep up with professionals, so I always appreciate the explanations you provide! I've learned something now about immediately invoked functions!

    Anyway, I wasn't getting any type of error, but this code here works perfectly!

    {
        data: 'field',
        title: some_icon_function('icon') + ' some column title'
    }
    
  • allanallan Posts: 63,441Questions: 1Answers: 10,459 Site admin
    Answer ✓

    Perfect!

    I feel like I'm constantly trying to keep up as well - the Javascript world moves so fast!

    Allan

Sign In or Register to comment.