Search code examples
jquery-uiprime-ui

How to sort prime-ui datatable, based on more than one column


I want to sort the datatable based on 2 columns. If I use the following property,

{sortField: 'ColumnHeader'}

Its not working.


Solution

  • It will not work with the current primeui (at the time of this answer it is 1.1). Have a look at the sort function:

        sort: function(field, order) {
            if(this.options.selectionMode) {
                this.selection = [];
            }
    
            if(this.options.lazy) {
                this.options.datasource.call(this, this._onLazyLoad, this._createStateMeta());
            }
            else {
                this.data.sort(function(data1, data2) {
                    var value1 = data1[field],
                    value2 = data2[field],
                    result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0;
    
                    return (order * result);
                });
    
                if(this.options.selectionMode) {
                    this.selection = [];
                }
    
                if(this.paginator) {
                    this.paginator.puipaginator('option', 'page', 0);
                }
    
                this._renderData();
            }
        },
    

    As you can see it uses the Array.prototype.sort() function and accesses the field-to

    var value1 = data1[field],
    value2 = data2[field],
    

    Maybe you can override this particular function and use your own sort method instead.