ट्विटर बूटस्ट्रैप का उपयोग करके मोडल / संवाद में विलोपन की पुष्टि करें?


284

मेरे पास डेटाबेस पंक्तियों से बंधी पंक्तियों की एक HTML तालिका है। मैं प्रत्येक पंक्ति के लिए एक "पंक्ति हटाएं" लिंक रखना चाहता हूं, लेकिन मैं उपयोगकर्ता के साथ पहले से पुष्टि करना चाहता हूं।

क्या ट्विटर बूटस्ट्रैप मोडल डायलॉग का उपयोग करने का कोई तरीका है?



3
इस सवाल के पार जाने के बाद मैं इस मुद्दे के लिए इस तरह के एक सरल और अधिक सुव्यवस्थित "फिक्स" के साथ झंकार करना चाहता था। मैं इसके साथ थोड़ी देर के लिए संघर्ष किया और फिर महसूस किया कि यह कितना सरल हो सकता है: बस वास्तविक फ़ॉर्म सबमिशन बटन को मोडल डायलॉग में डालें, और फिर फॉर्म पर सबमिट बटन ही कुछ नहीं करता बल्कि डायलॉग विंडो को फायर करता है ... समस्या हल हुई।
बजे माइकल डोलमैन

@jonijones यह उदाहरण मेरे लिए काम नहीं कर रहा है (पुष्टि संदेश पहले बटन पर क्लिक करने पर प्रदर्शित नहीं होता है) - क्रोम में परीक्षण किया गया
egmfrs

जवाबों:


397

नुस्खा प्राप्त करें

इस कार्य के लिए आप पहले से उपलब्ध प्लगइन्स और बूटस्ट्रैप एक्सटेंशन का उपयोग कर सकते हैं। या आप कोड की सिर्फ 3 लाइनों के साथ अपना पुष्टिकरण पॉपअप बना सकते हैं । इसकी जांच - पड़ताल करें।

कहें कि हमारे पास यह लिंक हैं (नोट के data-hrefबजाय href) या बटन जिनके लिए हम पुष्टि हटाना चाहते हैं:

<a href="#" data-href="delete.php?id=23" data-toggle="modal" data-target="#confirm-delete">Delete record #23</a>

<button class="btn btn-default" data-href="/delete.php?id=54" data-toggle="modal" data-target="#confirm-delete">
    Delete record #54
</button>

यहां #confirm-deleteआपके HTML में एक मोडल पॉपअप div की ओर इशारा किया गया है। यह इस तरह कॉन्फ़िगर किया गया "ठीक" बटन होना चाहिए:

<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                ...
            </div>
            <div class="modal-body">
                ...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger btn-ok">Delete</a>
            </div>
        </div>
    </div>
</div>

अब आपको केवल हटाने की कार्रवाई की पुष्टि करने के लिए इस छोटी जावास्क्रिप्ट की आवश्यकता है:

$('#confirm-delete').on('show.bs.modal', function(e) {
    $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
});

इसलिए show.bs.modalईवेंट डिलीट बटन hrefको संबंधित रिकॉर्ड आईडी के साथ URL पर सेट किया जाता है।

डेमो: http://plnkr.co/edit/NePR0BQf3VmKtuMmhVR7?p=preview


पोस्ट नुस्खा

मुझे लगता है कि कुछ मामलों में GST के बजाय POST या DELETE अनुरोध करने की आवश्यकता हो सकती है। यह बहुत अधिक कोड के बिना अभी भी बहुत सरल है। नीचे दिए गए डेमो को इस दृष्टिकोण से देखें:

// Bind click to OK button within popup
$('#confirm-delete').on('click', '.btn-ok', function(e) {

  var $modalDiv = $(e.delegateTarget);
  var id = $(this).data('recordId');

  $modalDiv.addClass('loading');
  $.post('/api/record/' + id).then(function() {
     $modalDiv.modal('hide').removeClass('loading');
  });
});

// Bind to modal opening to set necessary data properties to be used to make request
$('#confirm-delete').on('show.bs.modal', function(e) {
  var data = $(e.relatedTarget).data();
  $('.title', this).text(data.recordTitle);
  $('.btn-ok', this).data('recordId', data.recordId);
});

डेमो: http://plnkr.co/edit/V4GUuSueuuxiGr4L9LmG?p=preview


बूटस्ट्रैप 2.3

जब मैंने बूटस्ट्रैप 2.3 मोडल के लिए इस प्रश्न का उत्तर दिया था, तो मैंने जो कोड बनाया था, उसका एक मूल संस्करण यहां है।

$('#modal').on('show', function() {
    var id = $(this).data('id'),
        removeBtn = $(this).find('.danger');
    removeBtn.attr('href', removeBtn.attr('href').replace(/(&|\?)ref=\d*/, '$1ref=' + id));
});

डेमो : http://jsfiddle.net/MjmVr/1595/


1
यह लगभग पूरी तरह से काम करता है, लेकिन यहां तक ​​कि फ़ेडल संस्करण (मेरी साइट के अनुसार) में, आईडी मोडल में हां बटन को पारित नहीं किया गया है। मैंने नोटिस किया कि आप बदले के बदले और रेफरी करने की कोशिश कर रहे हैं ? इसलिए मैंने इसे बदलने की कोशिश की, लेकिन यह अभी भी काम नहीं कर रहा है। क्या मुझे यहाँ कुछ और याद आ रहा है? यह अन्यथा आपकी मदद के लिए TIA महान है!
SWL

धन्यवाद @dfsq - इसने खूबसूरती से काम किया। संवाद 'नहीं' बटन पर क्लिक करने से छिपा नहीं है, इसलिए मैंने मोडल छिपाने के बजाय href को # बदल दिया है और यह काम भी करता है। आपकी सहायता के लिए एक बार फिर से धन्यवाद।
SWL

22
एक ट्वीक है अंतिम डिलीट रिक्वेस्ट पोस्ट में रिजल्ट होनी चाहिए, जीईटी नहीं जैसा कि आप एक लिंक के साथ करेंगे। यदि आप एक GET पर डिलीट की अनुमति देते हैं, तो दुर्भावनापूर्ण तृतीय पक्ष आसानी से उन साइटों या ईमेलों पर लिंक को शिल्प कर सकते हैं जो आपके उपयोगकर्ता को अनजाने में डिलीट की गई चीजों का कारण बनाते हैं। यह मूर्खतापूर्ण लग सकता है, लेकिन ऐसे परिदृश्य हैं जहां यह एक गंभीर सुरक्षा समस्या होगी।
एरोनल्स

2
तुम Vex पर एक नज़र रखना चाहते हो सकता है । इतना सरल कि तुम क्या पूछ रहे हो: jsfiddle.net/adamschwartz/hQump
एडम

3
एक विनाशकारी कार्रवाई करने के लिए एक GET का उपयोग करने के लिए नीचे की ओर चढ़ाया गया। कई, कई अलग-अलग कारण हैं जो आपको कभी नहीं करना चाहिए।
NickG

158

http://bootboxjs.com/ - बूटस्ट्रैप 3.0.0 के साथ नवीनतम कार्य करता है

सबसे सरल संभव उदाहरण:

bootbox.alert("Hello world!"); 

साइट से:

पुस्तकालय अपने मूल जावास्क्रिप्ट समकक्षों की नकल करने के लिए डिज़ाइन किए गए तीन तरीकों को उजागर करता है। उनकी सटीक विधि हस्ताक्षर लचीले होते हैं क्योंकि प्रत्येक लेबल को अनुकूलित करने और चूक को निर्दिष्ट करने के लिए विभिन्न मापदंडों को ले सकता है, लेकिन उन्हें आमतौर पर ऐसा कहा जाता है:

bootbox.alert(message, callback)
bootbox.prompt(message, callback)
bootbox.confirm(message, callback)

यहां कार्रवाई में इसका एक स्निपेट है (नीचे "रन कोड स्निपेट पर क्लिक करें"):

$(function() {
  bootbox.alert("Hello world!");
});
<!-- required includes -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>

<!-- bootbox.js at 4.4.0 -->
<script src="https://rawgit.com/makeusabrew/bootbox/f3a04a57877cab071738de558581fbc91812dce9/bootbox.js"></script>


2
दुर्भाग्य से, जिस क्षण आपको शीर्षक और बटन पर गैर-अंग्रेजी पाठ की आवश्यकता होती है, आपको या तो जेएस को संशोधित करना होगा या लगभग बूटस्ट्रैप HTML और जेएस को जोड़ने के रूप में लगभग पैरामीटर बनाना शुरू करना होगा। :)
जॉन्सल २२'१५ को

31
  // ---------------------------------------------------------- Generic Confirm  

  function confirm(heading, question, cancelButtonTxt, okButtonTxt, callback) {

    var confirmModal = 
      $('<div class="modal hide fade">' +    
          '<div class="modal-header">' +
            '<a class="close" data-dismiss="modal" >&times;</a>' +
            '<h3>' + heading +'</h3>' +
          '</div>' +

          '<div class="modal-body">' +
            '<p>' + question + '</p>' +
          '</div>' +

          '<div class="modal-footer">' +
            '<a href="#" class="btn" data-dismiss="modal">' + 
              cancelButtonTxt + 
            '</a>' +
            '<a href="#" id="okButton" class="btn btn-primary">' + 
              okButtonTxt + 
            '</a>' +
          '</div>' +
        '</div>');

    confirmModal.find('#okButton').click(function(event) {
      callback();
      confirmModal.modal('hide');
    });

    confirmModal.modal('show');     
  };

  // ---------------------------------------------------------- Confirm Put To Use

  $("i#deleteTransaction").live("click", function(event) {
    // get txn id from current table row
    var id = $(this).data('id');

    var heading = 'Confirm Transaction Delete';
    var question = 'Please confirm that you wish to delete transaction ' + id + '.';
    var cancelButtonTxt = 'Cancel';
    var okButtonTxt = 'Confirm';

    var callback = function() {
      alert('delete confirmed ' + id);
    };

    confirm(heading, question, cancelButtonTxt, okButtonTxt, callback);

  });

1
इसकी एक पुरानी पोस्ट लेकिन मैं वही काम करना चाहता हूं लेकिन जब मैं उपरोक्त कोड का उपयोग करता हूं तो मोडल डायलॉग डॉसेंट शो?
सौरभ

28

मुझे इसके पुराने प्रश्न का एहसास होगा, लेकिन जब से मैं बूटस्ट्रैप मॉडल्स को संभालने के अधिक कुशल तरीके के बारे में सोच रहा था। मैंने कुछ शोध किया और कुछ बेहतर पाया फिर जो समाधान ऊपर दिखाए गए हैं, वह इस लिंक पर मिल सकते हैं:

http://www.petefreitag.com/item/809.cfm

सबसे पहले जक्वेरी को लोड करें

$(document).ready(function() {
    $('a[data-confirm]').click(function(ev) {
        var href = $(this).attr('href');

        if (!$('#dataConfirmModal').length) {
            $('body').append('<div id="dataConfirmModal" class="modal" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="dataConfirmLabel">Please Confirm</h3></div><div class="modal-body"></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button><a class="btn btn-primary" id="dataConfirmOK">OK</a></div></div>');
        } 
        $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
        $('#dataConfirmOK').attr('href', href);
        $('#dataConfirmModal').modal({show:true});
        return false;
    });
});

फिर बस href पर कोई प्रश्न / पुष्टि करें:

<a href="/any/url/delete.php?ref=ID" data-confirm="Are you sure you want to delete?">Delete</a>

इस तरह से पुष्टि मोडल बहुत अधिक सार्वभौमिक है और इसलिए इसे आसानी से आपकी वेबसाइट के अन्य हिस्सों पर फिर से उपयोग किया जा सकता है।


4
कृपया बिना किसी अन्य स्रोत के कोड को पोस्ट न करें: petefreitag.com/item/809.cfm
ए वेब

4
हालांकि पहली बार में ऑपशन को भूल गया, यह मेरे लिए एकदम सही बात थी। एक जादू की तरह काम करता है।
Janis Peisenieks

3
मुझे लगता है कि यह GET http अनुरोध के साथ एक अच्छा विचार हटाना आइटम नहीं है
मिगुएल Prz

7
मम्मा ने मुझसे कहा कि किसी भी ठंडे संलयन लिंक पर क्लिक न करें
माइक पर्सल

3
@BenY यह इस बारे में नहीं है कि उपयोगकर्ता के पास चीजें करने की अनुमति है या नहीं, यह दुर्भावनापूर्ण उपयोगकर्ताओं के बारे में है जिनके पास पहले से ही अन्य साइटों, ईमेलों आदि पर लिंक पर क्लिक करने की अनुमति है, इसलिए दुर्भावनापूर्ण उपयोगकर्ता उन उपयोगकर्ताओं की अनुमति का लाभ उठा सकते हैं।
ब्रेट

17

@ Jousby के समाधान के लिए धन्यवाद , मैं अपना काम करने में सफल रहा, लेकिन मैंने पाया कि इसे अपने समाधान के बूटस्ट्रैप मोडल मार्कअप को थोड़ा सुधारना था, ताकि इसे सही तरीके से प्रस्तुत किया जा सके जैसा कि आधिकारिक उदाहरणों में दिखाया गया है ।

तो, यहाँ जेनेरिक confirmफ़ंक्शन का मेरा संशोधित संस्करण है जो ठीक काम करता है:

/* Generic Confirm func */
  function confirm(heading, question, cancelButtonTxt, okButtonTxt, callback) {

    var confirmModal = 
      $('<div class="modal fade">' +        
          '<div class="modal-dialog">' +
          '<div class="modal-content">' +
          '<div class="modal-header">' +
            '<a class="close" data-dismiss="modal" >&times;</a>' +
            '<h3>' + heading +'</h3>' +
          '</div>' +

          '<div class="modal-body">' +
            '<p>' + question + '</p>' +
          '</div>' +

          '<div class="modal-footer">' +
            '<a href="#!" class="btn" data-dismiss="modal">' + 
              cancelButtonTxt + 
            '</a>' +
            '<a href="#!" id="okButton" class="btn btn-primary">' + 
              okButtonTxt + 
            '</a>' +
          '</div>' +
          '</div>' +
          '</div>' +
        '</div>');

    confirmModal.find('#okButton').click(function(event) {
      callback();
      confirmModal.modal('hide');
    }); 

    confirmModal.modal('show');    
  };  
/* END Generic Confirm func */

3
यहाँ महान समाधान। मैंने रद्द लिंक के लिए कॉलबैक को संभालने के लिए कुछ मामूली संशोधन किए। एक छोटी सिफारिश # का उपयोग करें! पृष्ठ को शीर्ष पर स्क्रॉल करने से रोकने के लिए अपने href में # के बजाय।
डोमिनिक डी।

अगर मैं इस वोट को दोगुना कर सकता हूं तो मैं करूंगा। अच्छा और सुरुचिपूर्ण। धन्यवाद।
निगेल जॉनसन

बहुत अच्छा समाधान है। एक और सुधार जो मैं सुझा सकता हूं, वह है एक और तर्क जोड़ना: btnType = "btn-primary"और फिर ओके बटन के कोड को बदलना होगा class="btn ' + btnType + '"। इस तरह से एक ओके बटन की उपस्थिति को बदलने के लिए एक वैकल्पिक तर्क पारित किया जा सकता है, जैसे btn-dangerकि हटाएं।
IAMNaN

धन्यवाद। इसके लिए मुझे सही तरीके से रेंडर करने के लिए <h3> और <a> टैग (h3 सबसे पहले) को स्वैप करना पड़ा।
डेव डॉकिन्स

10

मुझे यह उपयोगी और उपयोग में आसान लगा, साथ ही यह बहुत अच्छा लग रहा है: http://maxailloud.github.io/confirm-bootstrap/

इसका उपयोग करने के लिए, .js फ़ाइल को अपने पृष्ठ में शामिल करें, फिर चलाएँ:

$('your-link-selector').confirmModal();

ऐसे कई विकल्प हैं जिन पर आप इसे लागू कर सकते हैं, इसे हटाने के लिए इसे बेहतर करने के लिए इसे हटा दें, मैं उपयोग करता हूं:

$('your-link-selector').confirmModal({
    confirmTitle: 'Please confirm',
    confirmMessage: 'Are you sure you want to delete this?',
    confirmStyle: 'danger',
    confirmOk: '<i class="icon-trash icon-white"></i> Delete',
    confirmCallback: function (target) {
         //perform the deletion here, or leave this option
         //out to just follow link..
    }
});

यह एक अच्छा काम है
लोरेटोपरिसी

4

मैं इस प्रकार के कार्य को आसानी से bootbox.js लाइब्रेरी का उपयोग करके संभाल सकता हूं। सबसे पहले आपको बूटबॉक्स JS फाइल को शामिल करना होगा। तब आपके ईवेंट हैंडलर फ़ंक्शन में बस निम्नलिखित कोड लिखें:

    bootbox.confirm("Are you sure to want to delete , function(result) {

    //here result will be true
    // delete process code goes here

    });

आधिकारिक बूटबॉक्सjs साइट


2

निम्नलिखित समाधान bootbox.js से बेहतर है , क्योंकि

  • यह सब कुछ कर सकता है bootbox.js कर सकते हैं;
  • उपयोग सिंटैक्स सरल है
  • यह आपको "त्रुटि", "चेतावनी" या "जानकारी" का उपयोग करके अपने संदेश के रंग को सुरुचिपूर्ण ढंग से नियंत्रित करने की अनुमति देता है
  • बूटबॉक्स 986 लाइन लंबा है, मेरा केवल 110 लाइनें लंबा है

digimango.messagebox.js :

const dialogTemplate = '\
    <div class ="modal" id="digimango_messageBox" role="dialog">\
        <div class ="modal-dialog">\
            <div class ="modal-content">\
                <div class ="modal-body">\
                    <p class ="text-success" id="digimango_messageBoxMessage">Some text in the modal.</p>\
                    <p><textarea id="digimango_messageBoxTextArea" cols="70" rows="5"></textarea></p>\
                </div>\
                <div class ="modal-footer">\
                    <button type="button" class ="btn btn-primary" id="digimango_messageBoxOkButton">OK</button>\
                    <button type="button" class ="btn btn-default" data-dismiss="modal" id="digimango_messageBoxCancelButton">Cancel</button>\
                </div>\
            </div>\
        </div>\
    </div>';


// See the comment inside function digimango_onOkClick(event) {
var digimango_numOfDialogsOpened = 0;


function messageBox(msg, significance, options, actionConfirmedCallback) {
    if ($('#digimango_MessageBoxContainer').length == 0) {
        var iDiv = document.createElement('div');
        iDiv.id = 'digimango_MessageBoxContainer';
        document.getElementsByTagName('body')[0].appendChild(iDiv);
        $("#digimango_MessageBoxContainer").html(dialogTemplate);
    }

    var okButtonName, cancelButtonName, showTextBox, textBoxDefaultText;

    if (options == null) {
        okButtonName = 'OK';
        cancelButtonName = null;
        showTextBox = null;
        textBoxDefaultText = null;
    } else {
        okButtonName = options.okButtonName;
        cancelButtonName = options.cancelButtonName;
        showTextBox = options.showTextBox;
        textBoxDefaultText = options.textBoxDefaultText;
    }

    if (showTextBox == true) {
        if (textBoxDefaultText == null)
            $('#digimango_messageBoxTextArea').val('');
        else
            $('#digimango_messageBoxTextArea').val(textBoxDefaultText);

        $('#digimango_messageBoxTextArea').show();
    }
    else
        $('#digimango_messageBoxTextArea').hide();

    if (okButtonName != null)
        $('#digimango_messageBoxOkButton').html(okButtonName);
    else
        $('#digimango_messageBoxOkButton').html('OK');

    if (cancelButtonName == null)
        $('#digimango_messageBoxCancelButton').hide();
    else {
        $('#digimango_messageBoxCancelButton').show();
        $('#digimango_messageBoxCancelButton').html(cancelButtonName);
    }

    $('#digimango_messageBoxOkButton').unbind('click');
    $('#digimango_messageBoxOkButton').on('click', { callback: actionConfirmedCallback }, digimango_onOkClick);

    $('#digimango_messageBoxCancelButton').unbind('click');
    $('#digimango_messageBoxCancelButton').on('click', digimango_onCancelClick);

    var content = $("#digimango_messageBoxMessage");

    if (significance == 'error')
        content.attr('class', 'text-danger');
    else if (significance == 'warning')
        content.attr('class', 'text-warning');
    else
        content.attr('class', 'text-success');

    content.html(msg);

    if (digimango_numOfDialogsOpened == 0)
        $("#digimango_messageBox").modal();

    digimango_numOfDialogsOpened++;
}

function digimango_onOkClick(event) {
    // JavaScript's nature is unblocking. So the function call in the following line will not block,
    // thus the last line of this function, which is to hide the dialog, is executed before user
    // clicks the "OK" button on the second dialog shown in the callback. Therefore we need to count
    // how many dialogs is currently showing. If we know there is still a dialog being shown, we do
    // not execute the last line in this function.
    if (typeof (event.data.callback) != 'undefined')
        event.data.callback($('#digimango_messageBoxTextArea').val());

    digimango_numOfDialogsOpened--;

    if (digimango_numOfDialogsOpened == 0)
        $('#digimango_messageBox').modal('hide');
}

function digimango_onCancelClick() {
    digimango_numOfDialogsOpened--;

    if (digimango_numOfDialogsOpened == 0)
        $('#digimango_messageBox').modal('hide');
}

Digimango.messagebox.js का उपयोग करने के लिए :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>A useful generic message box</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

    <link rel="stylesheet" type="text/css" href="~/Content/bootstrap.min.css" media="screen" />
    <script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="~/Scripts/bootstrap.js" type="text/javascript"></script>
    <script src="~/Scripts/bootbox.js" type="text/javascript"></script>

    <script src="~/Scripts/digimango.messagebox.js" type="text/javascript"></script>


    <script type="text/javascript">
        function testAlert() {
            messageBox('Something went wrong!', 'error');
        }

        function testAlertWithCallback() {
            messageBox('Something went wrong!', 'error', null, function () {
                messageBox('OK clicked.');
            });
        }

        function testConfirm() {
            messageBox('Do you want to proceed?', 'warning', { okButtonName: 'Yes', cancelButtonName: 'No' }, function () {
                messageBox('Are you sure you want to proceed?', 'warning', { okButtonName: 'Yes', cancelButtonName: 'No' });
            });
        }

        function testPrompt() {
            messageBox('How do you feel now?', 'normal', { showTextBox: true }, function (userInput) {
                messageBox('User entered "' + userInput + '".');
            });
        }

        function testPromptWithDefault() {
            messageBox('How do you feel now?', 'normal', { showTextBox: true, textBoxDefaultText: 'I am good!' }, function (userInput) {
                messageBox('User entered "' + userInput + '".');
            });
        }

    </script>
</head>

<body>
    <a href="#" onclick="testAlert();">Test alert</a> <br/>
    <a href="#" onclick="testAlertWithCallback();">Test alert with callback</a> <br />
    <a href="#" onclick="testConfirm();">Test confirm</a> <br/>
    <a href="#" onclick="testPrompt();">Test prompt</a><br />
    <a href="#" onclick="testPromptWithDefault();">Test prompt with default text</a> <br />
</body>

</html>
यहां छवि विवरण दर्ज करें


1

आप कॉलबैक फ़ंक्शन के साथ अधिक पुन: प्रयोज्य मेरे समाधान की कोशिश कर सकते हैं । इस फ़ंक्शन में आप POST अनुरोध या कुछ तर्क का उपयोग कर सकते हैं। प्रयुक्त लाइब्रेरी: JQuery 3> और बूटस्ट्रैप 3>

https://jsfiddle.net/axnikitenko/gazbyv8v/

परीक्षण के लिए Html कोड:

...
<body>
    <a href='#' id="remove-btn-a-id" class="btn btn-default">Test Remove Action</a>
</body>
...

जावास्क्रिप्ट:

$(function () {
    function remove() {
        alert('Remove Action Start!');
    }
    // Example of initializing component data
    this.cmpModalRemove = new ModalConfirmationComponent('remove-data', remove,
        'remove-btn-a-id', {
            txtModalHeader: 'Header Text For Remove', txtModalBody: 'Body For Text Remove',
            txtBtnConfirm: 'Confirm', txtBtnCancel: 'Cancel'
        });
    this.cmpModalRemove.initialize();
});

//----------------------------------------------------------------------------------------------------------------------
// COMPONENT SCRIPT
//----------------------------------------------------------------------------------------------------------------------
/**
 * Script processing data for confirmation dialog.
 * Used libraries: JQuery 3> and Bootstrap 3>.
 *
 * @param name unique component name at page scope
 * @param callback function which processing confirm click
 * @param actionBtnId button for open modal dialog
 * @param text localization data, structure:
 *              > txtModalHeader - text at header of modal dialog
 *              > txtModalBody - text at body of modal dialog
 *              > txtBtnConfirm - text at button for confirm action
 *              > txtBtnCancel - text at button for cancel action
 *
 * @constructor
 * @author Aleksey Nikitenko
 */
function ModalConfirmationComponent(name, callback, actionBtnId, text) {
    this.name = name;
    this.callback = callback;
    // Text data
    this.txtModalHeader =   text.txtModalHeader;
    this.txtModalBody =     text.txtModalBody;
    this.txtBtnConfirm =    text.txtBtnConfirm;
    this.txtBtnCancel =     text.txtBtnCancel;
    // Elements
    this.elmActionBtn = $('#' + actionBtnId);
    this.elmModalDiv = undefined;
    this.elmConfirmBtn = undefined;
}

/**
 * Initialize needed data for current component object.
 * Generate html code and assign actions for needed UI
 * elements.
 */
ModalConfirmationComponent.prototype.initialize = function () {
    // Generate modal html and assign with action button
    $('body').append(this.getHtmlModal());
    this.elmActionBtn.attr('data-toggle', 'modal');
    this.elmActionBtn.attr('data-target', '#'+this.getModalDivId());
    // Initialize needed elements
    this.elmModalDiv =  $('#'+this.getModalDivId());
    this.elmConfirmBtn = $('#'+this.getConfirmBtnId());
    // Assign click function for confirm button
    var object = this;
    this.elmConfirmBtn.click(function() {
        object.elmModalDiv.modal('toggle'); // hide modal
        object.callback(); // run callback function
    });
};

//----------------------------------------------------------------------------------------------------------------------
// HTML GENERATORS
//----------------------------------------------------------------------------------------------------------------------
/**
 * Methods needed for get html code of modal div.
 *
 * @returns {string} html code
 */
ModalConfirmationComponent.prototype.getHtmlModal = function () {
    var result = '<div class="modal fade" id="' + this.getModalDivId() + '"';
    result +=' tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">';
    result += '<div class="modal-dialog"><div class="modal-content"><div class="modal-header">';
    result += this.txtModalHeader + '</div><div class="modal-body">' + this.txtModalBody + '</div>';
    result += '<div class="modal-footer">';
    result += '<button type="button" class="btn btn-default" data-dismiss="modal">';
    result += this.txtBtnCancel + '</button>';
    result += '<button id="'+this.getConfirmBtnId()+'" type="button" class="btn btn-danger">';
    result += this.txtBtnConfirm + '</button>';
    return result+'</div></div></div></div>';
};

//----------------------------------------------------------------------------------------------------------------------
// UTILITY
//----------------------------------------------------------------------------------------------------------------------
/**
 * Get id element with name prefix for this component.
 *
 * @returns {string} element id
 */
ModalConfirmationComponent.prototype.getModalDivId = function () {
    return this.name + '-modal-id';
};

/**
 * Get id element with name prefix for this component.
 *
 * @returns {string} element id
 */
ModalConfirmationComponent.prototype.getConfirmBtnId = function () {
    return this.name + '-confirm-btn-id';
};

0

जब एक प्रासंगिक रूप से बड़ी परियोजना की बात आती है, तो हमें कुछ फिर से उपयोग करने की आवश्यकता हो सकती है । यह कुछ ऐसा है जो मैं एसओ की मदद से आया था।

confirmDelete.jsp

<!-- Modal Dialog -->
<div class="modal fade" id="confirmDelete" role="dialog" aria-labelledby="confirmDeleteLabel"
 aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"
                    aria-hidden="true">&times;</button>
            <h4 class="modal-title">Delete Parmanently</h4>
        </div>
        <div class="modal-body" style="height: 75px">
            <p>Are you sure about this ?</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            <button type="button" class="btn btn-danger" id="confirm-delete-ok">Ok
            </button>
        </div>
    </div>
</div>

<script type="text/javascript">

    var url_for_deletion = "#";
    var success_redirect = window.location.href;

$('#confirmDelete').on('show.bs.modal', function (e) {
    var message = $(e.relatedTarget).attr('data-message');
    $(this).find('.modal-body p').text(message);
    var title = $(e.relatedTarget).attr('data-title');
    $(this).find('.modal-title').text(title);

    if (typeof  $(e.relatedTarget).attr('data-url') != 'undefined') {
        url_for_deletion = $(e.relatedTarget).attr('data-url');
    }
    if (typeof  $(e.relatedTarget).attr('data-success-url') != 'undefined') {
        success_redirect = $(e.relatedTarget).attr('data-success-url');
    }

});

<!-- Form confirm (yes/ok) handler, submits form -->
$('#confirmDelete').find('.modal-footer #confirm-delete-ok').on('click', function () {
    $.ajax({
        method: "delete",
        url: url_for_deletion,
    }).success(function (data) {
        window.location.href = success_redirect;
    }).fail(function (error) {
        console.log(error);
    });
    $('#confirmDelete').modal('hide');
    return false;
});
<script/>

reusingPage.jsp

<a href="#" class="table-link danger"
data-toggle="modal"
data-target="#confirmDelete"
data-title="Delete Something"
data-message="Are you sure you want to inactivate this something?"
data-url="client/32"
id="delete-client-32">
</a>
<!-- jQuery should come before this -->
<%@ include file="../some/path/confirmDelete.jsp" %>

नोट: यह डिलीट रिक्वेस्ट के लिए http डिलीट मेथड का उपयोग करता है, आप इसे जावास्क्रिप्ट से बदल सकते हैं या, किसी भी अनुरोध का समर्थन करने के लिए डेटा-शीर्षक या डेटा-यूआरएल आदि में डेटा-विशेषता का उपयोग करके भेज सकते हैं।


0

यदि आप इसे सबसे आसान शॉर्टकट में करना चाहते हैं, तो आप इसे इस प्लगइन के साथ कर सकते हैं ।


लेकिन यह प्लगइन बूटस्ट्रैप मोडल का उपयोग करके एक वैकल्पिक कार्यान्वयन है । और असली बूटस्ट्रैप कार्यान्वयन भी बहुत आसान है, इसलिए मुझे इस प्लगइन का उपयोग करना पसंद नहीं है क्योंकि यह पृष्ठ में अतिरिक्त जेएस सामग्री जोड़ता है, जो पृष्ठ लोडिंग समय को धीमा कर देगा।


विचार

मैं इसे इस तरह से खुद से लागू करना पसंद करता हूं-

  1. यदि उपयोगकर्ता सूची से किसी आइटम को हटाने के लिए एक बटन पर क्लिक करता है, तो एक जेएस कॉल मोडल के रूप में आइटम आईडी (या किसी भी अधिक आवश्यक डेटा) को डाल देगा ।
  2. फिर पॉप-अप में, पुष्टि के लिए 2 बटन होंगे।

    • हां फॉर्म सबमिट करेंगे (ajax या डायरेक्ट फॉर्म सबमिट के साथ)
    • नहीं , बस मोडल को खारिज कर देगा

कोड इस तरह होगा ( बूटस्ट्रैप का उपयोग करके ) -

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script>
$(document).ready(function()
{
    $("button").click(function()
    {
        //Say - $('p').get(0).id - this delete item id
        $("#delete_item_id").val( $('p').get(0).id );
        $('#delete_confirmation_modal').modal('show');
    });
});
</script>

<p id="1">This is a item to delete.</p>

<button type="button" class="btn btn-danger">Delete</button>

<!-- Delete Modal content-->

<div class="modal fade" id="delete_confirmation_modal" role="dialog" style="display: none;">
	<div class="modal-dialog" style="margin-top: 260.5px;">
				<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal">×</button>
				<h4 class="modal-title">Do you really want to delete this Category?</h4>
			</div>
			<form role="form" method="post" action="category_delete" id="delete_data">
				<input type="hidden" id="delete_item_id" name="id" value="12">
				<div class="modal-footer">
					<button type="submit" class="btn btn-danger">Yes</button>
					<button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
				</div>
			</form>
		</div>

	</div>
</div>

आपको अपनी आवश्यकता के अनुसार फॉर्म एक्शन को बदलना चाहिए।

हैप्पी कोडिंग :)


0

पोस्ट पृष्ठ और पुन: प्रयोज्य ब्लेड फ़ाइल को नेविगेशन के साथ पोस्ट पकाने की विधि

dfsq का जवाब बहुत अच्छा है। मैंने अपनी आवश्यकताओं को पूरा करने के लिए इसे थोड़ा संशोधित किया: मुझे वास्तव में इस मामले के लिए एक मॉडल की आवश्यकता थी, जिसे क्लिक करने के बाद, उपयोगकर्ता को संबंधित पृष्ठ पर भी नेविगेट किया जाएगा। असमान रूप से क्वेरी को निष्पादित करना हमेशा वह नहीं होता है जिसकी किसी को आवश्यकता होती है।

उपयोग करके Bladeमैंने फ़ाइल बनाई resources/views/includes/confirmation_modal.blade.php:

<div class="modal fade" id="confirmation-modal" tabindex="-1" role="dialog" aria-labelledby="confirmation-modal-label" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4>{{ $headerText }}</h4>
            </div>
            <div class="modal-body">
                {{ $bodyText }}
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <form action="" method="POST" style="display:inline">
                    {{ method_field($verb) }}
                    {{ csrf_field() }}
                    <input type="submit" class="btn btn-danger btn-ok" value="{{ $confirmButtonText }}" />
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    $('#confirmation-modal').on('show.bs.modal', function(e) {
        href = $(e.relatedTarget).data('href');

        // change href of button to corresponding target
        $(this).find('form').attr('action', href);
    });
</script>

अब, इसका उपयोग करना सीधा है:

<a data-href="{{ route('data.destroy', ['id' => $data->id]) }}" data-toggle="modal" data-target="#confirmation-modal" class="btn btn-sm btn-danger">Remove</a>

यहाँ बहुत कुछ नहीं बदला गया और मोडल को इस तरह शामिल किया जा सकता है:

@include('includes.confirmation_modal', ['headerText' => 'Delete Data?', 'bodyText' => 'Do you really want to delete these data? This operation is irreversible.',
'confirmButtonText' => 'Remove Data', 'verb' => 'DELETE'])

बस क्रिया को वहाँ लगाकर, उसका उपयोग करता है। इस तरह, CSRF का भी उपयोग किया जाता है।

मेरी मदद की, शायद यह किसी और की मदद करे।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.