Yii2: Replace default confirmation message used by Gridview with Sweet alert












2















I am using yii2mod/yii2-sweet-alert in my projects, I am using it on basic and advanced themes, and I love it.



The question.
How can I change the grid default confirmation dialog that is a plain javascript confirmation in order to use Sweet-alert to make it look better?



I already tried modifying the button's template for the delete, because if you want to change the message you will do the following:



        [
'class' => ActionColumn::className(),
'template' => '{update}{delete}',
'buttons' => [
'delete' => function($url, $model){
return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['delete', 'id' => $model->id], [
'class' => '',
'data' => [
'confirm' => 'Are you absolutely sure ? You will lose all the information about this user with this action.',
'method' => 'post',
],
]);
}
]
]


But I haven't been successful on changing the confirmation message from the javascript one to sweet alert.





Also I am trying as a second option, to make it work with Krajee/ Grid and actionColumn but still can make it work «this is the second option am working, to do this».



        [
'class' => 'kartikgridActionColumn',
'viewOptions' => ['hidden' => true],
'updateOptions' => ['title' => 'Edit events', 'data-toggle' => '??'],
'deleteOptions' => ['title' => 'delete your event', 'data-toggle' => 'I am Lost here'],
],


from classic to sweet alert



Please any idea on how to change this behavior?





MORE ON HOW TO SOLVE IT - thanks to @muhammad-omer-aslam





  1. create a js file on your public folder, in my case
    /backend/web/js/confirmSwal.js and add the provided code:



    Add these lines



    yii.confirm = function (message, okCallback, cancelCallback) {
    swal({
    title: message,
    type: 'warning',
    showCancelButton: true,
    closeOnConfirm: true,
    allowOutsideClick: true
    }, okCallback);
    };



  2. Add this to your AppAssets on



    /backend/assets/AppAssets.php



    public $js = [
    '/js/confirmSwal.js',
    ];


  3. And that's it it works beautiful.



enter image description here



Thanks again to Muhammad.










share|improve this question





























    2















    I am using yii2mod/yii2-sweet-alert in my projects, I am using it on basic and advanced themes, and I love it.



    The question.
    How can I change the grid default confirmation dialog that is a plain javascript confirmation in order to use Sweet-alert to make it look better?



    I already tried modifying the button's template for the delete, because if you want to change the message you will do the following:



            [
    'class' => ActionColumn::className(),
    'template' => '{update}{delete}',
    'buttons' => [
    'delete' => function($url, $model){
    return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['delete', 'id' => $model->id], [
    'class' => '',
    'data' => [
    'confirm' => 'Are you absolutely sure ? You will lose all the information about this user with this action.',
    'method' => 'post',
    ],
    ]);
    }
    ]
    ]


    But I haven't been successful on changing the confirmation message from the javascript one to sweet alert.





    Also I am trying as a second option, to make it work with Krajee/ Grid and actionColumn but still can make it work «this is the second option am working, to do this».



            [
    'class' => 'kartikgridActionColumn',
    'viewOptions' => ['hidden' => true],
    'updateOptions' => ['title' => 'Edit events', 'data-toggle' => '??'],
    'deleteOptions' => ['title' => 'delete your event', 'data-toggle' => 'I am Lost here'],
    ],


    from classic to sweet alert



    Please any idea on how to change this behavior?





    MORE ON HOW TO SOLVE IT - thanks to @muhammad-omer-aslam





    1. create a js file on your public folder, in my case
      /backend/web/js/confirmSwal.js and add the provided code:



      Add these lines



      yii.confirm = function (message, okCallback, cancelCallback) {
      swal({
      title: message,
      type: 'warning',
      showCancelButton: true,
      closeOnConfirm: true,
      allowOutsideClick: true
      }, okCallback);
      };



    2. Add this to your AppAssets on



      /backend/assets/AppAssets.php



      public $js = [
      '/js/confirmSwal.js',
      ];


    3. And that's it it works beautiful.



    enter image description here



    Thanks again to Muhammad.










    share|improve this question



























      2












      2








      2








      I am using yii2mod/yii2-sweet-alert in my projects, I am using it on basic and advanced themes, and I love it.



      The question.
      How can I change the grid default confirmation dialog that is a plain javascript confirmation in order to use Sweet-alert to make it look better?



      I already tried modifying the button's template for the delete, because if you want to change the message you will do the following:



              [
      'class' => ActionColumn::className(),
      'template' => '{update}{delete}',
      'buttons' => [
      'delete' => function($url, $model){
      return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['delete', 'id' => $model->id], [
      'class' => '',
      'data' => [
      'confirm' => 'Are you absolutely sure ? You will lose all the information about this user with this action.',
      'method' => 'post',
      ],
      ]);
      }
      ]
      ]


      But I haven't been successful on changing the confirmation message from the javascript one to sweet alert.





      Also I am trying as a second option, to make it work with Krajee/ Grid and actionColumn but still can make it work «this is the second option am working, to do this».



              [
      'class' => 'kartikgridActionColumn',
      'viewOptions' => ['hidden' => true],
      'updateOptions' => ['title' => 'Edit events', 'data-toggle' => '??'],
      'deleteOptions' => ['title' => 'delete your event', 'data-toggle' => 'I am Lost here'],
      ],


      from classic to sweet alert



      Please any idea on how to change this behavior?





      MORE ON HOW TO SOLVE IT - thanks to @muhammad-omer-aslam





      1. create a js file on your public folder, in my case
        /backend/web/js/confirmSwal.js and add the provided code:



        Add these lines



        yii.confirm = function (message, okCallback, cancelCallback) {
        swal({
        title: message,
        type: 'warning',
        showCancelButton: true,
        closeOnConfirm: true,
        allowOutsideClick: true
        }, okCallback);
        };



      2. Add this to your AppAssets on



        /backend/assets/AppAssets.php



        public $js = [
        '/js/confirmSwal.js',
        ];


      3. And that's it it works beautiful.



      enter image description here



      Thanks again to Muhammad.










      share|improve this question
















      I am using yii2mod/yii2-sweet-alert in my projects, I am using it on basic and advanced themes, and I love it.



      The question.
      How can I change the grid default confirmation dialog that is a plain javascript confirmation in order to use Sweet-alert to make it look better?



      I already tried modifying the button's template for the delete, because if you want to change the message you will do the following:



              [
      'class' => ActionColumn::className(),
      'template' => '{update}{delete}',
      'buttons' => [
      'delete' => function($url, $model){
      return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['delete', 'id' => $model->id], [
      'class' => '',
      'data' => [
      'confirm' => 'Are you absolutely sure ? You will lose all the information about this user with this action.',
      'method' => 'post',
      ],
      ]);
      }
      ]
      ]


      But I haven't been successful on changing the confirmation message from the javascript one to sweet alert.





      Also I am trying as a second option, to make it work with Krajee/ Grid and actionColumn but still can make it work «this is the second option am working, to do this».



              [
      'class' => 'kartikgridActionColumn',
      'viewOptions' => ['hidden' => true],
      'updateOptions' => ['title' => 'Edit events', 'data-toggle' => '??'],
      'deleteOptions' => ['title' => 'delete your event', 'data-toggle' => 'I am Lost here'],
      ],


      from classic to sweet alert



      Please any idea on how to change this behavior?





      MORE ON HOW TO SOLVE IT - thanks to @muhammad-omer-aslam





      1. create a js file on your public folder, in my case
        /backend/web/js/confirmSwal.js and add the provided code:



        Add these lines



        yii.confirm = function (message, okCallback, cancelCallback) {
        swal({
        title: message,
        type: 'warning',
        showCancelButton: true,
        closeOnConfirm: true,
        allowOutsideClick: true
        }, okCallback);
        };



      2. Add this to your AppAssets on



        /backend/assets/AppAssets.php



        public $js = [
        '/js/confirmSwal.js',
        ];


      3. And that's it it works beautiful.



      enter image description here



      Thanks again to Muhammad.







      php yii2 yii2-advanced-app yii-extensions sweetalert






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 17 '18 at 17:30









      rob006

      9,83231133




      9,83231133










      asked Mar 11 '18 at 12:43









      moplinmoplin

      1,4471022




      1,4471022
























          1 Answer
          1






          active

          oldest

          votes


















          4














          UPDATE2



          Just correction in the code you need to do





          • okCallback.call() or add parenthesis () like 'okCallback()`

          • and then cancelCallback.call() or add parenthesis cancelCallback()


          inside the .then((selection)=>{}) and it is an anonymous function and needs to be called rather than just using okCallback so the code inside the .then((selection)=>{}) becomes



          if(selection){ okCallback.call();}else{ cancelCallback.call();}


          UPDATE



          The following options are deprecated in sweetalert 2.0 version,




          • callback in favor of promise:
            If the user clicks the confirm button, the promise resolves to true. If the alert is dismissed (by clicking outside of it), the promise resolves to null.


          • allowClickOutside is now closeOnClickOutside for clarity.



          • showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons or buttons: false to hide all buttons. By default, only the confirm button is shown.

          • When using a single string parameter (e.g. swal("Hello world!")), that parameter will be the modal's text instead of its title.


          • type and imageUrl have been replaced with a single icon option. If you're using the shorthand version (swal("Hi", "Hello world", "warning")) you don't have to change anything.


          So you can change the code to the following in case you are using ver 2.x or upgrading from 1.x.



          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          text: message,
          icon: 'warning',
          buttons : {
          cancel : {
          text : "Oops! No",
          value : null,
          visible : true,
          className : "",
          closeModal : true
          },
          confirm : {
          text : "Delete It Already",
          value : true,
          visible : true,
          className : "",
          closeModal : true
          }
          },
          closeOnClickOutside: true
          }).then((selection) => {
          if(selection){okCallback;}else{cancelCallback;}
          });
          }




          You can override the Yii2 default data-confirm popup with the following code:



          The basics are to include the asset, then add this JS:



          /**
          * Override the default yii confirm dialog. This function is
          * called by yii when a confirmation is requested.
          *
          * @param message the message to display
          * @param okCallback triggered when confirmation is true
          * @param cancelCallback callback triggered when canceled
          */
          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          title: message,
          type: 'warning',
          showCancelButton: true,
          closeOnConfirm: true,
          allowOutsideClick: true
          }, okCallback);
          };





          share|improve this answer


























          • Thank you. it works beautiful. I will extend your solution on my question.

            – moplin
            Mar 11 '18 at 13:23











          • :) you are welcome bro @moplin

            – Muhammad Omer Aslam
            Mar 11 '18 at 13:28











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49220106%2fyii2-replace-default-confirmation-message-used-by-gridview-with-sweet-alert%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          UPDATE2



          Just correction in the code you need to do





          • okCallback.call() or add parenthesis () like 'okCallback()`

          • and then cancelCallback.call() or add parenthesis cancelCallback()


          inside the .then((selection)=>{}) and it is an anonymous function and needs to be called rather than just using okCallback so the code inside the .then((selection)=>{}) becomes



          if(selection){ okCallback.call();}else{ cancelCallback.call();}


          UPDATE



          The following options are deprecated in sweetalert 2.0 version,




          • callback in favor of promise:
            If the user clicks the confirm button, the promise resolves to true. If the alert is dismissed (by clicking outside of it), the promise resolves to null.


          • allowClickOutside is now closeOnClickOutside for clarity.



          • showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons or buttons: false to hide all buttons. By default, only the confirm button is shown.

          • When using a single string parameter (e.g. swal("Hello world!")), that parameter will be the modal's text instead of its title.


          • type and imageUrl have been replaced with a single icon option. If you're using the shorthand version (swal("Hi", "Hello world", "warning")) you don't have to change anything.


          So you can change the code to the following in case you are using ver 2.x or upgrading from 1.x.



          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          text: message,
          icon: 'warning',
          buttons : {
          cancel : {
          text : "Oops! No",
          value : null,
          visible : true,
          className : "",
          closeModal : true
          },
          confirm : {
          text : "Delete It Already",
          value : true,
          visible : true,
          className : "",
          closeModal : true
          }
          },
          closeOnClickOutside: true
          }).then((selection) => {
          if(selection){okCallback;}else{cancelCallback;}
          });
          }




          You can override the Yii2 default data-confirm popup with the following code:



          The basics are to include the asset, then add this JS:



          /**
          * Override the default yii confirm dialog. This function is
          * called by yii when a confirmation is requested.
          *
          * @param message the message to display
          * @param okCallback triggered when confirmation is true
          * @param cancelCallback callback triggered when canceled
          */
          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          title: message,
          type: 'warning',
          showCancelButton: true,
          closeOnConfirm: true,
          allowOutsideClick: true
          }, okCallback);
          };





          share|improve this answer


























          • Thank you. it works beautiful. I will extend your solution on my question.

            – moplin
            Mar 11 '18 at 13:23











          • :) you are welcome bro @moplin

            – Muhammad Omer Aslam
            Mar 11 '18 at 13:28
















          4














          UPDATE2



          Just correction in the code you need to do





          • okCallback.call() or add parenthesis () like 'okCallback()`

          • and then cancelCallback.call() or add parenthesis cancelCallback()


          inside the .then((selection)=>{}) and it is an anonymous function and needs to be called rather than just using okCallback so the code inside the .then((selection)=>{}) becomes



          if(selection){ okCallback.call();}else{ cancelCallback.call();}


          UPDATE



          The following options are deprecated in sweetalert 2.0 version,




          • callback in favor of promise:
            If the user clicks the confirm button, the promise resolves to true. If the alert is dismissed (by clicking outside of it), the promise resolves to null.


          • allowClickOutside is now closeOnClickOutside for clarity.



          • showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons or buttons: false to hide all buttons. By default, only the confirm button is shown.

          • When using a single string parameter (e.g. swal("Hello world!")), that parameter will be the modal's text instead of its title.


          • type and imageUrl have been replaced with a single icon option. If you're using the shorthand version (swal("Hi", "Hello world", "warning")) you don't have to change anything.


          So you can change the code to the following in case you are using ver 2.x or upgrading from 1.x.



          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          text: message,
          icon: 'warning',
          buttons : {
          cancel : {
          text : "Oops! No",
          value : null,
          visible : true,
          className : "",
          closeModal : true
          },
          confirm : {
          text : "Delete It Already",
          value : true,
          visible : true,
          className : "",
          closeModal : true
          }
          },
          closeOnClickOutside: true
          }).then((selection) => {
          if(selection){okCallback;}else{cancelCallback;}
          });
          }




          You can override the Yii2 default data-confirm popup with the following code:



          The basics are to include the asset, then add this JS:



          /**
          * Override the default yii confirm dialog. This function is
          * called by yii when a confirmation is requested.
          *
          * @param message the message to display
          * @param okCallback triggered when confirmation is true
          * @param cancelCallback callback triggered when canceled
          */
          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          title: message,
          type: 'warning',
          showCancelButton: true,
          closeOnConfirm: true,
          allowOutsideClick: true
          }, okCallback);
          };





          share|improve this answer


























          • Thank you. it works beautiful. I will extend your solution on my question.

            – moplin
            Mar 11 '18 at 13:23











          • :) you are welcome bro @moplin

            – Muhammad Omer Aslam
            Mar 11 '18 at 13:28














          4












          4








          4







          UPDATE2



          Just correction in the code you need to do





          • okCallback.call() or add parenthesis () like 'okCallback()`

          • and then cancelCallback.call() or add parenthesis cancelCallback()


          inside the .then((selection)=>{}) and it is an anonymous function and needs to be called rather than just using okCallback so the code inside the .then((selection)=>{}) becomes



          if(selection){ okCallback.call();}else{ cancelCallback.call();}


          UPDATE



          The following options are deprecated in sweetalert 2.0 version,




          • callback in favor of promise:
            If the user clicks the confirm button, the promise resolves to true. If the alert is dismissed (by clicking outside of it), the promise resolves to null.


          • allowClickOutside is now closeOnClickOutside for clarity.



          • showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons or buttons: false to hide all buttons. By default, only the confirm button is shown.

          • When using a single string parameter (e.g. swal("Hello world!")), that parameter will be the modal's text instead of its title.


          • type and imageUrl have been replaced with a single icon option. If you're using the shorthand version (swal("Hi", "Hello world", "warning")) you don't have to change anything.


          So you can change the code to the following in case you are using ver 2.x or upgrading from 1.x.



          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          text: message,
          icon: 'warning',
          buttons : {
          cancel : {
          text : "Oops! No",
          value : null,
          visible : true,
          className : "",
          closeModal : true
          },
          confirm : {
          text : "Delete It Already",
          value : true,
          visible : true,
          className : "",
          closeModal : true
          }
          },
          closeOnClickOutside: true
          }).then((selection) => {
          if(selection){okCallback;}else{cancelCallback;}
          });
          }




          You can override the Yii2 default data-confirm popup with the following code:



          The basics are to include the asset, then add this JS:



          /**
          * Override the default yii confirm dialog. This function is
          * called by yii when a confirmation is requested.
          *
          * @param message the message to display
          * @param okCallback triggered when confirmation is true
          * @param cancelCallback callback triggered when canceled
          */
          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          title: message,
          type: 'warning',
          showCancelButton: true,
          closeOnConfirm: true,
          allowOutsideClick: true
          }, okCallback);
          };





          share|improve this answer















          UPDATE2



          Just correction in the code you need to do





          • okCallback.call() or add parenthesis () like 'okCallback()`

          • and then cancelCallback.call() or add parenthesis cancelCallback()


          inside the .then((selection)=>{}) and it is an anonymous function and needs to be called rather than just using okCallback so the code inside the .then((selection)=>{}) becomes



          if(selection){ okCallback.call();}else{ cancelCallback.call();}


          UPDATE



          The following options are deprecated in sweetalert 2.0 version,




          • callback in favor of promise:
            If the user clicks the confirm button, the promise resolves to true. If the alert is dismissed (by clicking outside of it), the promise resolves to null.


          • allowClickOutside is now closeOnClickOutside for clarity.



          • showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons or buttons: false to hide all buttons. By default, only the confirm button is shown.

          • When using a single string parameter (e.g. swal("Hello world!")), that parameter will be the modal's text instead of its title.


          • type and imageUrl have been replaced with a single icon option. If you're using the shorthand version (swal("Hi", "Hello world", "warning")) you don't have to change anything.


          So you can change the code to the following in case you are using ver 2.x or upgrading from 1.x.



          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          text: message,
          icon: 'warning',
          buttons : {
          cancel : {
          text : "Oops! No",
          value : null,
          visible : true,
          className : "",
          closeModal : true
          },
          confirm : {
          text : "Delete It Already",
          value : true,
          visible : true,
          className : "",
          closeModal : true
          }
          },
          closeOnClickOutside: true
          }).then((selection) => {
          if(selection){okCallback;}else{cancelCallback;}
          });
          }




          You can override the Yii2 default data-confirm popup with the following code:



          The basics are to include the asset, then add this JS:



          /**
          * Override the default yii confirm dialog. This function is
          * called by yii when a confirmation is requested.
          *
          * @param message the message to display
          * @param okCallback triggered when confirmation is true
          * @param cancelCallback callback triggered when canceled
          */
          yii.confirm = function (message, okCallback, cancelCallback) {
          swal({
          title: message,
          type: 'warning',
          showCancelButton: true,
          closeOnConfirm: true,
          allowOutsideClick: true
          }, okCallback);
          };






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 31 '18 at 16:25

























          answered Mar 11 '18 at 13:16









          Muhammad Omer AslamMuhammad Omer Aslam

          12.9k72544




          12.9k72544













          • Thank you. it works beautiful. I will extend your solution on my question.

            – moplin
            Mar 11 '18 at 13:23











          • :) you are welcome bro @moplin

            – Muhammad Omer Aslam
            Mar 11 '18 at 13:28



















          • Thank you. it works beautiful. I will extend your solution on my question.

            – moplin
            Mar 11 '18 at 13:23











          • :) you are welcome bro @moplin

            – Muhammad Omer Aslam
            Mar 11 '18 at 13:28

















          Thank you. it works beautiful. I will extend your solution on my question.

          – moplin
          Mar 11 '18 at 13:23





          Thank you. it works beautiful. I will extend your solution on my question.

          – moplin
          Mar 11 '18 at 13:23













          :) you are welcome bro @moplin

          – Muhammad Omer Aslam
          Mar 11 '18 at 13:28





          :) you are welcome bro @moplin

          – Muhammad Omer Aslam
          Mar 11 '18 at 13:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49220106%2fyii2-replace-default-confirmation-message-used-by-gridview-with-sweet-alert%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'