3

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>
Abdul Manan
  • 2,255
  • 3
  • 27
  • 51
Maurice
  • 280
  • 3
  • 14
  • 1
    you need to post how you are initializing dataTable!! – Guruprasad J Rao May 08 '15 at 10:37
  • 1
    No, I don't need. Cause, if I create another without php codes, just html, everything works fine. The problem is in that code.
    – Maurice May 08 '15 at 10:39
  • 1
    The problem will be in your dataTable initialization!! You must have written somewhere like this `$("#sample_1").dataTable();` Post that part!! – Guruprasad J Rao May 08 '15 at 10:41
  • 1
    @Maurice have you checked that final (shown in browser) `` is correct/valid one? Mix of PHP and HTML is not really helpful - post example of final HTML.
    – Regent May 08 '15 at 10:42
  • Yes, is a correct one, full HTML source code is here: http://pastebin.com/X25zgTVW – Maurice May 08 '15 at 10:45
  • 1
    How does your script looks like? I think of the dataTables initialisation. – davidkonrad May 08 '15 at 11:04
  • show your jquery script for the table – Abdul Manan May 21 '15 at 06:38
  • Need jquery code to check this. well why r u using $functie .. just use switch or ifelse ... and else in end ... – Zohaib May 26 '15 at 12:05
  • which version of datatables and jquery are you using? – mason81 Jun 02 '15 at 02:39
  • Without your js code its a bit of a guess, but the last few times I saw this error it was due to making a call to a column that is present. Say your table has five column and you call to the sixth column. But with out js, this is only a guess.. – wesleywmd Jun 10 '15 at 21:20

2 Answers2

0

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.

0

Usually this error occurs for two reasons:

  • Missing table header.
  • Number of td elements in the table body differs from number of th 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.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185