Accessing SharePoint list with JavaScript Variable Storage -
i started yesterday javascript , i'm trying information sharepoint 2010 list , want print table. following code in body tag:
<script type="text/javascript"> executeordelayuntilscriptloaded(mainfunction, "sp.js"); var typenumber = "1520"; var statusarray = new array(3); try { function mainfunction() { var clientcontext = new sp.clientcontext.get_current(); var olist = clientcontext.get_web().get_lists().getbytitle('projektstatus'); //empty query string cause there problems lookup field var emptycamlquery = new sp.camlquery(); this.colllistitem = olist.getitems(emptycamlquery); clientcontext.load(colllistitem); clientcontext.executequeryasync(function.createdelegate(this, this.onquerysucceeded), function.createdelegate(this, this.onqueryfailed)); document.write(statusarray[2]); } function onquerysucceeded(sender, args) { var listiteminfo = ''; var = 0; var listitemenumerator = colllistitem.getenumerator(); while (listitemenumerator.movenext()) { var olistitem = listitemenumerator.get_current(); if (olistitem.get_item('title').match(typenumber)) { statusarray[i] = olistitem.get_item('kpi_status'); //works fine alert("nummer:" + + statusarray[i]); i++; } } } function onqueryfailed(sender, args) { alert('request failed. ' + args.get_message() + '\n' + args.get_stacktrace()); } var tableoutput = "<table><tr><th>indikatortyp</th><th>kpi-status</th></tr>" + "<tr><td>kosten</td><td>" + statusarray[2] + "</td></tr>" + "<tr><td>technik</td><td>" + statusarray[1] + "</td></tr>" + "<tr><td>termin</td><td>" + statusarray[0] + "</td></tr>" + "</table>" document.write(tableoutput); } catch (e) { alert(e); } </script>
the output alert works fine when want print statusarray in table doesn't work cause variable undefined. think problem createdelegate().
the problem because use global variable statusarray , code create table outside onquerysucceeded.
my suggestions are: - encapsulate statusarray variable - move creation of table inside onquerysucceeded
Comments
Post a Comment