Allow columns.title to be a function

Allow columns.title to be a function

Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10
edited July 2023 in Feature requests

I've started using Handlebars and one of the items that would be nice is to allow columns.title to be a function.

My use case is that I'd like a column with an icon to open each child row (˅), but in the header an icon to close all the child rows (˄).

| ˄ | Heading | Heading | Heading |
-----------------------------------
| ˅ | Data    | Data    | Data    |
| ˅ | Data    | Data    | Data    |
| ˅ | Data    | Data    | Data    |

I obviously can do that now:

        {
            data: null,
            title: "<html for close-all-child-rows icon ˄ >",
            def: "<html for open-child-row icon ˅ >"
        },

Or:

        {
            data: null,
            title: "<html for close-all-child-rows icon ˄ >",
            render: ()=> "<html for open-child-row icon ˅ >"
        },

But if I want to change the icons then I have to do it on all the tables on the site where I use those icons.

With Handlebars it would look like this:

        {
            data: null,
            title: ()=> Handlebars code for close-all-child-rows icon ˄,
            render: ()=> Handlebars code for open-child-row icon ˅
        },

And then I would just change the Handlebars template.

I can actually do that now EXCEPT when ordering is true I get the following error because it's expecting a string for the title instead of a function:

Uncaught TypeError: i.sTitle.replace is not a function

Anyway -- the ability to use a function for columns.title even when ordering is true would help my particular case and maybe someone else with something similar!

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    The title property would only be evaluated when the table is initialised, so if it must be in a function, you could use an immediately invoked function (i.e. it just calls itself).

    That said, I'm not clear on why a function is needed - if you are using a handlebars macro or something to complete that icon, wouldn't you just have that in place of the string?

    title: {{handlebars-variable-for-icon}
    

    for example?

    Allan

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

    Ah, ok -- the immediately invoked function works perfectly!

    As far as just using ``{{handlebars-variable-for-icon}}``` -- you might be right.

    I'll look at that in the morning -- it's 2:21 am my time so I can't think through why I'm doing it the current way right now!

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Oof - that's well past my bedtime!

    Allan

Sign In or Register to comment.