lorenzofox3 / Smart-Table

Code source of Smart Table module: a table/grid for Angularjs
http://lorenzofox3.github.io/smart-table-website/
1.8k stars 513 forks source link

How to sort when more than one single property value is given pro column #775

Closed bvahdat closed 7 years ago

bvahdat commented 7 years ago

Using angularjs 1.5.9 assume two given properties such as foo and bar being bound to a single column. Is there any way to instruct st-sort to either sort according to the foo or bar values. That's something along the following lines:

<th st-sort="[firstName, lastName]">first name <br /> last name</th>
lorenzofox3 commented 7 years ago

you can use a getter to return the computed property you want to sort with

controller.js

//..
$scope.getters = {
  computed(row){
     return row.firstName + row.lastname; //(stoopid example but you get it :) )
  }

}
//
<th st-sort="getters.computed"> ... </th>
bvahdat commented 7 years ago

@lorenzofox3 sorry for getting back too late on this, too much running right now on my side. Your example is actually what the documentation is suggesting, which is fine. But the behaviour is not exactly what I'm after, as because state changes after each click are always in this order ascending => descending => primary order. Given my example with foo and bar properties above, this is what going on:

As because the state transition is always fixed given as ascending => descending => primary order, it doesn't matter which property I return from my function as the state transition order is always fixed.

I hope this makes it clear about what the issue is, otherwise I could provide a simple example on Codepen if you want me to do so.

lorenzofox3 commented 7 years ago

Not sure to fully understand. But an idea would be to create your own directive which keep track of the sorting state and call the table api directly with the sequence you wish. You can have a look at the stSort directive source code if you need an example