0

I use standard pagination in Bootstrap table. I want to remember in cookie page size value selected by user.

HTML:

<table id="users-table" class="table table-striped table-condensed" 
   data-toggle="table"
   data-url="/data.json"
   data-side-pagination="server"
   data-pagination="true"
   data-page-size="25">
...
</table>

Javascript:

$('#users-table').bootstrapTable({
    onLoadError: function (status) {
        SetErrorMessage('error');
    }
});

How can I detect page size changing to save in cookie this value?

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
Jacek
  • 11,661
  • 23
  • 69
  • 123

4 Answers4

2

In order to solve this you have to use the Cookie extension that is supported by Bootstrap table plugin. Please see this: https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/cookie and this example: http://issues.wenzhixin.net.cn/bootstrap-table/#extensions/cookie.html

1

You can override onPreBody and onPageChange method for check if pageSize is changed and set it from cookie before post to server.

There is code

    var CookieName = "ShipmentsInSessionTablePageSize";

    $('#table').bootstrapTable({

        onPreBody: function(data) {

            var pageSizeCookie = $.cookie(CookieName);
            if (pageSizeCookie != null) {

                var pageSizeInt = parseInt(pageSizeCookie);
                this.pageSize = pageSizeInt;
            }
        },
        onPageChange: function(pageNumber, pageSize) {

            $.cookie(CookieName, pageSize, { expires: 30 });
        },
        ...
Jacek
  • 11,661
  • 23
  • 69
  • 123
0

Use jQuery to get the page-size:

var pagesize = $('#users-table').data('page-size');

Save the value in a cookie using the jquery-cookie plugin:

$.cookie('size', pagesize, {
      expires: 30 //Expires in 30 days
});

Retrieve it:

var cookie = $.cookie('size');

And set the table size after retrieving:

$('#users-table').data('page-size', cookie);
Rudie Visser
  • 570
  • 6
  • 23
0

You have to detect the click in size dropdown located under table to get the size selected by the user and save it in cookies using Jquery-cookies or js-cookie.

Detect user choice Example.

This is example using js-cookie :

var page_size = 25; //Default size

//If cookie page_size exist
if( Cookies.get('page_size') != null )
{
    page_size = Cookies.get('page_size');
    $('#inpost-users-table').data('page-size', page_size); //set page size
}

//When user choose new size 
$(document).on('click', '.dropdown-menu', function()
{
    var size_selected = $('.pagination-detail .page-size').text();
    Cookies.set('page_size', size_selected);
}); 

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101