javascript - Modal Bootstrap User Confirmation -
how can create jquery function waits till user confirms whether or not wants delete data.
how can achieve this:
function userconfirmation() { showmodal(); //"global" delete confirmation bootstrap modal //now wait till user decides whether or not wants //delete data(button events or on modal hide). //on user reply: if(userreplied == 'delete') {} else {} }
edit:
this html code modal in layout. modaldelete called through showmodal() function whenever need be.
<!-- delete data confirmation modal --> <div class="modal fade" id="modaldelete" role="dialog" data-backdrop="static" data-keyboard="false"> <div class="modal-dialog modal-sm"> <!-- modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" id="btn-cancel" data-dismiss="modal">×</button> <h4 class="modal-title">confirm delete</h4> </div> <div class="modal-body"> </div> <div class="modal-footer"> <button type="button" class="btn btn-success" id="btn-cancel" data-dismiss="modal">cancel</button> <button type="button" class="btn btn-danger btn-delete" id="btn-delete" data-dismiss="modal">delete</button> </div> </div> </div> </div>
the function userconfirmation listens modal events after calling showmodal() , on button press or modal hide proceed depending on user's choice.
i think can achieved through promise or deffered action.
ok here how solved question using promise jquery deferred:
this function listens till user clicks on button in #modaldelete , returns id of button clicked.
function modaloptionchoosen() { var deferred = $.deferred(); $('#modaldelete button').on('click', function (e) { var $target = $(e.target); // clicked button element $(this).closest('.modal').on('hidden.bs.modal', function () { //console.log($target); // logged if clicked button closed modal var result = $target.attr('id'); deferred.resolve(result); }); }); return deferred.promise(); };
while function waits promise above modaloptionchoosen() returned.
function showmodalwaitforresult () { showmodal(); var promise = modaloptionchoosen(); promise.done(function (result) { console.log(result); // log user selected value }); });
now can reference modaloptionchoosen function anywhere if need be.
Comments
Post a Comment