javve / list.js

The perfect library for adding search, sort, filters and flexibility to tables, lists and various HTML elements. Built to be invisible and work on existing HTML.
https://listjs.com
MIT License
11.2k stars 896 forks source link

Sorting by column working only in 1st page but not in 2nd page with pagination #659

Open Faizanq opened 5 years ago

Faizanq commented 5 years ago

Sorting not persistent when doing pagination it remains its actual state when going to other pages

d4mation commented 4 years ago

In my case, this ended up being there were tab characters and such messing up the sorting function.

I was able to correct it like this. The jQuery use is a little unnecessary in my example, but I was doing additional transformations in my specific case that I've left out of the below.

This is pretty much an exact copy of what list.js does for its normal sorting, but ensures that tab characters and any additional spacing is ignored.

// Webpack will recognize the copy of this included by list.js and will hardly impact your compiled JS size when including it again
var naturalSort = require( 'string-natural-compare' );

let list = new List(
    htmlElement,
    {
        valueNames: [
            'column1',
            'column2'
        ],
        page: 15,
        pagination: true,
        listClass: 'someCSSClass',
        sortFunction: function( itemA, itemB, options ) {

            var sort = naturalSort;
            sort.alphabet = options.alphabet || undefined;

            if ( ! sort.alphabet && options.insensitive) {
                sort = naturalSort.caseInsensitive;
            }

            var $itemA = $( $.parseHTML( itemA.values()[ options.valueName ] ) ),
                $itemB = $( $.parseHTML( itemB.values()[ options.valueName ] ) );

            return sort( $itemA.text().trim(), $itemB.text().trim() );

        }
    }
);