2012-09-05 24 views
17

Mam problem z tym, aby biblioteka jQuery Datatables była poprawnie wyświetlana na mojej stronie internetowej Joomla. http://datatables.netTypeError: oColumn jest niezdefiniowana podczas korzystania z biblioteki jQuery Datatables

Scenariusz jest w połowie stylizowany na mój stół, a następnie poddawany (otrzymuję zmianę koloru nagłówka tabeli i kolor tekstu, ale nie ma elementów sterujących datatables itp.).

Firebug jest również rzucanie się następujący błąd:

TypeError: oColumn is undefined 

W moich szablonów Joomla index.php Mam następujący w <head>:

<script src="./datatables/js/jquery.js" type="text/javascript"></script> 
<script src="./datatables/js/jquery.dataTables.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    jQuery.noConflict();     
    jQuery(document).ready(function() { 
    jQuery('#staff_table').dataTable({ 
     "bLengthChange": true, 
     "bFilter": true, 
     "bSort": true, 
     "bInfo": true, 
     "bAutoWidth": true 
     }); 
    }); 
</script> 

HTML/PHP wygląda następująco:

<h3>Members of Staff</h3> 
<p>If you're looking for a member of staff at Tower Road Academy, you'll find their details here.</p> 
<table class="staff_table" id="staff_table"> 
    <tr class="staff_table_head"> 
     <th>Name</th> 
     <th>Job Title</th> 
     <th>Email Address</th> 
    </tr> 

    <?php 
     $result = mysql_query("SELECT * FROM itsnb_chronoforms_data_addstaffmember"); 

     while($row = mysql_fetch_array($result)) 
     { 
     echo '<tr>'; 
     echo '<td>' . $row['staff_name'] . '</td><td>' . $row['staff_job'] . '</td><td><a  href=mailto:"' . $row['staff_email'] . '">' . $row['staff_email'] . '</a>' . '</td>'; 
     echo '</tr>'; 
     } 
    ?> 
</table> 

Odpowiedz

28

Aby moduły datatable działały poprawnie, tabele muszą być zbudowane z odpowiednimi znacznikami <thead> i <tbody>.

All DataTables needs in your HTML is a <TABLE> which is well formed (i.e. valid HTML), with a header (defined by a <THEAD> HTML tag) and a body (a <TBODY> tag)

Datatable docs

+0

dziękuję, to kliknęło w tym samym czasie, gdy wpisałem poniżej :-D –

+0

@IainSimpson tak, właśnie wpadłem na to całkiem niedawno, więc było to na samym szczycie mojej głowy! – hsalama

+0

świetna pomoc .. dziękuję .. – Charmie

0

Spróbuj tego:

jQuery('#staff_table').dataTable({ 
         "bPaginate": true, 
         "bLengthChange": true, 
         "bFilter": true, 
         "bSort": true, 
         "bInfo": true, 
         "bAutoWidth": true, 
        "aoColumns": [ 
             null, 
             null //put as many null values as your columns 

        ] 
        }); 
+0

nCell jest niezdefiniowany –

4

rozwiązywało problem!, Okazuje się DataTables jest bardzo surowe o html to akceptuje, zanim zgłosi błąd. Dodałem tagi do mojego html i teraz to działa, zauważ także, że musisz mieć tagi dla kolumn nagłówka, a nie tagów, ponieważ powoduje to również błąd.

Kod html wygląda teraz tak:

<h3>Members of Staff</h3> 
<p>If you're looking for a member of staff at Tower Road Academy, you'll find their  details here.</p> 
<table class="staff_table" id="staff_table"> 
<thead> 
<tr class="staff_table_head"> 
<th>Name</th> 
<th>Job Title</th> 
<th>Email Address</th> 
</tr> 
</thead> 

<?php 
$result = mysql_query("SELECT * FROM itsnb_chronoforms_data_addstaffmember"); 

while($row = mysql_fetch_array($result)) 
    { 
echo '<tr>'; 
echo '<td>' . $row['staff_name'] . '</td><td>' . $row['staff_job'] . '</td><td><a   href=mailto:"' . $row['staff_email'] . '">' . $row['staff_email'] . '</a>' . '</td>'; 
    echo '</tr>'; 
    } 
?> 
</table> 

i wywołanie jQuery itp wygląda następująco:

<script src="./datatables/js/jquery.js" type="text/javascript"></script> 
    <script src="./datatables/js/jquery.dataTables.js" type="text/javascript"></script> 

      <script type="text/javascript"> 

jQuery(document).ready(function() { 
jQuery('#staff_table').dataTable({ 
    "bLengthChange": true, 
    "bFilter": true, 
    "bSort": true, 
    "bInfo": true, 
    "bAutoWidth": true 
}); 
}); 

    </script> 
3

Nadzieja to by pomóc wszystkim, co najmniej, aby uzyskać podpowiedź z to.

http://datatables.net/forums/discussion/comment/42607

Mój problem był, TypeError: kol jest niezdefiniowany.

Podsumowując Odpowiedź:

Number of TH elements within the TR element within the THead element of the Table MUST BE EQUAL to the Number of TD elements(or number of columns within your Table Rows) within the TR element(s) of your TBody in the Table.

można przeczytać wyjaśnienie poniżej:

Problem polegał na tym, że nie miał wystarczającą ilość elementów Th lub Td w sekcji thead być równa do liczby kolumn, które drukuję jako elementy Td wewnątrz elementów Tr w sekcji TBody.

Poniższy kod dał mi błąd.

<thead> 
<tr> 
    <th> Heading 1</th> 
    <th> Heading 2</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
    <td> Column 1 </td> 
    <td> Column 2</td> 
    <td> Column 3</td> 
</tr> 
</tbody>" 

Ale dodanie jeszcze jednego elementu Th do elementu Tr w elemencie THead sprawiło, że działa jak urok! :) I mam wskazówkę z powyższego linku.

Zauważyłem również, że wszystkie elementy TH w elemencie THead mogą być również elementami TD, ponieważ jest to również poprawny HTML do wymaganego poziomu przez jQuery DataTables!

Mam nadzieję, że pomogłem niektórym z was zaoszczędzić czas! :)

Powiązane problemy