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.

Order of items not working

See original GitHub issue

I have seen multiple closed issues adressing this problem, but none of them seems to provide a solution. Is the “horizontalOrder” option broken?

I am loading an array of image objects from my database and using it to display the images and some other information.

HTML of the component:

<ngx-masonry [options]="masonryOptions">
    <div ngxMasonryItem class="wall-item" *ngFor="let image of images">
        <app-image-wall-post [username]="image.name" [message]="image.message" [imagePath]="image.path" [timestamp]="image.timestamp"></app-image-wall-post>
    </div>
</ngx-masonry>

The app-image-wall-post component contains a material card with the image in it.

TS of the component:

export class ImageWallComponent implements OnInit {
  images: ImageEntry[];

  public masonryOptions: NgxMasonryOptions = {
    horizontalOrder: true,
    originTop: true,
    originLeft: true,
    itemSelector: '.wall-item',
  };

  constructor(private wallService: WallService, private router: Router, private eventService:EventService) { }

  ngOnInit(): void {
    this.wallService.initImageStream();

    this.wallService.images.subscribe(newImages => {
      this.images = newImages.sort((a,b) => (a.timestamp > b.timestamp) ? -1 : ((b.timestamp > a.timestamp) ? 1 : 0));

      this.images.forEach(image => {
        console.log(new Date(image.timestamp) + ", " + image.name)
      })
    });
  }
}

The sort function in the subscribe callback makes sure that the images are sorted by their upload-timestamp. When checking the console output of the images array, the sorting seems fine. However, the masonry order is somehow random. Each time I refresh the page, the order changes.

Here is also a screenshot of the console output where you can see the properly sorted array, and the images. which are not ordered properly (see the date information on the card): image

Why is that, and what am I doing wrong? When I use a normal flexbox layout, the order is correct.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
wynfredcommented, May 17, 2020

@ifumi right, put the items to an array like pendingItems, and use an event from image item <img src="{{imageUrl}}" (load)="loaded.emit()" (error)="loaded.emit()">. When your masonry component gets the loaded event do this.cardItems.push(this.pendingItems.shift());

0reactions
wynfredcommented, Jun 15, 2020

@yingwei1025 yes, but you didn’t call it in onInit. You need to either initialize the cardItems with one item or call loaded when it’s initialized.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is order property not working on divs in block container?
The order property specifies the order of a flexible item relative to the rest of the flexible items inside the same container.
Read more >
Ordering flex items - CSS: Cascading Style Sheets | MDN
Items have a number showing their source order which has been rearranged. You can play around with the values in this live example...
Read more >
Product sorting order is not working correct - WordPress.org
In my WordPress dashboard, under “All products” menu item. I sorted products based on price in ASC Order. But this is how it...
Read more >
Pivot Table Sorting Fixes & Tips - Contextures
How to fix Excel pivot table sorting problems- new items at end, wrong items at top. Sort by column or row, custom order....
Read more >
Order Guide error "Order Guide has no items to order"
Group member(s) for the affected User Criteria is/are not set up correctly. You will notice that the group is associated with a sys_id...
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