Javascript自定义配置消息SweetAlert.js

时间:2019-12-05 15:02:17

标签: javascript sweetalert

我想在我的ASP.NET MVC应用程序中显示自定义确认消息。

经过一番搜索,我发现了SweetAlert,这是一个非常不错的工具。

https://sweetalert2.github.io/

我想在js文件中定义一个javascript方法,该方法调用sweetalert来显示对话框。

但是文档不会等待客户端的响应。

为了演示我的意思,我添加了以下代码。

代码警报(“不等待”);在sweetalert显示其消息之前执行。

我的目标是添加一个自定义javascript文件并定义一个simble函数来调用并返回true或false,以避免在每种确认情况下都键入以下所有代码。 因为它不等待客户端交互,所以我不知道是否可能。

有什么主意吗?

    <html>
    <head>
      <script src="SweetAlert.js"></script>
    </head>
    <body>
    <button onclick="customConfirm();">Confirm</button>
    </body>
    </html>

    <script type="text/javascript">

   function customConfirm(){
    Swal.fire({
    title: 'myTitle',
    text: 'my Question',
    type: 'question',
    showCancelButton: true,
    confirmButtonColor: 'rgb(181, 212, 83)',
    cancelButtonColor: '#d33',
    confirmButtonText: 'YES'
   }).then((result) => {
    if (result.value) {
        return true;
    }
    else {
        return false;
    }
   });

   alert("doesn't wait.");
  }

   </script>

1 个答案:

答案 0 :(得分:1)

您应该在回调中执行所有检查。

     private void selectDialog()
{
    AppCompatTextView txtCamera;
    AppCompatTextView txtGallery;
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.dialog_select_camera_or_gallery, null);

    txtCamera = view.findViewById(R.id.selectCameraOrGalleryTxtCamera);
    txtGallery = view.findViewById(R.id.selectCameraOrGalleryTxtGallery);

    txtCamera.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            currentPhotoPath = utils.takeFullSizePicFromCamera(RegisterActivity.this, REQUEST_IMAGE_CAPTURE_FULL_SIZE).toString();
            alertDialog.dismiss();
        }
    });

    txtGallery.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            takePicFromGallery();
            alertDialog.dismiss();
        }
    });

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setView(view);
    alertDialogBuilder.setCancelable(true);
    alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

private void takePicFromGallery()
{
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    String[] mimeTypes = new String[]{"image/jpeg", "image/png", "image/jpg"};
    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
    intent.putExtra("android.intent.extra.TITLE", "گالری مورد نظر را انتخاب کنید");
    startActivityForResult(intent, REQUEST_CHOOSE_IMAGE_FROM_GALLERY);
}

在另一个文件中:

         if (requestCode == REQUEST_IMAGE_CAPTURE_FULL_SIZE && resultCode == RESULT_OK)
    {
        try
        {
            String uri = MediaStore.Images.Media.insertImage(getContentResolver(), currentPhotoPath, "", "");
            utils.ImageCropFunction(this, Uri.parse(uri), cropMessageIntent, REQUEST_CROP_IMAGE);

        } catch (Exception e)
        {
            Toast.makeText(getApplicationContext(), "خطا در پردازش عکس", Toast.LENGTH_SHORT).show();
        }
    }

    else if (requestCode == REQUEST_CHOOSE_IMAGE_FROM_GALLERY && resultCode == RESULT_OK)
    {
        utils.ImageCropFunction(this, selectedImage, cropMessageIntent, REQUEST_CROP_IMAGE);
    }

    else if (requestCode == REQUEST_CROP_IMAGE && resultCode == RESULT_OK)
    {
        Bitmap bitmap = utils.getBitmap(RegisterActivity.this, data);
        imageData = utils.getStringImage(bitmap);
        profileImage.setImageBitmap(bitmap);
    }

或者:

   function customConfirm(callback){
    Swal.fire({
    title: 'myTitle',
    text: 'my Question',
    type: 'question',
    showCancelButton: true,
    confirmButtonColor: 'rgb(181, 212, 83)',
    cancelButtonColor: '#d33',
    confirmButtonText: 'YES'
   }).then((result) => {
    // Do some stuff and call the callback
    callback();
    if (result.value) {
        return true;
    }
    else {
        return false;
    }
   });