How to save pagelength only?

How to save pagelength only?

rotaerczrotaercz Posts: 31Questions: 7Answers: 0

I notice that

stateSave: true

also saves the sorting of the columns. Is there a way to only save the pagelength that's been selected?

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Add and event handler to the page length changed and use that event to put in in session or local storage.

  • rotaerczrotaercz Posts: 31Questions: 7Answers: 0

    That sounds pretty complicated. How would I go about doing this?

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    its easy. Here is a sample.
    http://jsbin.com/zizepoh/edit?js,output

        $(document).ready(function() {
            
            // get the saved value. localStorage.getItem returns null if not set so 
            // if null I set it to 15.
            var pageLength =  localStorage.getItem('example_length') || 15;
    
            var dtTable = $('#example').DataTable( {
                "data": dataStore.data,
                "select":"single",
               "lengthMenu": [[10, 15, 25, 50, -1], [10, 15, 25, 50, "All"]],
                // if I did not put the parseInt, it did not work for "All" which is the "-1"
                pageLength:parseInt(pageLength),
                "columns": [
                    { "data": "name" },
                    { "data": "position" },
                    { "data": "office" },
                    { "data": "extn" },
                    { "data": "start_date" },
                    { "data": "salary" }
                ], 
                dom: 'lfrtip'
             });
            // this id of the page length is the id of your table + '_length" soo...
            $('#example_length').on("change", 'select', function(){
                // for persistant data
                localStorage.setItem('example_length',$(this).val());
                // just for the session 
                sessionStorage.setItem('example_length',$(this).val());
            })
        });
    
  • rotaerczrotaercz Posts: 31Questions: 7Answers: 0

    Thank you! It works great! :smiley:

    Out of curiosity what is the "select" attribute for?

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    the select box itself does not have an id but its container div does.
    so that syntax will watch for any select boxes that change inside the example_length div. Since there is only one inside that container, it is a safe.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    here is a simple demonstration http://jsbin.com/zazulih/edit?html,js,output

  • rotaerczrotaercz Posts: 31Questions: 7Answers: 0

    Thank you for helping me out. Really appreciate it. :smile:

This discussion has been closed.