I trying to insert json collection in datatable using jQuery. This is my code:

I trying to insert json collection in datatable using jQuery. This is my code:

DzahiDzahi Posts: 6Questions: 2Answers: 0
                    function reloadseekers(vacancy){
                        if (vacancy) {
                            var seeker=[];
                            $.ajax({
                                url: '/getseekers/' + vacancy,
                                method: "GET",
                                dataType: "json",
                                cache: false,
                                success: function (data) {
                                    $.each(data.appSeekers, function (index) {
                                        seeker [index]={
                                            'ssn': data.appSeekers[index].ssn,
                                            'full name': data.appSeekers[index].first_name + ' ' + data.appSeekers[index].second_name + ' ' + 
                                                    data.appSeekers[index].middle_name + ' ' + data.appSeekers[index].last_name,
                                            'primary phone': data.appSeekers[index].phone_1,
                                            'secondary phone': data.appSeekers[index].phone_2,
                                            'Total evaluation': data.appSeekers[index].evaluation.total_evaluation,
                                            'Employment Notes': data.appSeekers[index].evaluation.employment_notes,
                                            'Referral count': data.appSeekers[index].redirection_Count,
                                            'communication': extractcommunication(data.appSeekers[index].communications)
                                        };
                                    });

                                        $('#evaluated_list_table').dataTable(
                                            {
                                            "paging": true,
                                            "lengthChange": true,
                                            "searching": true,
                                            "ordering": true,
                                            "info": true,
                                            "autoWidth": true,
                                            "data": seeker,
                                    });

The datatable stile empty!!!!
any help?

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Since you are using object data you will need to use columns.data to define the columns. I would eliminate the $.each() loop and use change the data option to "data": data.appSeekers. Then define columns.data using the original object property names.

    See the Data Sources docs for more details. See this example for ajax loaded objects.

    See this example for using columns.render to combining the name fields into one column.

    Kevin

  • DzahiDzahi Posts: 6Questions: 2Answers: 0
    edited March 2021

    Thanks Kevin for you answer. I modified the code as you answer:
    if (vacancy) {
    var seeker;
    $.ajax({
    url: '/getseekers/' + vacancy,
    method: "GET",
    dataType: "json",
    cache: false,
    success: function (data) {
    $('#evaluated_list_table').dataTable(
    {
    "paging": true,
    "lengthChange": true,
    "searching": true,
    "ordering": true,
    "info": true,
    "autoWidth": true,
    "data": data.appSeeker,
    "columns": [
    { "title": "SSN" },
    { "data": "ssn"},
    { "title": "First Name" },
    { "data": "first_name"},
    { "title": "Primary phone" },
    { "data": "phone_1"},
    { "title": "Secondary phone" },
    { "data": "phone_2"},
    { "title": "Total evaluation" },
    { "data": "total_evaluation"},
    { "title": "Employment notes" },
    { "data": "employment_notes"},
    { "title": "Referral count" },
    { "data": "referral_count"},
    ]
    });
    but i still have the the table with No data available in table!!!

  • DzahiDzahi Posts: 6Questions: 2Answers: 0
    edited March 2021

    Thanks Dear

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    @Dzahi is this still an issue? If so, can you post a few lines of the data returned from the ajax command, please.

    Colin

  • DzahiDzahi Posts: 6Questions: 2Answers: 0

    Thanks dear, the issue solved as Kevin presented.. thanks,, i have another problem with tabledit used in datatables,,,

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Yep, I replied on that other thread,

    Colin

This discussion has been closed.