jQuery dialog: How to remove added but unsaved items on close

89 views Asked by At

I have a jQuery Dialog popup in which I can review and add items to the existing content. The new content is being saved as soon as I click the SAVE-Button.

<div id="container">
  <ul>
    <li>Elem 1</li>
    <li>Elem 2</li>
  </ul>

  <button id="btnAdd">ADD</button>
  <button id="btnSave" value="submit">SAVE</button>
</div>

<button id="openModal">Open dialog</button>

---------------------------------------------

$(function() {
    $( "#container" ).dialog();
});

$('#openModal').click(function() {
    $( "#container" ).dialog();
});

$('#btnAdd').click(function() {
    $( "#container ul" ).append( "<li>NEW Elem</li>" );
});

$('#btnSave').click(function() { /* ignore */ });

My problem now is, that the content does not get kind of setback when the user clicks the "X" (close) in the titlebar. I want to get back to the original state when the dialog was first opened and before the new items have been added.

How can this be resolved?

Fiddle-Demo: https://jsfiddle.net/SchweizerSchoggi/vsd1qpLg/

EDIT: I've updated the sample. This is just simplified. I am not able to add a class or Id to the new li's.

3

There are 3 answers

4
Rahul Agrawal On BEST ANSWER

Just add this function in your jquery code

var numlist = $('li').length;

$( "#container" ).on( "dialogclose",function(event,ui){
     var count = 1;       
     $('li').each(function(){
       if(count>numlist)
       {
           $(this).remove();
       }
       count++;
     }); 
});

Or you can use this

var numlist = $('li').length;

$( "#container" ).on( "dialogclose",function(event,ui){
     var finallist = $('li').length;   
     $('li').slice( numlist,finallist).remove();
});
2
gaetanoM On

Handle the beforeClose event:

$(function () {
  // save the dialog original content
  var innerHtml = $("#container").html();
  $("#container").dialog({
    autoOpen: false,
    beforeClose: function(event, ui) {
      // restore the dialog original content
      $("#container").html(innerHtml);
    }
  });

  $('#openModal').click(function(e) {
    $( "#container" ).dialog('open');
  });
});

// the event delegation for buttons inside the dialog change in:
$(document).on('click', '#btnAdd', function() {
  $( "#container ul" ).append( "<li class='green'>NEW Elem</li>" );
});

$(document).on('click', '#btnSave', function() {
});
#container { padding: 10px; border: solid 1px red; margin-bottom: 30px; }
#container button { margin-top: 20px; }

#container ul li.green { color: green; }
<link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>


<div id="container">
    <ul>
        <li>Elem 1</li>
        <li>Elem 2</li>
    </ul>

    <button id="btnAdd">ADD</button>
    <button id="btnSave" value="submit">SAVE</button>
</div>

<button id="openModal">Open dialog</button>

0
John R On

Hope you need something like this.

Store the saved li's in separate global variable.

ulhtml = $("#container ul").html();

Append the saved li while opening the model.

$("#container ul").html(ulhtml);

FIDDLE DEMO

var ulhtml;
$(function () {
    ulhtml = $("#container ul").html();
    $("#container").dialog();
});
$('#openModal').click(function () {
    $("#container ul").html(ulhtml);
    $("#container").dialog();
});

$('#btnAdd').click(function () {
    $("#container ul").append("<li class='green'>NEW Elem</li>");
});

$('#btnSave').click(function () {
    ulhtml = $("#container ul").html();
});