Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am developing a Web App which uses jQuery Datatables to display dynamic data from an API url on my web server, which contains JSON data.

Now, I am trying to add an "Edit" button at the end of each data row, using mRender, but with no success so far. When I write the following piece of code:

...
'sAjaxSource': 'http://api.xxxxxxxx.dev:8000/v1/users',
'aoColumns': [
   { 'mData': 'id' },
   ...
   { 
      'mData': 'id',
      'mRender': function(data, type, full) {
                     return '<a href class="btn btn-xs" data-id="' + data.id + '">Edit</a>';
       }
   }

I end up with only the value of data.id being displayed, so without the whole <a>...</a> element.

Does anyone know how to fix this? Here is a screenshot of the actual web app: Click here

jQuery Datatables config

var dtOptions = {
'sAjaxSource': 'http://api.avansintro.dev:8000/v1/users',
'aoColumns': [
{ 'mData': 'id' },
{ 'mData': 'username' },
{ 'mData': 'display_name' },
{ 'mData': 'email' },
{ 'mData': 'created_at' },
{ "sTitle": "example",
  "mData": "id", 
  'mRender': function(data, type, full) {
     return '<a href class="btn btn-xs" data-id="' + data.id + '">Edit</a>';
  }
}
],
'aoColumnDefs': [
{
    'bSortable': false,
    'aTargets': [ 5 ]
},
{
    'bSearchable': false,
    'aTargets': [ 4, 5 ]
}
]
};
$scope.dtOptions = dtOptions;
share|improve this question
    
possibly because mData is rendering the id rather than mRender? What happens if you remove the line 'mData': 'id',? –  markpsmith 9 hours ago
    
@markpsmith You're right, but when I remove 'mData': 'id', it returns an error: ... requested unknown parameter 5 for row 0 ..., so without success :/ –  Robin 6 hours ago
    
Try changing data.id to full[0] –  markpsmith 6 hours ago
    
@markpsmith This resolves in the following result: [object Object], I really don't know what's going on here... –  Robin 6 hours ago
1  
It would be useful if you could create a JSFiddle. It's time-consuming unfortunately but you could find an existing one that has the datatable dependencies already and modify that e.g. here. You can add your data as a json var. –  markpsmith 5 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.