1

I am using the ParamQuery JavaScript library (ParamQuery Website) to build a JavaScript grid.

With JQuery I make a request to my service using Ajax which retunrs JSON data. I then loop through my JSON data and assign it to an array. My problem is the elements are strings but I think they need to be an array of objects.

<script type="text/javascript">
    $(document).ready(function () {
        var DataArray = [];

        $.ajax({
            url: "http://wks52025:82/WcfDataService.svc/GetNotes()?$format=json",
            type: "get",
            datatype: "json",
            success: function (data) {
                $.each(data.d, function (i, item) {
                    DataArray[i] = "[" + item.NotesTitle.trim() + "," + item.NotesText.trim() + "]";
                })

                for (var i = 0; i < DataArray.length; i++) {
                    //alert(DataArray[i]);
                    //Do something
                }

                // GRID LOGIC HERE
                var obj = { width: 800, height: 400, title: "Notes" };
                obj.colModel = [
                    { title: "NotesTitle", width: 500, dataType: "string" },
                    { title: "NotesText", width: 500, dataType: "string" } 
                ];
                obj.dataModel = { data: DataArray }; // Data Array goes here
                $("#grid_array").pqGrid(obj);
            }
        });    
    });
</script>

I have attached screenshots of my Watch tab in VS and a screenshot of how the grid renders after debug.

Visual studio watch

My Grid

This is the example code

Can anyone help me solve this problem?

1
  • If the screenshot is too small I can resize it. It looked bigger on paint. Commented May 14, 2013 at 8:24

1 Answer 1

1

If the question is only about turning this to array of objects take a look at this line:

$.each(data.d, function (i, item) {
  DataArray[i] = { NotesTitle: item.NotesTitle.trim(), NotesText:item.NotesText.trim() };
})

Update Had a look a the example, you need an Array of arrays

$.each(data.d, function (i, item) {
  DataArray[i] = [  item.NotesTitle.trim(), item.NotesText.trim() ];
})
Sign up to request clarification or add additional context in comments.

2 Comments

For this question it is. Do you have a suggestion?
Thanks that worked. I guess my use of brackets was incorrect for what I was trying to do.

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.