renderizar botones, serverside.
renderizar botones, serverside.

I am painting edit button if the user is administrator type.
It works correctly if it is administrator type, but how can I do so that it does not paint anything if the condition is not met, because I get this error:
{ data: 1},
{ data: 2},
{ data: 3},
{ data: 4},
{
"render": function ( data, type, row ) {
var idus = $('#variable_sesi').val();
var user = $('#variable_sesi2').val();
if(user==="ADMIN"){
return '<div class="btn-group" role="group" aria-label="Third group"><button class="btn btn-warning btnEditM manito-clic" edit='+row['0']+' idUse='+idus+' User='+user+' data-toggle="modal" data-target="#modalEditarM"><i class="fa fa-pencil"></i></button> </div>';
}
}
}
it works correctly if it is administrator type.
if it is not of type admin I receive this error: jquery.dataTables.min.js:76 Uncaught TypeError: Cannot read property 'style' of undefined
I only need to paint the button when it is of type admin, but then not paint anything, as if that column does not exist
Answers
You always need to return something in
columns.render
. What you return is up to your requirements but here is an example:Kevin
It doesn't work because I am validating from html, if the user is of type admin.
html:
when not admin i keep getting this error.
That is because you aren't returning anything. You can simple use
return '';
if you don't want to show anything. If you still need help post a link to your page or a test case showing the issue so we can see what you have and help debug.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
how to make a test case, using server side?
The data source isn't part of the problem. You can simulate the data using Javascript and use
data
instead ofajax
to apply the data to the table. See this example.Kevin
http://live.datatables.net/munegufo/1/edit
Thanks for the test case. The test case has the original HTML table with 6 columns. But your Datatables config has 7 columns resulting in the style of undefined error. I removed the HTML table and added
columns.title
to the 7th column (button column). Now the table is rendering:http://live.datatables.net/munegufo/2/edit
Kevin
I appreciate your help,
but I don't think I made myself understood.
the html table has 7 columns when the user is an administrator, that is, the missing th is painted when the user is an administrator.
but when the user is not admin then I don't paint the seventh column
that's why I keep getting the error when the user is not an admin
and I don't know how I can handle it
my code html:
You can use column visibility for this. See this updated example:
http://live.datatables.net/munegufo/3/edit
It uses
columns.visible
to hide the column by default. IninitComplete
it usescolumn().visible()
to make the column visible if the user isADMIN
. Change the global varuser
toADMIN
to see the change.Kevin
now i have this error and ajax data keeps loading
Its hard to say what the problem is without seeing it. Please update the test case or provide a link to your page so we can see what you have to help debug.
My guess is you still have a problem with the number of HTML table columns being different than the number of columns defined in Datatables. They need to match. Or you can use
columns.title
to have Datatables create the columns like the example we are working with.Kevin