简体   繁体   中英

Passing An Array To Datatable Child Row

I'm trying to show data in my datatable in two ways:

  1. the Parent rows have the data coming from a table that user should not edit
  2. the Child row have the data coming from a table that user should edit

I'm passing them all through one array, in the Child Row, I am calling the data from the array index and then assign an Edit button to call an edit method.

The data shows in my Child Row correctly, but whenever I press on the edit button to call upon my modal, an error that says "Uncaught ReferenceError: d is not defined" and "Uncaught TypeError: Cannot read property '11' of undefined" appears.

Here is my Datatable JavaScript code:

$(document).ready(function() {
    var table = $('#table_roster').DataTable({
        "processing": true,
        ajax:           "/getproposals",
        deferRender:    true,
        scrollX:        true,
        scrollY:        400,
        scrollCollapse: true,
        scroller:       true,
        dom: 'Bfrtip',
    });

    $('#table_roster tbody').on('click', 'tr', function () {

        var tr = $(this).closest('tr');
        var row = table.row( tr );
       // editRow(row.data());
 
        if ( row.child.isShown() ) {
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            row.child( format(row.data()) ).show();
            tr.addClass('shown');
        }

    });

And here is the function format()

function format ( d ) {

    console.log(d)

    return '<table id="table_child" cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
        '<tr>'+
            '<td>IRG:</td>'+
            '<td>'+d[11]+'</td>'+
            '<td>IRG Date:</td>'+
            '<td>'+d[12]+'</td>'+
            '<td>Council Cycle:</td>'+
            '<td>'+d[13]+'</td>'+
            '<td>Score:</td>'+
            '<td>'+d[14]+'</td>'+
            '<td>Percentile:</td>'+
            '<td>'+d[15]+'</td>'+
            '<td>Assessment:</td>'+
            '<td>'+d[16]+'</td>'+
            '<td>'+
            '<button class="btn" onclick="editRow(d)">'+
            'Edit'+
            '</button>'+
            '</td>'+
        '</tr>'+
    '</table>';
}

Thank you:)

I solved this by initializing d in the method.

this.d = d;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM