0

I have created a code which fetches data using the Ajax Call and populate it to the data table UI.

My code is as below:

jQuery.ajax(
    {
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Events')/items?$select=Title,Session_x0020_Room,Session_x0020_Date,StartTime,EndTime,Duties,OnsiteContactEmail",
        type: "GET",
        headers: { "Accept": "application/json;odata=verbose" },
        dataType: "json",                
        success: function (data, textStatus, xhr) {
                if (data.d.results.length > 0) {
                    var resColl = data.d.results;
                    var strData= new Array();
                    for(var i=0; i < resColl.length; i++){
                        var arr = new Array();
                        arr.push(resColl[i].Title);
                        arr.push(resColl[i].Session_x0020_Room);
                        strData[i] = JSON.stringify(arr);
                    }
                    $('#example').DataTable({
                        data:strData,
                        columns: [
                            { title: "Event" },
                            { title: "Room" }
                        ]
                    });
                }                                                               
            },
            error: function (data, textStatus, xhr) {
                console.error("Error while getting the data from Events list");
            }
        }); 

In strData object I am getting this value: "["Asian Women in Computing","Room 1"]"

But in the table of HTML I am not getting proper output.

enter image description here

What am I missing?

5
  • You are missing docs read from Datatables datatables.net Commented Jul 27, 2017 at 10:06
  • 1
    What happens if you remove strData[i] = JSON.stringify(arr);? I'm assuming it's printing charactars because strData is a JSON string instead of an actual object it will just print a single character at that specific location. Commented Jul 27, 2017 at 10:07
  • Actually there will be multiple records. In resColl there will be more than one records that's why I am pushing values into it. Commented Jul 27, 2017 at 10:08
  • Are you talking about the required scripts like jquery and jquery.dataTables.min.js @tilz0R? Commented Jul 27, 2017 at 10:09
  • 1
    Okay but what happens if you don't convert arr to a JSON string like so strData[i] = arr; @MohemmadK Commented Jul 27, 2017 at 10:11

1 Answer 1

1

I'm assuming that DataTable expects an array of arrays that exists of strings. You have an array of JSON strings because you convert the array arr to a JSON string and push it to strData, which DataTable will later use.

                var resColl = data.d.results;
                var strData= new Array();
                for(var i=0; i < resColl.length; i++){
                    var arr = new Array();
                    arr.push(resColl[i].Title);
                    arr.push(resColl[i].Session_x0020_Room);
                    strData[i] = arr;
                }

Don't convert arr to a JSON string and it should print fine.

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

2 Comments

Thank you sir. It is working perfectly fine. One question, don't I require to convert the string into JSON string?
No, in this case you don't need to convert the array into a JSON string because the required data set needs an array of arrays that consists out of strings, not an array that consists out of JSON strings.

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.