问题
Why I'm getting error
Uncaught TypeError: Cannot read property 'mData' of undefined
I respect the DataTables requirements (and I also read another topics about my error and I respect every answer and solution). Please help me.
Here is my php code:
<table class="table table-striped table-bordered table-hover" id="sample_1">
<thead>
<tr>
<th class="table-checkbox">
<input type="checkbox" class="group-checkable" data-set="#sample_1 .checkboxes"/>
</th>
<th>
Utilizator
</th>
<th>
Nume complet
</th>
<th>
Clasa
</th>
<th>
Functia
</th>
<th>
E-Mail
</th>
<th>
Ultima logare
</th>
</tr>
</thead>
<tbody>
<?
foreach($data["users"] as $student)
{
?>
<tr class="odd gradeX">
<td>
<input type="checkbox" class="checkboxes" value="1"/>
</td>
<td>
<? echo $student["username"]; ?>
</td>
<td>
<? echo " ".$student["last_name"]." ".$student["first_name"].""; ?>
</td>
<td>
<? echo getclass($student["class"]); ?>
</td>
<td>
<?
$functie = 0;
if($student["role"] == 1)
{
$functie = 1;
echo "Administrator site";
}
if($student["fctsc"])
{
$functie = 1;
echo "Director";
}
if($student["diriginte"])
{
$functie = 1;
echo "Diriginte";
}
if($student["profesor"])
{
$functie = 1;
echo "Profesor";
}
if($functie == 0)
echo "Elev";
?>
</td>
<td>
<a href="mailto:<? echo $student["email"]; ?>">
<? echo $student["email"]; ?>
</a>
</td>
<td class="center">
<? echo $student["lastlogin"]; ?>
</td>
</tr>
<?
}
?>
</tbody>
</table>
回答1:
check weather or not all the php echo's are actually outputting something that is not 'NULL' on all rows as DataTables will treat any cell with just NULL as content as non existant and this can also cause that error.
try avoiding direct php output to html table.
回答2:
Usually this error occurs for two reasons:
- Missing table header.
- Number of
td
elements in the table body differs from number ofth
elements in the table header.
However your HTML code seems to have correct number of columns, see this example.
See jQuery DataTables: Common JavaScript console errors - TypeError: Cannot read property ‘mData’ of undefined for more information.
来源:https://stackoverflow.com/questions/30121671/jquery-datatables-uncaught-typeerror-cannot-read-property-mdata-of-undefined