EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: jega on December 26, 2020, 08:10:36 AM



Title: messager.confirm in for loop [SOLVED]
Post by: jega on December 26, 2020, 08:10:36 AM
Hi.

Have a for loop with a confirm messager box. When waiting for my answer, the loop runs to end and write "Test 0".
But when i remove the messager and only use confirm('Are you sure to send data'), the loop pause until my answer, and after answer it goes to console.log() and loops to next.

for (var i = 0; i < sendData.length; i++) {
   if (sendData == '1'){

      //var r = confirm("Are you sure to send data");

      $.messager.confirm({
         width:400,
         title: 'Test',
         msg: 'Are you sure to send data',
         ok: 'Yes',
         cancel: 'No',
         fn: function(r){
            sendFTPData(url,r)
         }
      });
   };
   console.log('Test '+i)
}

Regards
Jesper


Title: Re: messager.confirm in for loop
Post by: jarry on December 27, 2020, 02:24:23 AM
The messager works in async mode, so you can't block it like the native 'confirm' function. Here is the possible way to send your array data with confirmation.
Code:
function confirmData(data){
  // filter the data to send
  data = data.filter(item => item == 1);
  // the data item index
  var index = 0;
  var el = $('<div></div>').on('confirm', function(){
    if (index < data.length){
      $.messager.confirm({
        title: 'Test',
        msg: 'Are you sure to send data?',
        fn: function(r){
          console.log('send '+index);
          index++;
          el.triggerHandler('confirm');
        }
      })
    }
  })
  el.triggerHandler('confirm');
}

var sendData = [1,2,1,3];
confirmData(sendData);



Title: Re: messager.confirm in for loop
Post by: jega on December 29, 2020, 12:40:43 PM
Thanks.

Works in my setup