jquery - getting results back from json array within function -
i trying build re-usable function pulling json results , writing page
so far json , when returned run function passes data index , result. works fine
the problem when went iteration around result in function (seen below)
sometimes data structure data.usergroup.form , other times data.usergroup.user , on
what have tried pass name want use function last argument i.e. "user" , wihin inner .each call element.name (where name can vary) doesnt work. can help?
here code
$.when(promise).then(function(result) { $(result.data.usergroup).each(function(index, element) { var html = gethtml( ["name", "delete"], index, element, "user"); $("#accordion2").append(html); }); }) function gethtml(array, index, element, name) { var html = " <div class='accordion-group'>"; html = html + "<div class='accordion-heading'>"; html = html + "<span class='accordian-image'></span>" + element.name + "</a>"; var inner =""; $(element.name).each(function(i, result) { inner = inner + "<tr><td>" + result.name + "</td>" ; }); html = html + inner; return html; }
you need use bracket notation instead of dot notation member operator.
element[name]
ex
$.when(promise).then(function(result) { $(result.data.usergroup).each(function(index, element) { var html = gethtml( ["name", "delete"], index, element, "user"); $("#accordion2").append(html); }); }) function gethtml(array, index, element, name) { var html = " <div class='accordion-group'>"; html = html + "<div class='accordion-heading'>"; html = html + "<span class='accordian-image'></span>" + element[name] + "</a>"; var inner =""; $(element.name).each(function(i, result) { inner = inner + "<tr><td>" + result[name] + "</td>" ; }); html = html + inner; return html; }
Comments
Post a Comment