I try to write simple jquery plugin with some php code for displaying tables from database. Plugin allows user to search, go through the pages and sort. I know there's some great plugins for that, but I'm a newbie (6 months with any kind of programming) and I try to learn something.
Practically everything is done but I stopped at trying to do "multilevel" options.
The simplest option for user looks like that (almost all of them looks like that):
$('#main').ableTable({
mode :'sqlite'
})
Then it overrides the default settings:
var settings = $.extend({
mode : 'mysql'
} ,options);
That's simple.
But If I have some more complicated option like:
var settings = $.extend({
translate :{
navigation :{
prev :'previous' ,
next :'next'
} ,
search :'search' ,
no_result :'nothing was found' ,
from :'from' ,
total :'total'
}
// other options
} ,options);
Then if user declare only:
$('#main').ableTable({
translate :{
navigation :{
prev :'poprzedni'
}
}
})
It overwrites everything in settings.translate
How to correclty handle user defined options, if I wanna overwrite only the settings.navigation.prev (in this example) and leave the rest of default setttings?
You need to use the deep
parameter in the overloaded extend()
function - it's basically another version of the same function, but it makes it recursive so it looks within the object, rather than just the top level...
var settings = {
translate :{
navigation :{
prev :'previous' ,
next :'next'
} ,
search :'search' ,
no_result :'nothing was found' ,
from :'from' ,
total :'total'
}
}
var options = {
translate: {
navigation: {
prev: "something else"
}
}
};
$.extend(true, settings, options);
Have a look at the documentation for extend()
to find out more...
https://api.jquery.com/jquery.extend/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With