Plexis Plexis
Plexis Plexis

Reputation: 302

How to refresh a div after hiding contents

I have the following code:

// I am filling the data of the "MultiSelectDialog_List" div using javascript like this:

var s = ''; // JSON string ----- YOU NEED TO ADD AN EXAMPLE OF S
var jsonData = JSON.parse(s);
for (var i = 0; i < jsonData.length; i++) {

  // Hold the original list

  $("#MultiSelectDialog_List").append("<input type='checkbox'  id ='" + jsonData[i][idProp] + "'  value='" +
    jsonData[i][idProp] + "' data-value='" + jsonData[i][nameProp].toLowerCase() + "' > <label data-value ='" +
    jsonData[i][nameProp].toLowerCase() + "'  id ='lbl" + jsonData[i][idProp] + "'>" + jsonData[i][nameProp] + "</label> <br/>");
}



// Then I am using this script to hide some content:

var enteredText = $("#MultiSelectDialog_Search").val();
var ary = $("input[type='checkbox']:not([data-value*='" + enteredText.toLowerCase() + "'])");

for (var i = 0; i < ary.length; i++) {
  $("#" + ary[i]["id"]).hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="multiSelectDialog" title="Select">
  <input type="text" id="MultiSelectDialog_Search" name="MultiSelectDialog_Search" onchange="multiSelectDialog_Search_TextChanged()" />
  <div id="MultiSelectDialog_List" data-value="">
    <!--The data will goes here-->
  </div>
  <input type="hidden" name="MultiSelectDialog_Values" id="MultiSelectDialog_Values" />
  <button id="MultiSelectDialog_Submit" onclick="multiSelectDialog_SubmitButton_Click()">Submit</button>
</div>

The problem is that the content of the div are not redrawen correctly after hiding some elements. Check the image:

Before hiding elements

When hiding elements, see the gaps

How should I refresh the div? I tried to use hide() and show(), fadein(). but did not work.

Upvotes: 3

Views: 431

Answers (2)

Mandar Dhadve
Mandar Dhadve

Reputation: 381

Try something like this

$(document).ready(function(){
    // use localStorage.removeItem('show'); to unset an item
    var show = localStorage.getItem('show');
    if(show === 'true'){
        $('#MultiSelectDialog_List').show();
    }
    
    $("#btn").click(function(event){
        event.preventDefault();
        $('#MultiSelectDialog_List').show();
        localStorage.setItem('show', 'true');
    });
});

Upvotes: 1

flx
flx

Reputation: 1578

// I am filling the data of the "MultiSelectDialog_List" div using javascript like this:

var s = '[{"EnName":"Device Linked Officer","ArName":"Device Linked Officer","Status":false,"Project":"CMS","ID":"ROL29","IsOrderNumRequired":true,"IsTimeInterval":false,"IsHealthCare":true,"isBiller":false},{"EnName":"pharmacist","ArName":"pharmacist","Status":false,"Project":"CMS","ID":"ROL30","IsOrderNumRequired":true,"IsTimeInterval":false,"IsHealthCare":true,"isBiller":false},{"EnName":"Store & Pharmacy","ArName":"Store & Pharmacy","Status":false,"Project":"CMS"}]'; // JSON string ----- YOU NEED TO ADD AN EXAMPLE OF S
var jsonData = JSON.parse(s);
var idProp = 'ID';
var nameProp = 'EnName';

for (var i = 0; i < jsonData.length; i++) {
  // Hold the original list
  $("#MultiSelectDialog_List").append("<input type='checkbox'  id ='" + jsonData[i][idProp] + "'  value='" +
    jsonData[i][idProp] + "' data-value='" + jsonData[i][nameProp].toLowerCase() + "' > <label data-value ='" +
    jsonData[i][nameProp].toLowerCase() + "'  id ='lbl" + jsonData[i][idProp] + "'>" + jsonData[i][nameProp] + "</label> <br/>");
}



// Then I am using this script to hide some content:

function multiSelectDialog_SubmitButton_Click() {
  var enteredText = $("#MultiSelectDialog_Search").val();
  var ary = $("input[type='checkbox']:not([data-value*='" + enteredText.toLowerCase() + "'])");

  for (var i = 0; i < ary.length; i++) {
    $("#" + ary[i]["id"]).hide();
  }
}

function multiSelectDialog_Search_TextChanged() {
  // TODO
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="multiSelectDialog" title="Select">
  <input type="text" id="MultiSelectDialog_Search" name="MultiSelectDialog_Search" onchange="multiSelectDialog_Search_TextChanged()" />
  <div id="MultiSelectDialog_List" data-value="">
    <!--The data will goes here-->
  </div>
  <input type="hidden" name="MultiSelectDialog_Values" id="MultiSelectDialog_Values" />
  <button id="MultiSelectDialog_Submit" onclick="multiSelectDialog_SubmitButton_Click()">Submit</button>
</div>

I've updated the given snippet a little bit to try to achieve a working code.

Your problem is, that its not really clear what idProp or nameProp is (so I decided on my own).

Another problem you will face:

You do hide stuff, but you never get it back. Once a checkbox got hidden its gone for good.

Upvotes: 1

Related Questions