添加到数据表的链接以获取更多信息

Adding a link to datatables for more information

本文关键字:获取 信息 链接 数据表 添加      更新时间:2023-09-26

我有这个代码清单,我想添加一个链接到数据表,我得到一个错误:DataTables警告(表id = 'example'):请求未知参数'3'从数据源为第0行,当我点击确定,它不加载链接我添加,这是我的代码

<script type="text/javascript" charset="utf-8">        
    $(document).ready(function() {
        var oTable = $('#example').dataTable( {
            "bProcessing": true,
            "sAjaxSource": "<?php echo base_url('operations/dataDisplayBanks/'); ?>",
            "aoColumns": [
                { "mData": "bank_id" },
                { "mData": "bank_name" },
                { "mData": "absolute_amount" },
                {
                    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                        $('td:eq(3)', nRow).html('<?php echo base_url() ?>operations/display/' + aData[0] + '">' +
                            aData[0] + '</a>');
                        return nRow;
                    }
                },
            ]
        } );
    } );
</script>

<div id="demo">
    <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
        <thead>
            <tr>
                <th>Client ID</th>
                <th>Client Name</th>
                <th>Absolute Limit</th>  
                <th>History</th> 
            </tr>
        </thead>
        <tbody> 
        </tbody>
        <tfoot>
    </table>
</div>

EDIT我的意思是说mRender在服务器端实现中使用比FnRowCallback更适合从数据创建url

这里是一个使用你的代码的例子,添加它并删除FnRowCallback

  { "mData": null , //its null here because history column will contain the mRender
    "mRender" : function ( data, type, full ) {
    return '<a href="<?php echo base_url(); ?>operations/display/'+full[0]+'">'+full[0]+'</a>';}
  },

文档:http://www.datatables.net/release-datatables/examples/advanced_init/column_render.html

您需要在aoColumns中为历史属性添加一个条目,这应该可以解决您看到的错误。datatable期望表中的每一列都有一个值,即使您计划以编程方式设置该值。我还没有找到解决这个问题的方法。

同样,你的fnRowCallback不应该是aoColumns的一部分。fnRowCallback应该是数据表配置对象的一部分(即aoColumns的对等体)。

你的配置看起来像这样:

{
    "bProcessing": true,
    "sAjaxSource": "<?php echo base_url('operations/dataDisplayBanks/'); ?>",
    "aoColumns": [
        { "mData": "bank_id" },
        { "mData": "bank_name" },
        { "mData": "absolute_amount" },
        { "mData": "history" } //need an entry here for history
    ],
    "fnRowCallback": function(nRow, aData, iDisplayIndex) {...}
}

你的数据看起来像这样:

[{
    "bank_id":1,
    "bank_name": "Fred's Bank",
    "absolute_amount": 1000,
    "history": ""
},
...
]