javascript - SweetAlert2 add dynamic options to select input -
i want add dynamic options sweetalert2 input type select looks this:
swal({ title: 'select ukraine', input: 'select', inputoptions: { 'srb': 'serbia', 'ukr': 'ukraine', 'hrv': 'croatia' }, inputplaceholder: 'select country', showcancelbutton: true, inputvalidator: function(value) { return new promise(function(resolve, reject) { if (value === 'ukr') { resolve(); } else { reject('you need select ukraine :)'); } }); } }).then(function(result) { swal({ type: 'success', html: 'you selected: ' + result }); })
instead of 3 countries want add own options saved in object. how can using javascript?
you possibly try building options object this.
function linkthing() { //this assume loaded via ajax or something? var myarrayofthings = [ { id: 1, name: 'item 1' }, { id: 2, name: 'item 2' }, { id: 3, name: 'item 3' } ]; var options = {}; $.map(myarrayofthings, function(o) { options[o.id] = o.name; }); swal({ title: 'my title', text: 'please select option', input: 'select', inputoptions: options, showcancelbutton: true, animation: 'slide-from-top', inputplaceholder: 'please select' }).then(function (inputvalue) { if (inputvalue) { console.log(inputvalue); } }); };
Comments
Post a Comment