ngx-datatable: Default Sort Not Working / Not Documented.

I’m submitting a … (check one with “x”)

[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here

Current behavior

Default Sorting does not work Expected behavior

Have a way to be able to define which column to sort by at the start.

Reproduction of the problem

http://plnkr.co/edit/TLx2vYlCTLsqcLwvWafd is broken, if you can provide me a different plnkr I will post a repro What is the motivation / use case for changing the behavior?

Please tell us about your environment:

  • Table version: 0.7.x
  • Angular version: 2.0.x
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
  • Language: [all | TypeScript X.X | ES6/7 | ES5]

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 24 (2 by maintainers)

Commits related to this issue

Most upvoted comments

According to the commit from @marjan-georgiev it looks like its done like this:

<ngx-datatable [sorts]="[{prop: 'name', dir: 'desc'}]">...

default_sort

@yaotzin68 rather than waiting I did the sort myself with the help of underscore:

this.rows = _.sortBy(pages, function (o) {
          return parseInt(o['page_number']);
        });

This seems to be Fixed in 10.3.0. Thanks

I agree that this issue should be reopened. The workaround provided by @RicardoVaranda works fine if you’re doing everything client side, I however am handling limit on the server side via Postgres. For anyone else doing the same, I have been working around this by manually calling the changeSort method in my ngOnInit() here is an example:

list.html

<ngx-datatable
	[rows]="rows"
	[columnMode]="'force'"
	[headerHeight]="50"
	[footerHeight]="50"
	[rowHeight]="50"
	[scrollbarV]="true"
	[externalSorting]="true",
    [loadingIndicator]='showLoadingIndicator()',
    (sort)="changeSortSettings($event.column.prop, $event.newValue)">

list.component.ts

@Component({
   selector: 'my-list'
   // ...
})
export class ListComponent implements OnInit {
    private limit:number;
    private defaultSortColumnProp: string;
    private defaultSortDirection: string;
    private sortColumn: string;
    private sortDirection: string;

    //...

    ngOnInit() {
        //...
        this.changeSortSettings(this.defaultSortColumnProp, this.defaultSortDirection);
        //...
    }

    //...

    changeSortSettings(columnProp: string, direction: string) {
        this.sortColumn = columnProp;
        this.sortDirection = direction;

        // reset the page to 0 on sort
        this.page.pageNumber = 0;

        // make http get call to backend api. 
        //backend api makes postgres query with limit and sortby props
        this.rowsService.refreshRowsFromBackend(this.limit, this.sortColumn, this.sortDirection);
    }
};

hope that helps others until this issue is fixed.

@marjan-georgiev this issue still seems to exist, as as @lopadk notes, the issue can be seen on the demo page for default sorting: http://swimlane.github.io/ngx-datatable/#default-sorting. Can this issue be re-opened?

Works for me (9.3)

<ngx-datatable	[rows]="currentSolarState"
		[selectionType]="'single'"
		(select)='onSolarRowClick($event)'
		[headerHeight]="30"
		[footerHeight]="30"
		[rowHeight]="'20px'"
		[sorts]="[{prop: 'name', dir: 'desc'}]"
		class="material">

Same here, when page is loaded sorting indicator is visible, but items are not sorted.