I'm using Select2 in my website and I need to reload (change) the options in the select with new ones after some user interaction.
I have this:
When I chose one day, I need to reload hours. Because each day has different hours available, like this:
This is my javascript
object with all the info I need (No AJAX needed):
var ob = [
{
// Don't worry about activityId
activityId: '1234',
days: [
{
//Some date format
id: '20161001',
//Available hours for each day
hours: ["10:00", "11:00"]
},
{
id: '20161002',
hours: ["10:00", "12:00"]
}
]
},
{
activityId: '4567',
days: [
{
id: '20161003',
hours: ["10:30", "19:00"]
},
{
id: '20161002',
hours: ["10:00", "14:00"]
}
]
}
]
I've been trying with $('#selectId').select2({ data: data })
but it only adds data
to the current data
available and it only works once. Second time is not adding anymore.
I want to delete options and set new ones. I'm using this variable from select2 doc's:
var data = [
{ id: 0, text: 'enhancement' },
{ id: 1, text: 'bug' },
{ id: 2, text: 'duplicate' },
{ id: 3, text: 'invalid' },
{ id: 4, text: 'wontfix' }
];
Summarize: I want to change the data of the HOURS select when the DAYS select changes.
Any help? Thanks!