0

i started yesterday with javascript and i'm trying to get some information from a sharepoint 2010 list and i want print it into a table. the following code is in the 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 are some problems with a 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 i = 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:" + i + 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 with the alert works fine but when i want to print the statusArray in the table this doesn't work cause the variable is undefined. i think the problem could be something with createDelegate().

1 Answer 1

1

The problem is because you use a global variable statusArray and your code that create the table is outside the onQuerySucceeded.

My suggestions are: - encapsulate the statusArray variable - move the creation of the table inside the onQuerySucceeded

Sign up to request clarification or add additional context in comments.

6 Comments

i moved the initialisation of the statusarray, the tableoutput and the document.write(tableoutput) into the block of onQuerySucceeded. the alerts are working but now the table isn't printed out any more...
did you move the document.write function inside the onQuerySucceded too?
yep, i also tried it with document.getElementById("ID").innerHTML = tableOutput; but this didn't work too
is the value null? try to execute your code on a $(document).ready function
i think the problem is the which loop in the onSuccededQuery. so i build in a break and it works, but this is sick :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.