Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column sort doesn't work with get function #149

Closed
tcjr opened this issue Apr 27, 2012 · 3 comments
Closed

Column sort doesn't work with get function #149

tcjr opened this issue Apr 27, 2012 · 3 comments

Comments

@tcjr
Copy link

tcjr commented Apr 27, 2012

Given this data:

  var data = [
    { first: "Bob", last: "Barker", age: 89 },
    { first: "Vanna", last: "White", age: 55 },
    { first: "Pat", last: "Sajak", age: 65 }
  ]; 

And this columns definition:

  [
     { label: "Name", get: function(d){ return d.first + ' ' + d.last; } },
     { label: "Age", field: "age" }
  ]

The grid displays properly, but the "Name" column is not sortable. The asc/desc sort triangle flips, but the rows do not sort.

@ghost
Copy link

ghost commented May 25, 2012

The thing is, sorting happens against the data (or store, if one is involved), whereas get happens during the rendering process which is later in the chain of events. However, there's nothing preventing you from also specifying a field to your column definition with a custom getter, like so:

[
  { label: "Name", field: "first", get: function(d){ return d.first + ' ' + d.last; } },
  { label: "Age", field: "age" }
]

Sort directly correlates to columns' designated fields, so this will have the effect of sorting by first name when you click the Name column's header, even though your get function results in a more aggregated form of data being displayed. (Alternatively, if you'd like it to sort by last name instead, you could naturally specify field: "last" there instead.)

Sorry for the delay on this reply. Let us know if you have any other related questions; otherwise we can close this out. Thanks!

@tcjr
Copy link
Author

tcjr commented May 29, 2012

The data I used in the issue was just illustrative. The actual usage I have is more like this:

var data = [
    { name: "Bob", total: 10, correct: 7 },
    { name: "Vanna", total: 12, correct: 11 },
    { name: "Pat", total: 6, correct: 6 }
  ];
  [
    { label: "Name", field: "name" },
    { label: "Average", get: function(d){ return Math.round(d.correct/d.total) + "%"; } }
  ]

There is obviously no field I can specify to get the desired order. I am working around it by modifying the underlying data to have an additional column.

Perhaps the sort arrows shouldn't be present if the data cannot be sorted (that is, if a 'field' has not been specified in the column definition.)

@tcjr tcjr closed this as completed May 29, 2012
@ghost
Copy link

ghost commented May 29, 2012

Good point RE disabling sort for fieldless columns; I've fixed that in 4b55763.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant