Irfan
Irfan

Reputation: 5062

Add dynamic values to the options of plugin

I'm using jquery plugin http://amsul.ca/pickadate.js/date/

I'm in a situation where I need to add dynamic options/setting value to the plugin.

Example of working settings:

$input = $('.datepicker').pickadate({
              disable: [
                  { from: [2015,5,10], to: [2015,5,17] }
                 ]
           });

I'm want to create the from and to date from the database and here is my code:

booking_dates is an array output:

 Array
    (
        [0] => Array
            (
                [0] => 2015-06-02
                [1] => 2015-06-07
            )

        [1] => Array
            (
                [0] => 2015-06-10
                [1] => 2015-06-15
            )

        [2] => Array
            (
                [0] => 2015-06-16
                [1] => 2015-06-20
            )

    )

var booking_dates = ajax_object.booking_dates;
                var data = '';
                for(i = 0; i < booking_dates.length; i++ ) {
                    k = 0;

                    var from = booking_dates[i][k].split('-');
                    var to = booking_dates[i][++k].split('-');

                    if(i == 0) {
                        data = '{ from: [' + from[0] + ',' + (--from[1]) + ',' + from[2] + '], to: [' + to[0] + ','+ (--to[1]) + ',' + to[2] + '] }';
                    } else {
                        data = data + ', ' + '{ from: [' + from[0] + ',' + (--from[1]) + ',' + from[2] + '], to: [' + to[0] + ','+ (--to[1]) + ',' + to[2] + '] }';
                    }
                } 

                    alert (data); //{ from: [2015,5,15], to: [2015,5,20]}

data variable result is: { from: [2015,5,15], to: [2015,5,20] }

but when I replace data with settings then it does not work:

DOES NOT WORK:

  $input = $('.datepicker').pickadate({
                            disable: [
                               data
                            ]
                    });

is it a string therefore not working? Or I have to create the options string in different way?

Upvotes: 0

Views: 53

Answers (2)

Satpal
Satpal

Reputation: 133403

You should create proper an array and pass it.

var booking_dates = ajax_object.booking_dates;
var data = [];
for (i = 0; i < booking_dates.length; i++) {
    k = 0;
    var from = booking_dates[i][k].split('-');
    var to = booking_dates[i][++k].split('-');
    data.push({
        from: [from[0],  (--from[1]) , from[2]],
        to: to[0] , (--to[1]) , to[2]
    })   
}

Then use

$('.datepicker').pickadate({
    disable: data
});

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You need to create an array of objects instead you are creating a string

var booking_dates = ajax_object.booking_dates;
var data = [];
for (i = 0; i < booking_dates.length; i++) {
    k = 0;

    var from = booking_dates[i][k].split('-');
    var to = booking_dates[i][++k].split('-');

    data.push({
        from: [from[0], --from[1], from[2]],
        to: [to[0], --to[1], to[2]]
    })
}

alert(data);

$input = $('.datepicker').pickadate({
    disable: data
});

Upvotes: 1

Related Questions