Trouble reformatting or rendering column data with mdata, mrender

Trouble reformatting or rendering column data with mdata, mrender

dchevalierCACLVdchevalierCACLV Posts: 3Questions: 0Answers: 0
edited January 2014 in DataTables 1.9
Greetings:

I'm having trouble understanding how to use the aoColumns, aTargets, mdata and mrender functions to reformat some column results as links.

I have a table set up with server side processing, and the last two columns (the 6th and 7th column) are configured to return the id (primary key) of the row from the database.

Here is my code the first try:

[code]


$(document).ready(function() {
$('#example').dataTable( {
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/server_processing.php",


"aoColumnDefs": [ {
"aTargets": [ 5, 6 ],
"mData": 5,
"mRender": function ( data, type, full ) {
return 'Edit1';
},
"mData": 6,
"mRender": function ( data, type, full ) {
return 'Edit2';
}

}],

"oScroller": {
"loadingIndicator": true
}
} );
} );


[/code]

This results in both the columns I've targeted being set to the same link (the last link) [code] 'Edit2'[/code].

I changed the code to this:

[code]

"aoColumnDefs": [ {
"aTargets": [ 5 ],
"mData": 5,
"mRender": function ( data, type, full ) {
return 'Edit';
},
}],
"aoColumnDefs": [ {
"aTargets": [ 6 ],
"mData": 6,
"mRender": function ( data, type, full ) {
return 'Edit';
}
}],

[/code]

and it completely skips the 6th column but correctly renders the last and 7th column.

I'm not real familiar with javascript, but I've been working with PHP and mysql for about 6 month. What am I doing wrong with this script?

I would be grateful for any assistance.


Best,
David

Replies

  • dchevalierCACLVdchevalierCACLV Posts: 3Questions: 0Answers: 0
    edited January 2014
    Hey I found my solution based on an older post in the forums I had yet to discover - here is the code that works for me ( it properly adds a link to the second to last column in my table that goes to edit 1, and link in the last column that goes to edit2). Cheers!

    [code]
    "aoColumnDefs": [
    { 'mRender': function ( data, type, full ) {
    return 'Edit';
    }, 'aTargets' : [ 5 ] },
    { 'mRender': function ( data, type, full ) {
    return 'Edit';
    }, 'aTargets' : [ 6 ] },

    { "mData": 5, 'aTargets': [ 5 ] },
    { "mData": 6, 'aTargets': [ 6 ] }


    ],
    [/code]
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Your last code looks okay, if a bit verbose. Before you were defining mData and mRender twice in a single object. That would be like doing:

    [code]
    var i = 1;
    i = 2;
    [/code]

    and expecting `i` to contain both 1 and 2 :-). Splitting it into two objects is the way to do it:

    [code]
    "aoColumnDefs": [{
    'aTargets': [5],
    "mData": 5,
    'mRender': function (data, type, full) {
    return 'Edit';
    }
    }, {
    'aTargets': [6],
    "mData": 6,
    'mRender': function (data, type, full) {
    return 'Edit';
    }
    }]
    [/code]

    Allan
This discussion has been closed.