How can I set default setting value?

How can I set default setting value?

matsumatsu Posts: 4Questions: 0Answers: 0
edited February 2010 in General
Hello.

I use this plugin all over my site, but most initialize parameters are duplicated like this.

Example #1
[code]
$('#dt_example').dataTable( {
"sAjaxSource": ''aaaa.php'
, "aaSorting": [[ 0, "desc" ]]

, "bStateSave": true
, "sPaginationType": "full_numbers"
, "bJQueryUI": true
, "oLanguage" : {
"sUrl": '/p/back/js/languages/ja_JP.txt'
}
});
[/code]

Example #2
[code]
$('#dt_example').dataTable( {
"sAjaxSource": ''bbb.php'
, "aaSorting": [[ 2, "desc" ]]

, "bStateSave": true
, "sPaginationType": "full_numbers"
, "bJQueryUI": true
, "oLanguage" : {
"sUrl": '/p/back/js/languages/ja_JP.txt'
}
});
[/code]

I'd like to avoid coding same setting each time.
jQuery UI can be changed default setting value like following.
[code]
jQuery.ui.dialog.defaults.modal = true;
jQuery.ui.dialog.defaults.bgiframe = true;
jQuery.ui.dialog.defaults.resizable = true;
jQuery.ui.dialog.defaults.width = 400;
[/code]

I hope DataTables has same function like jQuery UI.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Have a look at this thread from a few days ago with covers this topic: http://datatables.net/forums/comments.php?DiscussionID=1285 . It's not quite the same a jQuery UI, but it's certainly possible to have a default object.

    Regards,
    Allan
  • matsumatsu Posts: 4Questions: 0Answers: 0
    Thanks, Allan

    I've checked http://datatables.net/forums/comments.php?DiscussionID=1285.

    I dont' wanna disturb original source code as well so I'd like to take approach #2. However, the #2 doesn't look nice for me comparing to the jQuery UI's. Could you give me a example code for me plase?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It's just like I say in the post there - use $.extend to clone a default object:

    [code]
    var oDefault = {
    "bFilter": false,
    "bSort": false
    };

    var oST = $.extend( true, {}, oDefault );
    oST.bFilter = true;

    $('#example').dataTable( oST );
    [/code]
    This would have sorting disabled, but filtering enabled.

    It might not be as nice as the jQuery UI option, but to be honest, I hadn't actually considered this particular set up! You could stick the default object somewhere in your source files, so it doesn't need to be quite as ugly as I have above...

    Regards,
    Allan
  • matsumatsu Posts: 4Questions: 0Answers: 0
    Thanks Allan.

    All right...
    The code is not enough smart for me. I'd rather re-write original source code and set default values.

    Thank you again, Allan.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    The only line that isn't particularly attractive is the $.extend line - and that can be smoothed down to:

    [code]
    var oDefault = {
    "bFilter": false,
    "bSort": false
    };

    $('#example').dataTable( $.extend( true, {}, oDefault, {
    "bFilter": true
    } ) );
    [/code]
    The first parameter as 'true' isn't really needed in this case, but it would be if you were using aoColumns etc. But if you would rather alter the original source, that is a just as effective a solution :-)

    I'll have a think about other options to change the default parameters, but I suspect most of them would probably require about the same amount of code as this :-).

    Regards,
    Allan
  • matsumatsu Posts: 4Questions: 0Answers: 0
    We need to remember the variables which contains option, in this case 'oDefault'. It makes programmers care about about the default options.

    I took a look around the original source code and tried to separate default option value, it seems a large amount of modifications.
  • darthvandarthvan Posts: 9Questions: 0Answers: 0
    Hello I have simillar problem,is there a way to change sAjaxSource on button click.I try example from above but it didnt help.
This discussion has been closed.