Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>GET recipe</h2> <p>For this task you can use already available plugins and bootstrap extensions. Or you can make your own confirmation popup with just <strong>3</strong> lines of code. Check it out.</p> <p>Say we have this links (note <code>data-href</code> instead of <code>href</code>) or buttons that we want to have delete confirmation for:</p> <pre><code>&lt;a href="#" data-href="delete.php?id=23" data-toggle="modal" data-target="#confirm-delete"&gt;Delete record #23&lt;/a&gt; &lt;button class="btn btn-default" data-href="/delete.php?id=54" data-toggle="modal" data-target="#confirm-delete"&gt; Delete record #54 &lt;/button&gt; </code></pre> <p>Here <code>#confirm-delete</code> points to a modal popup div in your HTML. It should have an "OK" button configured like this:</p> <pre><code>&lt;div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt; &lt;div class="modal-dialog"&gt; &lt;div class="modal-content"&gt; &lt;div class="modal-header"&gt; ... &lt;/div&gt; &lt;div class="modal-body"&gt; ... &lt;/div&gt; &lt;div class="modal-footer"&gt; &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Cancel&lt;/button&gt; &lt;a class="btn btn-danger btn-ok"&gt;Delete&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Now you only need this little javascript to make a delete action confirmable:</p> <pre><code>$('#confirm-delete').on('show.bs.modal', function(e) { $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href')); }); </code></pre> <p>So on <code>show.bs.modal</code> event delete button <code>href</code> is set to URL with corresponding record id.</p> <p><strong>Demo:</strong> <a href="http://plnkr.co/edit/NePR0BQf3VmKtuMmhVR7?p=preview" rel="noreferrer">http://plnkr.co/edit/NePR0BQf3VmKtuMmhVR7?p=preview</a></p> <hr> <h2>POST recipe</h2> <p>I realize that in some cases there might be needed to perform POST or DELETE request rather then GET. It it still pretty simple without too much code. Take a look at the demo below with this approach:</p> <pre><code>// 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); }); </code></pre> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>// 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'); setTimeout(function() { $modalDiv.modal('hide').removeClass('loading'); }, 1000); // In reality would be something like this // $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); });</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>.modal.loading .modal-content:before { content: 'Loading...'; text-align: center; line-height: 155px; font-size: 20px; background: rgba(0, 0, 0, .8); position: absolute; top: 55px; bottom: 0; left: 0; right: 0; color: #EEE; z-index: 1000; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script data-require="jquery@*" data-semver="2.0.3" src="//code.jquery.com/jquery-2.0.3.min.js"&gt;&lt;/script&gt; &lt;script data-require="bootstrap@*" data-semver="3.1.1" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"&gt;&lt;/script&gt; &lt;link data-require="bootstrap-css@3.1.1" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" /&gt; &lt;div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt; &lt;div class="modal-dialog"&gt; &lt;div class="modal-content"&gt; &lt;div class="modal-header"&gt; &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;×&lt;/button&gt; &lt;h4 class="modal-title" id="myModalLabel"&gt;Confirm Delete&lt;/h4&gt; &lt;/div&gt; &lt;div class="modal-body"&gt; &lt;p&gt;You are about to delete &lt;b&gt;&lt;i class="title"&gt;&lt;/i&gt;&lt;/b&gt; record, this procedure is irreversible.&lt;/p&gt; &lt;p&gt;Do you want to proceed?&lt;/p&gt; &lt;/div&gt; &lt;div class="modal-footer"&gt; &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Cancel&lt;/button&gt; &lt;button type="button" class="btn btn-danger btn-ok"&gt;Delete&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;a href="#" data-record-id="23" data-record-title="The first one" data-toggle="modal" data-target="#confirm-delete"&gt; Delete "The first one", #23 &lt;/a&gt; &lt;br /&gt; &lt;button class="btn btn-default" data-record-id="54" data-record-title="Something cool" data-toggle="modal" data-target="#confirm-delete"&gt; Delete "Something cool", #54 &lt;/button&gt;</code></pre> </div> </div> </p> <p><strong>Demo:</strong> <a href="http://plnkr.co/edit/V4GUuSueuuxiGr4L9LmG?p=preview" rel="noreferrer">http://plnkr.co/edit/V4GUuSueuuxiGr4L9LmG?p=preview</a></p> <hr> <h2>Bootstrap 2.3</h2> <p>Here is an original version of the code I made when I was answering this question for Bootstrap 2.3 modal.</p> <pre><code>$('#modal').on('show', function() { var id = $(this).data('id'), removeBtn = $(this).find('.danger'); removeBtn.attr('href', removeBtn.attr('href').replace(/(&amp;|\?)ref=\d*/, '$1ref=' + id)); }); </code></pre> <p><strong>Demo</strong>: <a href="http://jsfiddle.net/MjmVr/1595/" rel="noreferrer">http://jsfiddle.net/MjmVr/1595/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload