Default values in another .js-file

Default values in another .js-file

svierkantsvierkant Posts: 33Questions: 2Answers: 0
edited April 2011 in General
Is there an option to save your default values (i.e. language, pagination, statesave, etc.) in another file?

Replies

  • GregPGregP Posts: 500Questions: 10Answers: 0
    Which file are they stored in right now? ;-)

    Yes, this is core functionality of JavaScript itself. You simply need to be careful in how you structure things:

    1. Default values can be stored in a separate .js, but you must load them before calling dataTable() on your table.
    2. Make sure additional scripts (either in a separate file or right on the page) do not overwrite or null your defaults unless you want them to.

    With Allan's help, I created a system for reusable defaults. In my case, they were for multiple tables on a single page, but you could do something similar for multiple tables across your application. Have a look and see if any of this is helpful: http://datatables.net/forums/comments.php?DiscussionID=4101&page=1#Item_10
  • svierkantsvierkant Posts: 33Questions: 2Answers: 0
    I want to load the Dutch translation if datatables.php?lang=nl is loaded, and de English translation if ?lang=en is loaded. How do I do that?

    With the famous jQuery Validate plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) you can just call the localisation file. For example: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/localization/messages_nl.js
  • GregPGregP Posts: 500Questions: 10Answers: 0
    In general, what I personally would do is store the language parameters as a variable in your separate JS files. So a tiny example of a file called language_es.js might look like:

    [code]
    var dt_translation = {
    "oPaginate": {
    "sFirst": "Primera Pagina"
    },
    "sEmptyTable": "No hay nada aqui!"
    }
    [/code]

    Use PHP to get your query string variable, turn it into your separate JavaScript filename (for example, 'language_nl.js"), use PHP to then include this .js file into your page at an appropriate place, then write a function that takes your core initialization object and extends it using jQuery's .extend(), and then pass this new object into the dataTable()

    Don't have the time to put together sample code right now, but it shouldn't be too hard if you still need help later.
  • svierkantsvierkant Posts: 33Questions: 2Answers: 0
    There isn't a 'default-option'-function, like jQuery UI? Then you just can include:
    [code]$.extend($.ui.dialog.prototype.options,
    {
    close:function(event, id)
    {
    $(this).dialog('destroy');
    },
    height: 300,
    width: 400
    });
    [/code]
  • allanallan Posts: 63,540Questions: 1Answers: 10,476 Site admin
    Currently no - however I'm in the middle of adding the ability to set defaults right now :-). This functionality will be in the 1.9 beta that I hope to release before too long.

    Allan
  • svierkantsvierkant Posts: 33Questions: 2Answers: 0
    Ok, thanks!
  • svierkantsvierkant Posts: 33Questions: 2Answers: 0
    Thanks for the update: http://datatables.net/beta/1.9/examples/advanced_init/defaults.html
This discussion has been closed.