Datatables ajax.reload() launching error

Datatables ajax.reload() launching error

pierissimopierissimo Posts: 5Questions: 0Answers: 0

Hi!
I'm working on datatables with server side processing, my code is:

$(document).ready(function() {
        $companyTable = $('#admin-companies-datatable');
        publics.datatableInstance = $companyTable.DataTable({
            //"processing": true,
            "serverSide": true,
            ajax: {
                url: ENV.routes.getCompaniesData,
                type: 'POST'
            },
            "columnDefs": [ 
                { "data": "company_name", "targets": 0 },
                { "data": "phy_address", "targets": 1 },
                { "data": "phy_city", "targets": 2 },
                {
                    "targets": 3,
                    "data": "active",
                    "render": function ( data, type, full, meta ) {
                        if( type === 'display' ){
                            return TPL.admin.companies.statuslabel({ companyID : full.id, status : full.active });
                        }
                    }
                },
                {
                    "targets": 4,
                    "data": "id",
                    "render": function ( data, type, full, meta ) {
                        if( type === 'display' ){
                            return TPL.admin.companies.actions({ companyID : full.id, status : full.active });
                        }
                    }
                }  
            ]
        });

        $companyTable.on('click','.actions [data-action]',function(e){
            e.preventDefault();
            $a = $(this);

            MYRESAPP.adminActions({
                url : ENV.routes.companyStatus,
                data: { active : $a.data('action')  },
                callback : function(data){
                    alert('fatto!');
                }
            });
        });

    });

the json data is:

{  
   "draw":"1",
   "recordsTotal":2,
   "recordsFiltered":2,
   "data":[  
      {  
         "company_name":"MyResApp",
         "phy_address":"4333 Goodfellow Blvd",
         "phy_city":"Saint Louis",
         "active":"1",
         "id":"1"
      },
      {  
         "company_name":"Give Back Mktg Group",
         "phy_address":"7110 Oakland Ave. Suite 208",
         "phy_city":"Saint Louis",
         "active":"2",
         "id":"18"
      }
   ]
}

On load, it works well, but when :

datatableInstance.ajax.reload()

it launch the error "DataTables warning: table id=admin-companies-datatable - Requested unknown parameter 'active' for row 0. For more information about this error, please see http://datatables.net/tn/4"

Why? I cannot figure it out!
Thanks
Piero

Replies

  • pierissimopierissimo Posts: 5Questions: 0Answers: 0

    up! I need an help :)

  • pierissimopierissimo Posts: 5Questions: 0Answers: 0

    no one ?

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited February 2015

    Is publics an object? Your table is publics.datatableInstance so I'm just wondering why your ajax reload wouldn't look something like publics['datatableInstance'].ajax.reload().

    The error makes me think that isn't the issue though, but the active column is missing from the reload data. Is your json data the exact same on initial load and after the reload?

  • allanallan Posts: 63,682Questions: 1Answers: 10,498 Site admin
    edited February 2015

    publics['datatableInstance'].ajax.reload()

    edit I was talking rubbish here. I suspect that its just a typo in the opening post, but it is a valid point you make @ignignokt!

    However, I agree - the fact you are getting the error on reload suggests the data is different on reload. Can you show us the JSON data from the reload. Or better yet a link to the page showing the issue (as per the forum rules).

    Allan

  • pierissimopierissimo Posts: 5Questions: 0Answers: 0

    @ignignokt!

    Right, it was a typo!
    I managed to solve it, i added "return data" outside each if in render function.
    This way:

    "render": function ( data, type, full, meta ) {
          if( type === 'display' ){
               return TPL.admin.companies.statuslabel({ companyID : full.id, status : full.active});
           }
           return data;
     }
    
    

    Thank you!

This discussion has been closed.