Adding a link to datatables for more information

2019-07-28 02:21发布

问题:

I have this code listing, i would like to add a link to datatables, I am getting an error: DataTables warning (table id = 'example'): Requested unknown parameter '3' from the data source for row 0, When i click ok, it does not load the link i have added, Here is my code

<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>

回答1:

EDIT i meant to say mRender is preferred for use over FnRowCallback on server-side implementations to create urls from data

here is an example using your code, add it and remove the 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>';}
  },

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



回答2:

You need an entry in aoColumns for the history property, that should solve there error you're seeing. Datatables expects a value for every column in the table, even if you plan on setting that value programmatically. I haven't found a way around this.

Also, your fnRowCallback shouldn't be a part of aoColumns. fnRowCallback should be part of the datatables configuration object (i.e., a peer of aoColumns).

Your config will look something like this:

{
    "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) {...}
}

Your data will look something like this:

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