Change datatable columns properties ( aoColumns ) when we wish

Change datatable columns properties ( aoColumns ) when we wish

ChemonChemon Posts: 19Questions: 0Answers: 0
edited December 2012 in General
Hi, merry christmas everyone,

How can we create dinamicaly columns properties for our datatable ?

The table ( datatable ) need to take some diferent number of columns depending of the user privileges. So when an user load the page, datatable has to be created depending of user privileges ( his rol, privileges, resources... are stored on database ).
And other point, an admin change user privileges, giving or taking away privileges to see / not see a specific column ( or see it in other way, with other properties - other diferent example - ). The user is currently using the application, taking comunication via ajax and getting the filtered, ordered, paginated data. But our application must work refreshing the datatable on users client to show or take away the column from datatable.
I know how send the new column data on the ajax call, isnt hard, is server code to make the new json with the data of the columns we had + the data of the new one.
But what about changing columns properties on Datatable to add the new one on client side ?
How do we do this ?

I have been studying datatables like 7 days.
Can my problem be solved with datatables ? and how do i have to implement my code to do it ?
Please, please, please, I am like 3 days stoped in this point, need to know if i can and how, to start implement or look for any other solution.
Ty vm. I wish you all health.

[code]
$(document).ready(function() {
var oTable;
$('#dynamic').html('');

function initTabla( json )
{
oTable = $('#example').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "http://gestionmimascota.loc/categorias/cargaDatos",
"sServerMethod": "POST",
"aaData": json.aaData,
"aoColumns": json.aoColumns,
...
});
}

$.ajax( {
"dataType": 'json',
"type": "POST",
"url": "http://gestionmimascota.loc/categorias/iniciaTabla",
"success": initTabla,
"error": function(){ alert("Error en la inicializacion de la tabla.\n Contacte con el administrador.")}
} );
});
[/code]

This is my code and it works like this. But i dont know if it is the best solution, if it is right or i could have problems, ...

Another example for the problem i am talking about is if u want to create dinamic processing on datatable, depending on how many data rows u send from server.
We could need to make datatable Client processing if we have < 500 registers and server processing if we have > 500 registers of the data we want to show on table.

Replies

  • allanallan Posts: 63,389Questions: 1Answers: 10,450 Site admin
    You cannot - the column settings are static once initialised and connote be changed. Looking through the DataTables source you'll be able to see that some could be, and you could add modifications to expose that ability, but it is not supported. In general if you want to change a setting after initialisation you need to destroy the table and recreate it.

    Allan
  • ChemonChemon Posts: 19Questions: 0Answers: 0
    edited December 2012
    Oh Allan, ty for ur quickly answer and thanks for being so clear.

    And what do u think about ? whats the best solution in ur opinion ?

    I will tell u what i have in mind: I could check in each Ajax data request if user privileges changed ( saving timestamp in $_SERVER and checking with DB timestamp )
    If it is diferent, changed, then i could reload the page to do one more time the first normal ajax call to create the datatable with the new aoColumns properties.
    [code]
    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": "http://gestionmimascota.loc/categorias/iniciaTabla",
    "success": initTabla,
    "error": function(){ alert("Error en la inicializacion de la tabla.\n Contacte con el administrador.")}
    } );
    [/code]

    write in the fnServerData something like
    if json.timestampChanged then
    location.reload();

    I think the first thing with what i could have problem is with state of filter, pagination, order.
    If a user has his own set up and state is saved on cookie or anywhere. If i reload the page ... i could have problems.

    Dont know, how do u see it ? whats ur opinion ?
  • allanallan Posts: 63,389Questions: 1Answers: 10,450 Site admin
    That sounds reasonable to me, but you'll need to decide what interaction you want for your own app/page :-). State saving with localStorage also sounds like a good idea for retaining state - but you'll need to watch that if the table reloaded with a different number of columns, you'll probably want to invalidate the old state saved data.

    Allan
This discussion has been closed.