본문으로 건너뛰기

Guides

Grid Types

MasonryInfiniteGrid

MasonryInfiniteGrid is a grid that stacks items with the same width as a stack of bricks. Adjust the width of all images to the same size, find the lowest height column, and insert a new item.

<div class="container"></div>
import { MasonryInfiniteGrid } from "@egjs/infinitegrid";

function getItems(nextGroupKey, count) {
const nextItems = [];

for (let i = 0; i < count; ++i) {
const num = nextGroupKey * count + i;
nextItems.push(`<div class="item">
<div class="thumbnail">
<img src="https://naver.github.io/egjs-infinitegrid/assets/image/${(num % 33) + 1}.jpg" alt="egjs" />
</div>
<div class="info">egjs ${num}</div>
</div>`);
}
return nextItems;
}
const ig = new MasonryInfiniteGrid(".container", {
gap: 5,
});

ig.on("requestAppend", (e) => {
const nextGroupKey = (+e.groupKey || 0) + 1;

ig.append(getItems(nextGroupKey, 10), nextGroupKey);
});
ig.renderItems();

JustifiedInfiniteGrid

'justified' is a printing term with the meaning that 'it fits in one row wide'. JustifiedGrid is a grid that the item is filled up on the basis of a line given a size.

  • If 'data-grid-inline-offset' or 'data-grid-content-offset' are set for item element, the ratio is maintained except for the offset value.
  • If 'data-grid-maintained-target' is set for an element whose ratio is to be maintained, the item is rendered while maintaining the ratio of the element.
<div class="container"></div>
import { JustifiedInfiniteGrid } from "@egjs/infinitegrid";

function getItems(nextGroupKey, count) {
const nextItems = [];

for (let i = 0; i < count; ++i) {
const num = nextGroupKey * count + i;
nextItems.push(`<div class="item">
<div class="thumbnail">
<img src="https://naver.github.io/egjs-infinitegrid/assets/image/${(num % 33) + 1}.jpg" alt="egjs" data-grid-maintained-target="true"/>
</div>
<div class="info">egjs ${num}</div>
</div>`);
}
return nextItems;
}
const ig = new JustifiedInfiniteGrid(".container", {
gap: 5,
});

ig.on("requestAppend", (e) => {
const nextGroupKey = (+e.groupKey || 0) + 1;

ig.append(getItems(nextGroupKey, 10), nextGroupKey);
});
ig.renderItems();

JustifiedInfiniteGrid (Stretch)

stretch is possible to basically break the proportion of the item and stretch the inline size to fill the container. If you set the sizeRange range narrowly, you can stretch well.

  • stretchRange: If -, +, or % are added as a string value, it is a relative value to the original size. If it is a number value, the stretch range can be set as an absolute value. If data-grid-min-stretch and data-grid-max-stretch are set in the Element or JSX of each item, they will be applied first.
  • passUnstretchRow: Items placed in the last row are not stretched and are drawn maintaining their proportions. When using InfiniteGrid, it is calculated and re-rendered as follows:
<div class="container"></div>
import { JustifiedInfiniteGrid } from "@egjs/infinitegrid";

function getItems(nextGroupKey, count) {
const nextItems = [];

for (let i = 0; i < count; ++i) {
const num = nextGroupKey * count + i;
nextItems.push(`<div class="item">
<div class="thumbnail">
<img src="https://naver.github.io/egjs-infinitegrid/assets/image/${(num % 33) + 1}.jpg" alt="egjs" />
</div>
<div class="info">egjs ${num}</div>
</div>`);
}
return nextItems;
}
const ig = new JustifiedInfiniteGrid(".container", {
gap: 5,
stretch: true,
passUnstretchRow: true,
sizeRange: [228,228],
stretchRange: [144,320],
});

ig.on("requestAppend", (e) => {
const nextGroupKey = (+e.groupKey || 0) + 1;

ig.append(getItems(nextGroupKey, 10), nextGroupKey);
});
ig.renderItems();

FrameInfiniteGrid

'Frame' is a printing term with the meaning that 'it fits in one row wide'. FrameGrid is a grid that the item is filled up on the basis of a line given a size.

<div class="container"></div>
import { FrameInfiniteGrid } from "@egjs/infinitegrid";

function getItems(nextGroupKey, count) {
const nextItems = [];

for (let i = 0; i < count; ++i) {
const num = nextGroupKey * count + i;
nextItems.push(`<div class="item">
<div class="thumbnail">
<img src="https://naver.github.io/egjs-infinitegrid/assets/image/${(num % 33) + 1}.jpg" alt="egjs" />
</div>
<div class="info">egjs ${num}</div>
</div>`);
}
return nextItems;
}
const ig = new FrameInfiniteGrid(".container", {
gap: 5,
frame: [[1,1,2,3,3],[1,1,4,4,5]],
});

ig.on("requestAppend", (e) => {
const nextGroupKey = (+e.groupKey || 0) + 1;

ig.append(getItems(nextGroupKey, 10), nextGroupKey);
});
ig.renderItems();

PackingInfiniteGrid

The PackingGrid is a grid that shows the important items bigger without sacrificing the weight of the items.

  • Rows and columns are separated so that items are dynamically placed within the horizontal and vertical space rather than arranged in an orderly fashion.
  • If sizeWeight is higher than ratioWeight, the size of items is preserved as much as possible.
  • Conversely, if ratioWeight is higher than sizeWeight, the ratio of items is preserved as much as possible.
<div class="container"></div>
import { PackingInfiniteGrid } from "@egjs/infinitegrid";

function getItems(nextGroupKey, count) {
const nextItems = [];

for (let i = 0; i < count; ++i) {
const num = nextGroupKey * count + i;
nextItems.push(`<div class="item">
<div class="thumbnail">
<img src="https://naver.github.io/egjs-infinitegrid/assets/image/${(num % 33) + 1}.jpg" alt="egjs" />
</div>
<div class="info">egjs ${num}</div>
</div>`);
}
return nextItems;
}
const ig = new PackingInfiniteGrid(".container", {
gap: 5,
});

ig.on("requestAppend", (e) => {
const nextGroupKey = (+e.groupKey || 0) + 1;

ig.append(getItems(nextGroupKey, 10), nextGroupKey);
});
ig.renderItems();

Insert Data

Through scrolling, when the scroll reaches the end, the requestAppend event is raised, and when it reaches the start, the requestPrepend event is raised. You can add data within this event.

  • You can set the key of an item through itemBy prop.
  • You can set the group's key through groupBy prop.

If the append method or prepend method is used, there is no need to use a separate key. You can set the groupKey through the second argument.

ig.on("requestAppend", e => {
const nextGroupKey = (+e.groupKey || 0) + 1;

ig.append(getItems(nextGroupKey, 10), nextGroupKey);
});

Wait Data Loading

Use wait & ready

If you want to add items asynchronously, call the e.wait function and when the data is ready call the e.ready function.

ig.on("requestAppend", e => {
const nextGroupKey = (+e.groupKey || 0) + 1;

e.wait();

setTimeout(() => {
e.ready();
ig.append(getItems(nextGroupKey, 10), nextGroupKey);
}, 1000);
});

Use Placeholder

You can add placeholders to show instead while data is being loaded/added. The placeholder is placed on the grid instead of the actual item and can be maintained until the actual item is added.

You can set a placeholder through the ig.setPlaceholder method.

ig.setPlaceholder({
html: `<div class="placeholder"></div>`,
});

ig.on("requestAppend", e => {
const nextGroupKey = (+e.groupKey || 0) + 1;

e.wait();
e.currentTarget.appendPlaceholders(5, nextGroupKey);
setTimeout(() => {
e.ready();
ig.append(getItems(nextGroupKey, 10), nextGroupKey);
}, 1000);
});