Javascript - Sweetalert - 将值添加到选择列表

时间:2016-08-12 17:48:52

标签: javascript sweetalert

我正在使用SweetAlert2,并有一个选择列表。我的挑战是选择列表中的值是以编程方式添加的。当我的代码运行时,下拉列表中有正确的NUMBER个值,文本显示[object Object]而不是我添加的内容。我究竟做错了什么?代码如下。

     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
    });
  }) 

2 个答案:

答案 0 :(得分:2)

您必须向JavaScript对象添加动态属性

像这样:data[propertyName] = propertyValue;

&#13;
&#13;
    var inputOptions = {}; // Define like this!

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

		var vREGISTRY_ID = "500";
		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
        });
      }) 
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

假设您的“数据”是来自Api调用的响应,您可以更改响应格式。然后,您可以返回Dictionary<int,string>形式的字典并直接绑定响应:inputOptions: data