Is there a way to modify the config options via a plugin?

Is there a way to modify the config options via a plugin?

DanSwitzer2DanSwitzer2 Posts: 21Questions: 4Answers: 0

I'm working on some code that will allow you to specify an XML template to config the columns option, and you can configure a templating engine to trigger the display rendering.

The idea is you could specify a new templateConfig: '#template-config' option and it would parse the following:

<script id="template-config" type="text/template">
<columns>
    <column data="name" />
    <column data="position" />
    <column data="office" />
    <column data="extn" />
    <column data="start_date" render-value="data.display" render-sort="data.timestamp">
        <![CDATA[<strong>{{=data.display}}</strong>]]>
    </column>
    <column data="salary" />
</columns>
</script>

And it essentially convert it into something like:

columns: [
  { data: "name" },
  { data: "position" },
  { data: "office" },
  { data: "extn" },
  { data: "start_date", render: function (data, type, row, meta){
    if( type === "sort" ) return data.timestamp;
    
   // this is actually generated via a templating engine
    return "<strong>" + data.display + "</strong>";
  } },
  { data: "salary" }
]

The problem I'm running into making this as a plug-in, is there does not seem to be a way to interject code that runs before the columns option would be processed.

In order to make this a plugin, I think I would need to be able to run code before _fnCompatOpts() and _fnCompatCols() are called during the this.each() routine.

Is there any way to hook into the settings options before the logic really starts to be applied during the plugin initialization?

Am I missing anything?

If I can't do it this way, I'll just end up creating a helper method that does the work to build the columns option. Just thought it would be nice to implement it as a config option.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 62,315Questions: 1Answers: 10,225 Site admin
    Answer ✓

    What you describe is something that I'd thought of doing myself as well, but I've never got around to it I'm afraid. You can use HTML5 data attributes to define options so you can get a long way there, but it doesn't allow for functions to be defined.

    I think what you'd need to do here is to run your code before initialising the DataTable, and then using the result from that to initialise the table - just as you say.

    There isn't any events that you can hook into at the moment before the columns are processed in the initialisation I'm afraid. Although that's a good suggestion for a future enhancement - thanks!

    Allan

  • DanSwitzer2DanSwitzer2 Posts: 21Questions: 4Answers: 0

    @allan,

    Thanks for the response. I'll just create a helper function that will generate the value of the columns option.

This discussion has been closed.