reactive-table icon indicating copy to clipboard operation
reactive-table copied to clipboard

Reactively set what columns need to sort?

Open 0o-de-lally opened this issue 9 years ago • 3 comments

I'm looking through the docs. I can't see a way to set what columns need to sort from outside the table (and after render). I see there is a reactive var option. But I would prefer that sortOrder was a function, such that it could accept:

sortOrder: function() { ( if this.key === Session.get('whatKeyToSortOn' ) ) { return 0 }else { return 1} }

I see functions are not valid for that setting param. Any other ways of achieving this?

0o-de-lally avatar May 09 '16 18:05 0o-de-lally

If you don't need it to update when the Session variable changes, you can call the function and set sortOrder to its output.

But the ReactiveVar option is the only way to make it reactive.

You can set up the ReactiveVars for each sortOrder based on the value of the Session variable

I think it would be something like this:

Template.templateName.onCreated(function () {
  this.sortOrderVars = {};
  _.each(fieldKeys), function (key) {
    var sortOrder = new ReactiveVar(0);
    this.sortOrderVars[key] = sortOrder;
    this.autorun(function () {
      if (key === Session.get('whatKeyToSortOn' ) ) { sortOrder.set(0); }else { sortOrder.set(1);}
  });
});

// In table field settings
sortOrder: Template.instance().sortOrderVars[key]

aslagle avatar May 10 '16 02:05 aslagle

@aslagle I have tried a few variations on this. One issue I have ( which may well be very specific to my dataset) is that having any reactive vars in the table settings causes an unwanted chain reaction to recompute the dataset.

Ideally it would be great to have an api to update the table settings object dynamically. I don't know the internals of reactive-table, but perhaps you already have a reactive var that can be extended to include the settings obj.

0o-de-lally avatar May 26 '16 20:05 0o-de-lally

The internal settings object isn't a reactive var, it's a plain object that contains a bunch of reactive vars.

I agree having an API would be useful, but it would require a lot of refactoring.

aslagle avatar May 29 '16 15:05 aslagle