【问题标题】:TypeError C is undefined datatablesTypeError C 是未定义的数据表
【发布时间】:2018-01-01 13:05:07
【问题描述】:

我正在尝试将使用 ajax 获得的一些数据渲染到数据表中,但我似乎遗漏了一些东西,因为它显示错误:TypeError: c is undefined。

我已经阅读了这篇文章 Datatables TypeError: c is undefined,但是给出的任何解决方案都没有解决我的问题。有人有什么想法可以帮助我吗?谢谢。

html代码:

<table id="itinerariDetailTable" class="table table-sm table-striped table-bordered" style="font-size: x-small;">
                        <thead class="thead-inverse">
                            <tr>
                                <th>Id</th>
                                <th>Codi</th>
                                <th>Descripció</th>
                                <th>Temporada</th>
                                <th>Districte</th>
                                <th>Barri</th>
                                <th>C. Treball</th>
                                <th>G. Servei</th>
                                <th>T. Servei</th>
                                <th>Máquina</th>
                                <th>Corretorn</th>
                                <th>Torn</th>
                                <th>Tipus Día</th>
                                <th>Equips</th>
                                <!-- <th>Jornades</th>
                                <th>Cost unitari</th>
                                <th>T. Itinerari</th>
                                <th>Escenario</th> -->                              
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <th>Id</th>
                                <th>Codi</th>
                                <th>Descripció</th>
                                <th>Temporada</th>
                                <th>Districte</th>
                                <th>Barri</th>
                                <th>C. Treball</th>
                                <th>G. Servei</th>
                                <th>T. Servei</th>
                                <th>Máquina</th>
                                <th>Corretorn</th>
                                <th>Torn</th>
                                <th>Tipus Día</th>
                                <th>Equips</th>
                                <!-- <th>Jornades</th>
                                <th>Cost unitari</th>
                                <th>T. Itinerari</th>
                                <th>Escenario</th> -->                              
                            </tr>
                        </tfoot>
                    </table>

js代码:

$('#itinerariDetailTable').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax":{
            "url":'/escenaris/selectById',
            "type":'GET',
            "data": function(d){
                d.idEscenari = $('#idEscenari').val();                  
            }
        },
        "order": [[ 0, "asc" ]],
        "columns": [
            { "data": "idItinerari" },
            { "data": "codiItinerari" },
            { "data": "descripcio" },
            { "data": "temporada.codiTemporada" },
            { "data": "districte" },
            { "data": "barri" },
            { "data": "centreTreball" },
            { "data": "grupServei" },
            { "data": "tractamentRecursos" },
            { "data": "maquinaCombustible" },
            { "data": "corretorn" },
            { "data": "torn" },
            { "data": "tipusDia" },
            { "data": "nombreEquips" }
            /*{ "data": "frequencia" },
            {"data": "resultatItinerari.costUnitari", "defaultContent": "0" },
            { "data": "tipusItinerari.codiTipusItinerari" }/*,                          
            { "data": "escenari.idEscenari" }           */  
        ]
    });

【问题讨论】:

  • 已经阅读了这篇文章,但没有解决任何问题。
  • 请同时提供您的 html 代码,如果可能的话,请提供解决问题的方法

标签: javascript datatables


【解决方案1】:

这个问题已经很久没有被问过了,但是我已经在几个线程中看到了它,所以我将分享我的答案以供将来参考。

这种类型的错误通常(如果不是特别是)与 HTML table 元素的结构有关。

根据 DataTables 手册:'为了让 DataTables 能够增强 HTML 表格,表格必须是有效的、格式良好的 HTML,带有标题 (thead) 和单体(tbody)。也可以使用可选的页脚 (tfoot)。'

上面的代码缺少 tbody 标签,这似乎是问题所在。

【讨论】:

    【解决方案2】:

    解决的方法是

    1. th 的个数必须等于表中 td 的个数

    用 tbody 代替 tfoot

    格式是你必须把thead和tbody括起来

      <table> 
          <thead>
               <tr>
                  <th>sn</th>
                  <th>name</th>
                  <th>address</th>
               </tr>
          </thead>
          <tbody>
               <tr>
                  <th>1</th>
                  <th>Avi chhetri</th>
                  <th>Nepal</th>
               </tr>
          </tbody> 
     </table>
    

    【讨论】:

      猜你喜欢
      • 2017-04-07
      • 1970-01-01
      • 2016-01-15
      • 2019-12-11
      • 2012-10-23
      • 2016-01-14
      • 2015-08-04
      • 2014-10-26
      • 1970-01-01
      相关资源
      最近更新 更多