My code not working dataSrc Please any body help me.

My code not working dataSrc Please any body help me.

sagar-rajsagar-raj Posts: 2Questions: 1Answers: 0

//ajax-call:

$(document).ready(function() {
$('#example').DataTable({
ajax: 'api/users',
dataSrc: 'data.table',
columns: [
{ data: 'id' },
{ data: 'firstName' },
{ data: 'lastName' },
],
responsive: true
});
});

//api= response:

[{"id":1,"firstName":"harbhajan","lastName":"sing"},{"id":2,"firstName":"akon","lastName":"bkon"},{"id":3,"firstName":"david","lastName":"warner"},{"id":4,"firstName":"rahul","lastName":"dravid"},{"id":5,"firstName":"sachin","lastName":"tendulkar"}]

//html table:

id firstName lastName
id firstName lastName

Answers

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    Looks like two issues. First the ajax.dataSrc is to combined with the ajax option. The second is dataSrc: 'data.table', is not correct since your data is not within an object. Take a look at the second example in the ajax.dataSrc docs for how you should configure this.

    Kevin

  • sagar-rajsagar-raj Posts: 2Questions: 1Answers: 0
    edited June 2020

    Thanks kthorngren for quick response. i got the answer.
    Solution:

        $('#example').DataTable({
                ajax:{
                    "url":"api/users",
                    "dataSrc": "",
                    },
    
                columns: [
                        { data: 'id' },
                        { data: 'firstName' },
                        { data: 'lastName' },
                ]     });
    

    Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This discussion has been closed.