question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async load collection for editors

See original GitHub issue

Hello, I have next workflow: Getting data for collection is deferred. How can I organize properly load of grid?

next tries didn’t work: (I tried to re-init grid after filling collection)

 this.dataService.getStatuses().subscribe(data => {
       this.columns[0].editor.collection = data.map(item => ({ label: item.desc, value: item.id }))
       // this.grid.gridService.resetGrid(this.columns);
       this.grid.gridService.init(this.grid.slickGrid, this.dataset);
 }, this.handleError);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
ghiscodingcommented, Jul 17, 2018

@asdman384 @YadavAvinashK I started some work to address this, it’s a work in progress but I have the proof of concept working (not entirely tested though). You can provide feedback now before I push the code. I currently only worked on the Filter but the Editor will behave exactly the same.

Step 1 - use a new asyncCollection property

I created a new property named asyncCollection which accepts an Observable/Subject, so typically you would simply pass an HttpClient Observable. Also note that when you use asyncCollection, the other collection property can be omitted (however note the library itself will still use the collection property). So the code could look like the following

this.columnDefinitions = [
{ 
  id: 'duration', name: 'Duration', field: 'duration', 
  filter: {
    asyncCollection: this.http.get('/api/durations'),
    model: Filters.multipleSelect,

    // add a collection sort if you wish, for demo let's sort DESC
    collectionSortBy: {
        property: 'value',
        sortDesc: true,
        fieldType: FieldType.number
    },
  }
}

Step 2 - how to refresh the Filter / Editor collection?

If you want to refresh or change the collection used by the Filter or Editor, you can still use the collection which will always contain the latest array used by the Filter/Editor (basically every time the async resolves, the lib updates the collection property). However, there’s a slight problem, I could not find a way to simply watch the collection (like $watchCollection in AngularJS), so in order for the lib to know there’s a change, the only way I found is by triggering a next on the Observable. So the code looks like the following

  addItem() {
    // add a new row to the grid
    const lastRowIndex = this.dataset.length;
    const newRows = this.mockData(1, lastRowIndex);
    this.angularGrid.gridService.addItemToDatagrid(newRows[0]);

    // refresh the MultipleSelect Filter of the "Duration" column
    const durationColumnDef = this.columnDefinitions.find((column: Column) => column.id === 'duration');
    if (durationColumnDef) {
      const asyncCollection = durationColumnDef.filter.asyncCollection;
      const collection = durationColumnDef.filter.collection;
      if (Array.isArray(collection)) {
        // you can use the "collection" to know what was the last array used by the Filter/Editor
        // so if we add the new value to the collection
        collection.push({ value: lastRowIndex, label: lastRowIndex });

        // trigger a change for the Observable/Subject of the async collection
        // I wish that I could get rid of this line and only watch the "collection" change
        // but for now, that is the way I got it to work
        if (asyncCollection instanceof Subject) {
          asyncCollection.next(collection);
        }
      }
    }
  }

If someone knows how to do the same as $watchCollection like we had in AngularJS, please let me know and provide a solution on the SO question, which got down voted 😦 I would really like to get rid of the extra asyncCollection.next(collection) call.

Also note that I did the proof of concept only locally with setTimeout and a Subject which is not an HttpClient call but it should work the same, see below for a quick demo. Below, I’m adding 2 new rows which will add 2 new options to the filter (500 & 501).

2018-07-17_19-46-35

0reactions
ghiscodingcommented, Aug 14, 2018

This feature is now available in version 1.4.0

If you haven’t yet voted, please do ⭐️ Cheers

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to async loading scene in editor? - Unity Forum
hello. I want async loading scene in editor .. Because, in my project, it is a project that creates only scenes without using...
Read more >
Async loaded .NET projects may impact Visual Studio ...
NET projects may impact Visual Studio extensions. In Visual Studio 2019 version 16.3, the CSProj project system (C#/VB non-SDK style) ...
Read more >
How To Handle Async Data Loading, Lazy Loading, and Code ...
Step 1 — Loading Asynchronous Data with useEffect. In this step, you'll use the useEffect Hook to load asynchronous data into a sample ......
Read more >
Use timing changes to load elements on the page ... - Optimizely
Visual Editor · From the Experiments tab, click Variations. · Select the variation you want to load asynchronously. · Under Changes, select the ......
Read more >
Asynchronous Asset Loading - Unreal Engine Documentation
In this example, ItemList is a TArray< TSoftObjectPtr<UGameItem> > that was modified by designers in the editor. The code iterates that list, converts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found