javascript - How to expand/collapse (+/-) for a nested dynamic list using jquery -


i have tried toggle function think selecting proper list having problem.

var json ={"asia": [{"regionlist": [{"regionname": "eastern asia","countrylist": [{"countryname": "china","subcountrylist": [{"subcountryname": "southern china"},{"subcountryname": "eastern china"}]},{"countryname": "hong kong"}]},{"regionname": "southern asia","countrylist": [{"countryname": "india"},{"countryname": "pakistan"}]}]}]};  var html = '';  $.each(json, function(i1, object) {    html += '<li><input type="checkbox" /><b class=' + i1 + ' ><a name="' + i1 + '" >' + i1 + '</a></b>';      $.each(object, function(i2, continent) {  	html += '<ul>';      	$.each(continent.regionlist, function(i3, region) {  	    html += '<li><input type="checkbox" /><b>' + region.regionname + '</b>';  		  $.each(region.countrylist, function(i4, country) {  			html += '<ul><li class=' + country.countryname + '><input type="checkbox" />' + country.countryname;  			if (country.subcountrylist) {  			  $.each(country.subcountrylist, function(i5, subcountry) {  				html += '<ul><li><input type="checkbox" />' + subcountry.subcountryname + '</li></ul>';  			  });  				$("b." + country.countryname).toggle(function() {  					$(this).children('ul').slideup();  				},  				function() {  					$(this).children('ul').slidedown();  				});  				};  				html += '</li></ul>';  			  });  			  html += '</li>';  			});  			html += '</ul>';  		  });  		  html += '</li>';  		});    		$('#regioncontent ol').html(html);
li, ol{list-style:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div id="regioncontent">    <ol></ol>  </div>

i wanted add toggle function country name(china) has sub countries(southern china). on click of china should collapse sub countries list +/- sign , vice versa.

try


var json = {    "asia": [{      "regionlist": [{        "regionname": "eastern asia",        "countrylist": [{          "countryname": "china",          "subcountrylist": [{            "subcountryname": "southern china"          }, {            "subcountryname": "eastern china"          }]        }, {          "countryname": "hong kong"        }]      }, {        "regionname": "southern asia",        "countrylist": [{          "countryname": "india"        }, {          "countryname": "pakistan"        }]      }]    }]  };  var html = '';  $.each(json, function(i1, object) {    html += '<li><input type="checkbox" /><b class=' + i1 + ' ><a name="' + i1 + '" >' + i1 + '</a></b>';    $.each(object, function(i2, continent) {      html += '<ul>';      $.each(continent.regionlist, function(i3, region) {        html += '<li><input type="checkbox" /><b>' + region.regionname + '</b>';        $.each(region.countrylist, function(i4, country) {          html += '<ul><li class=' + country.countryname.replace(/\s/g, "-") + '><input type="checkbox" />' + country.countryname;                    if (country.hasownproperty("subcountrylist") && country.subcountrylist.length > 0) {          html += '<span class=' + country.countryname.replace(/\s/g, "-") + '>-</span>';            };                      $(document)            .on("click", "input ~ span." + country.countryname.replace(/\s/g, "-") + ":first", function(e) {              $(this).siblings("ul").is(":visible") ? $(this).siblings("ul").slideup({                start: function() {                  e.target.textcontent = "+"                }              }) : $(this).siblings("ul").slidedown({                start: function() {                  e.target.textcontent = "-"                }              })            });          if (country.subcountrylist) {            $.each(country.subcountrylist, function(i5, subcountry) {              html += '<ul><li><input type="checkbox" />' + subcountry.subcountryname + '</li></ul>';              });          };          html += '</li></ul>';        });        html += '</li>';      });      html += '</ul>';    });    html += '</li>';  });    $('#regioncontent ol').html(html);
li,  ol {    list-style: none;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div id="regioncontent">    <ol></ol>  </div>


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -