【问题标题】:How to add extra field in datatable which is not database如何在不是数据库的数据表中添加额外的字段
【发布时间】:2016-10-03 06:40:22
【问题描述】:

我正在尝试在数据表中添加editdelete 按钮。

我有html

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Theater name</th>
            <th>Action</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Theater name</th>
            <th>Action</th>
        </tr>
    </tfoot>
</table>
$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "<?php echo JRoute::_('index.php?option=com_wsmovies&task=addtheatres' ); ?>"
    });
});

我尝试在 theadtbody 中添加列,但它提醒我说

DataTables 警告:表 id=example - 请求第 0 行第 1 列的未知参数“1”。有关此错误的详细信息,请参阅http://datatables.net/tn/4

服务器返回数据

{"draw":0,"recordsTotal":57,"recordsFiltered":57,"data":[["Tiger","Nixon"],["Garrett","Winters"],["Ashton ","Cox"],["Cedric","Kelly"],["Airi","Satou"],["Brielle","Williamson"],["Herrod","Chandler"],["Rhona ","戴维森"],["科琳","赫斯特"],["索尼娅","弗罗斯特"],["耶拿","盖恩斯"],["奎因","弗林"],["查德","Marshall"],["Haley","Kennedy"],["Tatyana","Fitzpatrick"],["Michael","Silva"],["Paul","Byrd"],["Gloria ","Little"],["Bradley","Greer"],["Dai","Rios"],["Jenette","Caldwell"],["Yuri","Berry"],["Caesar ","Vance"],["Doris","Wilder"],["Angelica","Ramos"],["Gavin","Joyce"],["Jennifer","Chang"],["Brenden ","Wagner"],["Fiona","Green"],["Shou","Itou"],["Michelle","House"],["Suki","Burks"],["Prescott ","Bartlett"],["Gavin","Cortez"],["Martena","Mccray"],["Unity","Butler"],["Howard","Hatfield"],["Hope ","Fuentes"],["Vivian","Harrell"],["Timothy","Mooney"],["Jackson","Bradshaw"],["Olivia","Liang"],["Bruno ","Nash"],["Sakura","Yamamoto"],["Thor","Walton"],["Finn","Camacho"],["Serge","Baldwin"],["Zenaida ", "Frank"],["Zorita","Serrano"],["Jennifer","Acosta"],["Cara","Stevens"],["Hermione","Butler"],["Lael", "Greer"],["Jonas","Alexander"],["Shad","Decker"],["Michael","Bruce"],["Donna","Snider"]]}

谁能帮我解决这个问题

【问题讨论】:

  • 你有没有尝试过?请发布您尝试的代码,我们可以帮助您调试它

标签: javascript php jquery datatables joomla3.0


【解决方案1】:

您只需将其 HTML 添加到您的 DataTable 定义中

$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "<?php echo JRoute::_('index.php?option=com_wsmovies&task=addtheatres' ); ?>",
    "columns": [
             {
                "targets": -1,
                "data": null,
                "orderable": false,
                "defaultContent": [ 
                    "<i  class='glyphicon glyphicon-edit'></i>"+
                    "<i  class='glyphicon glyphicon-trash'></i>"]

             }

    ]
} );

演示:https://jsfiddle.net/Prakash_Thete/evfchh7q/

如下更改表定义(在发送两列数据时添加了一个标题 + 编辑按钮列)。

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Theater name</th>
            <th>One more header</th>
            <th>Action</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Theater name</th>
            <th>One more header</th>
            <th>Action</th>
        </tr>
    </tfoot>
</table>

【讨论】:

  • 它不工作。我只收到Processing... @PrakashThete
  • 在控制台TypeError: f is undefined kb() jquery.dataTables.min.js:27 ga() jquery.dataTables.min.js:48 m/&lt;() jquery.dataTables.min.js:93 .each() jquery-1.12.3.js:370 jQuery.prototype.each() jquery-1.12.3.js:137 m() jquery.dataTables.min.js:82 h.fn.DataTable() jquery.dataTables.min.js:166 &lt;anonymous&gt; datatable:12 jQuery.Callbacks/fire() jquery-1.12.3.js:3232 jQuery.Callbacks/self.fireWith() jquery-1.12.3.js:3362 .ready() jquery-1.12.3.js:3582 completed()
  • 我添加了演示链接。请看一看。
  • 您正在向["Tiger","Nixon"] 之类的数据发送一个额外的参数,或者添加一个额外的标头,或者在不需要时从响应中删除"Nixon"
  • 更新了答案,JSFiddle请看一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多