Adding further entries to the language file and using the values for the table

Adding further entries to the language file and using the values for the table

u2fanu2fan Posts: 10Questions: 0Answers: 0
edited December 2009 in General
Hi there

I would like to add custom entries to the language file fetched using the oLanguage.sUrl initialisation parameter, so that I can use these internationalised Strings elsewhere on the table. One example of such a need is for a tooltip on a button. I don't want to require an additional XHR for the custom entries and would like to reuse the existing DataTables logic for this.

Looking at the DataTables code, the _fnLanguageProcess function is used to copy the parts of the retrieved JSON object. What I think I need to do is to "override" (or rather, add advice around) the _fnLanguageProcess function to first copy my custom entries into the oSettings.oLanguage object, then call the original _fnLanguageProcess function to copy the default entries.

In principle, this should be straightforward. I'm just not sure exactly how to accomplish it in the context of the DataTables code. Here's a simple example of adding advice around a base function:

[code]



var base_foo;
function foo(yourname){
alert('Hello ' + yourname);
}
base_foo = foo;
// Adding advice around a function:
foo = function(yourname) {
alert('before');

// call original version
base_foo(yourname);

alert('after');
}
function clickme(){
foo('Jonathan');
}




[/code]

Thanks for any pointers you can give me.

Replies

  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Hi u2fan,

    There isn't any way to reuse the XHR which is made by DataTables for language processing, without modifying the DataTables core code. If you want to do this then it's trivial, just add a call to your function in _fnLanguageProcess() and pass in the oLanguage object in the function - this object will then contain any of the new information you have in your text file.

    I can't think of a cleaner way of doing it I'm afraid, I hadn't really considered using the XHR for the language file for anything else. Being able to bind to functions like this is something I want to have in the next major (2.x) version of DataTables - although that's quite a while off...!

    Regards,
    Allan
  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Thinking about it, I suppose it might be possible to use your pointer trick in combination with the oApi reference in the DataTables object that is created at initialisation time. This might work well for most other functions, but I'd be considered that the XHR might return before the modification function had had a chance to run. Perhaps worth a try though... oTable.oApi._fnLanguageProcess is the function reference.

    Regards,
    Allan
  • u2fanu2fan Posts: 10Questions: 0Answers: 0
    Hi Allan

    The pointer trick unfortunately didn't work (at least locally), probably due to the XHR returning before the modification was applied to override the function. In the meantime I've used an additional XHR as follows, once initialisation has happened after the "dataTable" function call:

    [code]
    $.getJSON( languageUrl, null, function( json ) {
    var oSettings = oDataTable.fnSettings();
    // (add further keys)
    });
    [/code]

    I hope that this kind of thing becomes a lot easier in 2.x. Thanks for a very useful and well designed piece of software.
This discussion has been closed.