Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean bStateSave cookie

Tags:

datatables

The bStateSave option can be used to save the state of the DataTable in a cookie.

I'd like to clean this cookie when logging out.

Currently, I checked the name of the cookie from my browser and changed its expiration date with PHP setcookie() at logout, coding the name in hard. It works, but it's a bit ugly since I don't know how to retrieve the cookie name from API.

Is there a proper way to do this?

Thanks

like image 786
galli2000 Avatar asked Jul 17 '12 08:07

galli2000


2 Answers

I don't know if you already got an answer, but this is what I used.

there is a property to initialize DataTables called: iCookieDuration. What you should do is to set a time expressed in seconds of "cookie duration"

then you have:

     $('.datatable').dataTable({           
        "iCookieDuration": 60*60*24,// 1 day (in seconds)
     });

then you set a time that you consider optimal. hope to help you !!

like image 60
CesarCarrillo Avatar answered Nov 06 '22 21:11

CesarCarrillo


State saving in DataTables is done by saving a JSON string to a cookie, allowing it to maintain as much browser compatibility as possible, while keeping the state storage on the client-side. At times it can be useful to be able to modify the parameters that are saved by the table.

If you want to clear datatable state on logging-out then i would like to suggest to simply clear storage.

The removeItem() method of the Storage interface, when passed a key name, will remove that key from the storage.

function populateStorage() {
  localStorage.setItem('bgcolor', 'red');
  localStorage.setItem('font', 'Helvetica');
  localStorage.setItem('image', 'myCat.png');

  localStorage.removeItem('image');
}

OR

Just write in javascript at loggin-out action.

echo '<script type="text/javascript">localStorage.clear();</script>';

ie

<script type="text/javascript">localStorage.clear();</script>
like image 39
Abhijit Jagtap Avatar answered Nov 06 '22 21:11

Abhijit Jagtap