数据表(行详细信息):未捕获的TypeError:无法读取属性' style'未定义的

时间:2018-02-04 06:54:10

标签: javascript php jquery datatables

我试图实现数据表行详细信息

这是我的表格,其ID为" user_datas"

<table id="user_datas" class="table table-bordered table-striped" width="100%">
     <thead>
       <tr>
         <th>NAME</th>
         <th>SEX</th>
         <th>BIRTHDATE</th>
         <th>AGE</th>
       </tr>
     </thead>
  </table>

这是来自我试图实现

的数据表文档的代码
function format ( d ) {
    return 'Name: '+d.name+'<br>'+
   'Gender: '+d.sex+'<br>'+
        'The child row can contain any data you wish, including links, images, inner tables etc.';
}

$(document).ready(function() {
    var dataTable = $('#user_datas').DataTable({
      "processing":true,
      "serverSide":true,
      "order":[],
      "ajax":{
        url:"../controller/fetch_senior_citizen.php",
        type:"POST"
      },

      "columns": [
          {
            "class":          "details-control",
            "orderable":      false,
            "data":           null,
            "defaultContent": ""
          },
          { "data": "name" },
          { "data": "sex" },
          { "data": "birthdate" },
          { "data": "age" }
      ],
        "order": [[1, 'asc']]
    });

     // Array to track the ids of the details displayed rows
    var detailRows = [];

    $('#user_datas tbody').on( 'click', 'tr td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = dataTable.row( tr );
        var idx = $.inArray( tr.attr('id'), detailRows );

        if ( row.child.isShown() ) {
            tr.removeClass( 'details' );
            row.child.hide();

            // Remove from the 'open' array
            detailRows.splice( idx, 1 );
        }
        else {
            tr.addClass( 'details' );
            row.child( format( row.data() ) ).show();

            // Add to the 'open' array
            if ( idx === -1 ) {
                detailRows.push( tr.attr('id') );
            }
        }
    } );

    // On each draw, loop over the `detailRows` array and show any child rows
    dataTable.on( 'draw', function () {
        $.each( detailRows, function ( i, id ) {
            $('#'+id+' td.details-control').trigger( 'click' );
        } );
    } );                    
});

这是我的一些代码来自fetch_senior_citizen.php

foreach($result as $row)
{
    $name = clean($row["f_name"])." ".clean($row["m_name"])." ".clean($row["l_name"]);
    $sub_array = array();
    $sub_array["id"] = $row['id'];
    $sub_array["name"] = $name;
    $sub_array["sex"] = strtoupper($row["sex"]);
    $sub_array["birthdate"] = $row["birthdate"];
    $sub_array["age"] = $row["age"];
    $data[] = $sub_array;
}

$output = array(
    "draw"              =>  intval($_POST["draw"]),
    "recordsTotal"      =>  $filtered_rowsz,
    "recordsFiltered"   =>  get_total_all_records4(),
    "data"              =>  $data
);
echo json_encode($output, JSON_PRETTY_PRINT);

我目前面临的错误是未捕获的TypeError:无法读取属性&#39; style&#39;未定义,但如果我删除此代码

{
    "class": "details-control",
    "orderable": false,
    "data": null,
    "defaultContent": ""
}

万事如意。但我的表没有行详细信息

1 个答案:

答案 0 :(得分:1)

问题解决了!我只是忘了在thead上放空了