2016-06-29 12 views
1

を選択するために、動的なオプションを追加することが次のようになります選択:代わりに、私は内に保存されている自分のオプションを追加したいものを3カ国のSweetAlert2は、私が<a href="https://sweetalert2.github.io/" rel="nofollow noreferrer">SweetAlert2</a>入力タイプに動的なオプションを追加したい入力

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 to select Ukraine :)'); 
    } 
    }); 
} 
}).then(function(result) { 
    swal({ 
    type: 'success', 
    html: 'You selected: ' + result 
    }); 
}) 

オブジェクト。どうすればjavascriptを使ってそれを行うことができますか?

+0

「動的オプション」とはどういう意味ですか? –

+0

@Andrei Zamfir: "オブジェクトに保存された自分のオプション"は、var savedObject = {'value_1': 'name_1'、 'value_2': 'name_2'、 'value_3': 'name_3'} 'ではない ? –

+0

ええ、それは私が意味するものです –

答えて

0

あなたはこのようなオプションオブジェクトを構築する可能性があります。

function linkThing() { 

     //this i assume would be 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 an option', 
      input: 'select', 
      inputOptions: options, 
      showCancelButton: true, 
      animation: 'slide-from-top', 
      inputPlaceholder: 'Please select' 
     }).then(function (inputValue) { 
      if (inputValue) { 

       console.log(inputValue); 

      } 
     }); 
    }; 
関連する問題