Datatables - adding data via array loop how to change records displayed?

0 votes
asked Mar 14, 2017 by kingtreelo

So i am bringing back and array from an ajax call and then looping over it to add records to my datatable:

for (i = 0; i < data.length; i++) {
jobDetails.push(data[i]);
$('#JobTable').dataTable().fnAddData([
                    data[i].Id,
                    data[i].Title,
                    data[i].Frequency,
                    data[i].Priority,
                    data[i].DateDue,
                    data[i].AssignedToName
                    ]);
}

As i am adding data to the table in this way, is there a way for to me to display more than the standard set of records?

I have tried initializing the DB and adding custom values such as "pageLength": 50, but when i am trying to pass my data into the datatable, it is trying to reinitialize it(which obviously does not work)

The reason for passing data into the DT this way, is that it is using a viewmodel that has a lot more values(used elsewhere, see jobDetails array that is being populated) so i can define exactly what i need to display in a much simpler, neater way.

I have actually figured out how to do this now using a slightly different way of pushing my Array into my datatable:

$('#JobTable').DataTable({
        "pageLength": 50,
        "aaSorting": [3, 'asc'],
        "ajax": {
            "url": "/Home/GetJobs",
            "dataSrc": function (data) {
                var returnData = new Array();
                for (i = 0; i < data.length ; i++) {
                    jobDetails.push(data[i]);
                    var dateDue = moment(data[i].NextDue).format('DD-MMM-YYYY');
                   // var formattedDate = moment().format('DD-MMM-YYYY');
                    returnData.push({
                        "Id": data[i].Id,
                        "Title": data[i].Title,
                        "Frequency": data[i].Frequency,
                        "Priority": data[i].Priority,
                        "NextDue": dateDue,
                        "AssignedToName": data[i].AssignedToName
                    });
                }
                return returnData;
            }
        },
        "columns": [
            { "data": "Id" },
            { "data": "Title" },
            { "data": "Frequency" },
            { "data": "Priority" },
            { "data": "NextDue" },
            { "data": "AssignedToName" }
        ]
    });

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...