Datatable child remote undefined ID in child table

Viewed 130

i have code to build datatable with child datatable, and i use codeigniter to build my project, but it always error as this

var KTDatatableChildRemoteDataDemo = function() {
             var demo = function() {
                 var datatable = $('.kt-datatable').KTDatatable({ 
                     data: {
                         type: 'remote',
                         source: {
                             read: {
                                url: '<?= base_url($c_name . 'listdata') ?>',
                            },
                         },
                         serverPaging: true,
                         serverSorting: true,
                     },
                     detail: {
                         title: 'Load sub table',
                         content: subTableInit,
                     },
                     columns: [{
                         field: 'ID',
                         title: '',
                     },
                     {
                         field: 'FULL_NAME',
                         title: 'Full Name',
                     }],
                 });
                 function subTableInit(e) {
                     $('<div/>').attr('id', 'child_data_ajax_' + e.data.ID).appendTo(e.detailCell).KTDatatable({
                         data: {
                             type: 'remote',
                             source: {
                                 read: {
                                    url: '<?= base_url($c_name . 'listUniversity') ?>',
                                    method: 'post',
                                     params: { 
                                         ID: e.data.ID,
                                     },
                                 },
                             },
                             serverPaging: true,
                             serverSorting: true,
                         },  
                         columns: [{
                             field: 'UNIVERSITY',
                             title: 'University',
                             width: 100,
                         }, {
                             field: 'YEAR',
                             title: 'Year',
                         }],
                     });
                 }
             };
             return { 
                    init: function() { 
                    demo();
                 },
             };
         }();
         jQuery(document).ready(function() {
             KTDatatableChildRemoteDataDemo.init();
         });

the error is undefined ID in

$('<div/>').attr('id', 'child_data_ajax_' + e.data.ID).appendTo(e.detailCell)

and then i print var e with console.log(e) and the output is Cannot read property 'ID' of undefined enter image description here so how to solve this error?

1 Answers

I was facing this issue a little while ago. In my case, I'm using Oracle db. I was trying to show a ktdatatable with subtables on each main row.

The issue is that e.data is undefined in the e object. That's why it shows below error when logged.

javascript error

The reason is in the core.datatable.js file. Core datatable js e.data

I can't explain it in much detail, but you can look into it.

In my case, the main reason was, when the data was received from the request, the all the 'ids' were type string since I was using Oracle. Make sure for the data you're fetching remotely, the primary field like 'id' should be of type number.

You'll have to convert those string 'ids' to type 'number' on the backend.

Related