2016-08-12 99 views
0

我使用SweetAlert2,並有一個選擇列表。我的挑戰是選擇列表中的值是以編程方式添加的。當我的代碼運行時,下拉列表有正確的NUMBER個值,文本顯示[object Object],而不是我添加的內容。我究竟做錯了什麼?代碼如下。Javascript - Sweetalert - 將值添加到選擇列表

 var outputStr = [];  

     for (var i = 0; i < data.rows.length; i++) { 

    // If here, we have data, so show the information....  
     var vREGISTRY_ID    = data.rows[i].REGISTRY_ID    ? data.rows[i].REGISTRY_ID     : '-'; 
     var vNN_NAME     = data.rows[i].NN_NAME     ? data.rows[i].NN_NAME      : '-'; 
     var vACCOUNT_NAME    = data.rows[i].ACCOUNT_NAME    ? data.rows[i].ACCOUNT_NAME     : '-'; 
     var vSITE_DUNS_9DIG    = data.rows[i].SITE_DUNS_9DIG   ? data.rows[i].SITE_DUNS_9DIG    : '-';   
     var vPRIMARY_CITY    = data.rows[i].PRIMARY_CITY    ? data.rows[i].PRIMARY_CITY     : '-'; 
     var vPRIMARY_STATE_PROVINCE  = data.rows[i].PRIMARY_STATE_PROVINCE ? data.rows[i].PRIMARY_STATE_PROVINCE  : '-'; 

     outputStr.push({ 
      value:vREGISTRY_ID, 
      label: vACCOUNT_NAME 
     }) ; 


     }; // end of FOR loop 

     swal({ 
     title: 'Select Account Name or Division', 
     input: 'select', 
     inputOptions: outputStr , 
     inputPlaceholder: 'Select from dropdown', 
     showCancelButton: true, 
     inputValidator: function(value) { 
     return new Promise(function(resolve, reject) { 
     if (value === 'abc') { 
     resolve(); 
     } else { 
     reject('You need to select abc :)'); 
     } 
    }); 
    } 
    }).then(function(result) { 
    swal({ 
    type: 'success', 
     html: 'You selected: ' + result 
    }); 
    }) 

回答

1

你必須動態屬性添加到JavaScript對象

像這樣:data[propertyName] = propertyValue;

var inputOptions = {}; // Define like this! 
 

 
\t \t // Instead of sample variables, 
 
\t \t // your data handling here 
 

 
\t \t var vREGISTRY_ID = "500"; 
 
\t \t var vACCOUNT_NAME = "Peter"; 
 

 
    
 
     
 
     // Add the Variables like this 
 
     // This will create '500' : 'Peter', 
 
     inputOptions[vREGISTRY_ID] = vACCOUNT_NAME; 
 
     inputOptions["455"] = "Martin"; 
 
     
 
     // Note that the options will get sorted by their value 
 

 
     swal({ 
 
     title: 'Select Account Name or Division', 
 
     input: 'select', 
 
     inputOptions: inputOptions, 
 
     inputPlaceholder: 'Select from dropdown', 
 
     showCancelButton: true, 
 
     inputValidator: function(value) { 
 
      return new Promise(function(resolve, reject) { 
 
      if (value == "500") { 
 
       resolve(); 
 
      } else { 
 
       reject('You need to select Peter :)'); 
 
      } 
 
      }); 
 
     } 
 
     }).then(function(result) { 
 
     swal({ 
 
      type: 'success', 
 
      html: 'You selected: ' + result 
 
     }); 
 
     })
<link href="https://cdn.jsdelivr.net/sweetalert2/4.1.5/sweetalert2.css" rel="stylesheet"/> 
 
<script src="https://cdn.jsdelivr.net/sweetalert2/4.1.5/sweetalert2.js"></script>

+0

這是SOOOO接近我想要的東西。無論如何添加數據而沒有自動排序?我想按照特定的順序輸入數據,這就是我如何添加它......(來自JSON數據集) – user1009073

+0

這是鉻,命令對象的屬性....嘗試將數字轉換爲字符串... if這不起作用,你可以嘗試使用'Map()',它保證了鍵的順序。有關Chrome「Bug」的更多信息,請參閱https://bugs.chromium.org/p/v8/issues/detail?id=164 – 2016-08-12 20:06:27

+0

請參閱此處獲取更好的解釋http://stackoverflow.com/a/38218582/5827241 – 2016-08-12 20:08:17

相關問題