오늘·최근 7일·이번 달로 거르기Filter by today, last 7 days, this month
'오늘', '최근 7일', '이번 달' 같은 기간은 열 때의 오늘 날짜로 시작일을 계산해서 날짜 범위 필터로 바꿔 걸어요. 접수일 데이터도 오늘 기준으로 만들어서 언제 열어도 결과가 나와요.Periods like today, last 7 days or this month are turned into a date range from today's date. The sample dates are generated from today, so every period has rows whenever you open it.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<select aria-label="기간" id="period">
<option value="all" selected>전체</option>
<option value="today">오늘</option>
<option value="week">최근 7일</option>
<option value="month">이번 달</option>
<option value="days30">최근 30일</option>
</select>
</div>
<div id="grid" style="height: 640px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('period').addEventListener('change', (event) => actions.period(api, event.target.value));const titles = [
'견적 요청',
'배송 문의',
'환불 접수',
'사용법 문의',
'계정 잠금',
'결제 오류',
'세금계산서 요청',
'기능 제안',
];
const today = () => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days) => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// 접수일은 '오늘'을 기준으로 만들어서 언제 열어도 오늘·최근 7일 같은 기간에 행이 들어와요.
export const options = {
columns: [
{ id: 'title', title: '문의', width: 170 },
{
id: 'created',
title: '접수일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8],
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// 기간은 '시작일 ~ 오늘' 범위(between, 양 끝 포함)로 바꿔서 걸어요. 달력 날짜 기준이에요.
function from(period) {
const t = today();
if (period === 'today')
return t;
if (period === 'week')
return daysAgo(6);
if (period === 'month')
return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30')
return daysAgo(29);
return null;
}
export function setup(api, log) {
const off = api.on('filterChanged', () => log({
ko: `보이는 행 ${api.getVisibleRowCount()} / 전체 ${api.getRowCount()}`,
en: `Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`,
}));
return () => off();
}
export const actions = {
period: (api, value) => {
const start = from(value ?? 'all');
api.setFilterModel(start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null);
},
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>오늘·최근 7일·이번 달로 거르기</title>
</head>
<body>
<div class="toolbar">
<select aria-label="기간" id="period">
<option value="all" selected>전체</option>
<option value="today">오늘</option>
<option value="week">최근 7일</option>
<option value="month">이번 달</option>
<option value="days30">최근 30일</option>
</select>
</div>
<div id="grid" style="height: 640px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const titles = [
'견적 요청',
'배송 문의',
'환불 접수',
'사용법 문의',
'계정 잠금',
'결제 오류',
'세금계산서 요청',
'기능 제안',
];
const today = () => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days) => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// 접수일은 '오늘'을 기준으로 만들어서 언제 열어도 오늘·최근 7일 같은 기간에 행이 들어와요.
const options = {
columns: [
{ id: 'title', title: '문의', width: 170 },
{
id: 'created',
title: '접수일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8],
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// 기간은 '시작일 ~ 오늘' 범위(between, 양 끝 포함)로 바꿔서 걸어요. 달력 날짜 기준이에요.
function from(period) {
const t = today();
if (period === 'today')
return t;
if (period === 'week')
return daysAgo(6);
if (period === 'month')
return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30')
return daysAgo(29);
return null;
}
function setup(api, log) {
const off = api.on('filterChanged', () => log({
ko: `보이는 행 ${api.getVisibleRowCount()} / 전체 ${api.getRowCount()}`,
en: `Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`,
}));
return () => off();
}
const actions = {
period: (api, value) => {
const start = from(value ?? 'all');
api.setFilterModel(start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null);
},
};
options.ariaLabel = "오늘·최근 7일·이번 달로 거르기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('period').addEventListener('change', (event) => actions.period(api, event.target.value));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="기간"
disabled={!api}
onChange={(event) => api && actions.period(api, event.target.value)}
>
<option value="all">전체</option>
<option value="today">오늘</option>
<option value="week">최근 7일</option>
<option value="month">이번 달</option>
<option value="days30">최근 30일</option>
</select>
</div>
<Grid style={{ height: 640 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
created: Date;
}
const titles = [
'견적 요청',
'배송 문의',
'환불 접수',
'사용법 문의',
'계정 잠금',
'결제 오류',
'세금계산서 요청',
'기능 제안',
];
const today = (): Date => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days: number): Date => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d: Date): string =>
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// 접수일은 '오늘'을 기준으로 만들어서 언제 열어도 오늘·최근 7일 같은 기간에 행이 들어와요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '문의', width: 170 },
{
id: 'created',
title: '접수일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8] as string,
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// 기간은 '시작일 ~ 오늘' 범위(between, 양 끝 포함)로 바꿔서 걸어요. 달력 날짜 기준이에요.
function from(period: string): Date | null {
const t = today();
if (period === 'today') return t;
if (period === 'week') return daysAgo(6);
if (period === 'month') return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30') return daysAgo(29);
return null;
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('filterChanged', () =>
log({
ko: `보이는 행 ${api.getVisibleRowCount()} / 전체 ${api.getRowCount()}`,
en: `Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`,
}),
);
return () => off();
}
export const actions = {
period: (api: GridApi<Row>, value?: string) => {
const start = from(value ?? 'all');
api.setFilterModel(
start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null,
);
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="기간"
:disabled="!api"
@change="api && actions.period(api, ($event.target as HTMLSelectElement).value)"
>
<option value="all" selected>전체</option>
<option value="today">오늘</option>
<option value="week">최근 7일</option>
<option value="month">이번 달</option>
<option value="days30">최근 30일</option>
</select>
</div>
<Grid style="height: 640px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
created: Date;
}
const titles = [
'견적 요청',
'배송 문의',
'환불 접수',
'사용법 문의',
'계정 잠금',
'결제 오류',
'세금계산서 요청',
'기능 제안',
];
const today = (): Date => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days: number): Date => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d: Date): string =>
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// 접수일은 '오늘'을 기준으로 만들어서 언제 열어도 오늘·최근 7일 같은 기간에 행이 들어와요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '문의', width: 170 },
{
id: 'created',
title: '접수일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8] as string,
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// 기간은 '시작일 ~ 오늘' 범위(between, 양 끝 포함)로 바꿔서 걸어요. 달력 날짜 기준이에요.
function from(period: string): Date | null {
const t = today();
if (period === 'today') return t;
if (period === 'week') return daysAgo(6);
if (period === 'month') return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30') return daysAgo(29);
return null;
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('filterChanged', () =>
log({
ko: `보이는 행 ${api.getVisibleRowCount()} / 전체 ${api.getRowCount()}`,
en: `Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`,
}),
);
return () => off();
}
export const actions = {
period: (api: GridApi<Row>, value?: string) => {
const start = from(value ?? 'all');
api.setFilterModel(
start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null,
);
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="기간"
[disabled]="!api"
(change)="api && actions.period(api, $any($event.target).value)"
>
<option value="all" selected>전체</option>
<option value="today">오늘</option>
<option value="week">최근 7일</option>
<option value="month">이번 달</option>
<option value="days30">최근 30일</option>
</select>
</div>
<mx-grid style="height: 640px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
created: Date;
}
const titles = [
'견적 요청',
'배송 문의',
'환불 접수',
'사용법 문의',
'계정 잠금',
'결제 오류',
'세금계산서 요청',
'기능 제안',
];
const today = (): Date => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days: number): Date => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d: Date): string =>
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// 접수일은 '오늘'을 기준으로 만들어서 언제 열어도 오늘·최근 7일 같은 기간에 행이 들어와요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '문의', width: 170 },
{
id: 'created',
title: '접수일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8] as string,
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// 기간은 '시작일 ~ 오늘' 범위(between, 양 끝 포함)로 바꿔서 걸어요. 달력 날짜 기준이에요.
function from(period: string): Date | null {
const t = today();
if (period === 'today') return t;
if (period === 'week') return daysAgo(6);
if (period === 'month') return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30') return daysAgo(29);
return null;
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('filterChanged', () =>
log({
ko: `보이는 행 ${api.getVisibleRowCount()} / 전체 ${api.getRowCount()}`,
en: `Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`,
}),
);
return () => off();
}
export const actions = {
period: (api: GridApi<Row>, value?: string) => {
const start = from(value ?? 'all');
api.setFilterModel(
start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null,
);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<select aria-label="Period" id="period">
<option value="all" selected>All</option>
<option value="today">Today</option>
<option value="week">Last 7 days</option>
<option value="month">This month</option>
<option value="days30">Last 30 days</option>
</select>
</div>
<div id="grid" style="height: 640px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('period').addEventListener('change', (event) => actions.period(api, event.target.value));const titles = [
'Quote request',
'Shipping question',
'Refund request',
'How-to question',
'Account locked',
'Payment error',
'Invoice request',
'Feature suggestion',
];
const today = () => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days) => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// The received dates are built from "today", so whenever you open it, rows fall into periods such as Today and Last 7 days.
export const options = {
columns: [
{ id: 'title', title: 'Inquiry', width: 170 },
{
id: 'created',
title: 'Received',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8],
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// A period is applied as a "start date ~ today" range (between, ends included), compared as calendar dates.
function from(period) {
const t = today();
if (period === 'today')
return t;
if (period === 'week')
return daysAgo(6);
if (period === 'month')
return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30')
return daysAgo(29);
return null;
}
export function setup(api, log) {
const off = api.on('filterChanged', () => log(`Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`));
return () => off();
}
export const actions = {
period: (api, value) => {
const start = from(value ?? 'all');
api.setFilterModel(start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Filter by today, last 7 days, this month</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Period" id="period">
<option value="all" selected>All</option>
<option value="today">Today</option>
<option value="week">Last 7 days</option>
<option value="month">This month</option>
<option value="days30">Last 30 days</option>
</select>
</div>
<div id="grid" style="height: 640px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const titles = [
'Quote request',
'Shipping question',
'Refund request',
'How-to question',
'Account locked',
'Payment error',
'Invoice request',
'Feature suggestion',
];
const today = () => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days) => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// The received dates are built from "today", so whenever you open it, rows fall into periods such as Today and Last 7 days.
const options = {
columns: [
{ id: 'title', title: 'Inquiry', width: 170 },
{
id: 'created',
title: 'Received',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8],
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// A period is applied as a "start date ~ today" range (between, ends included), compared as calendar dates.
function from(period) {
const t = today();
if (period === 'today')
return t;
if (period === 'week')
return daysAgo(6);
if (period === 'month')
return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30')
return daysAgo(29);
return null;
}
function setup(api, log) {
const off = api.on('filterChanged', () => log(`Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`));
return () => off();
}
const actions = {
period: (api, value) => {
const start = from(value ?? 'all');
api.setFilterModel(start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null);
},
};
options.ariaLabel = "Filter by today, last 7 days, this month";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('period').addEventListener('change', (event) => actions.period(api, event.target.value));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Period"
disabled={!api}
onChange={(event) => api && actions.period(api, event.target.value)}
>
<option value="all">All</option>
<option value="today">Today</option>
<option value="week">Last 7 days</option>
<option value="month">This month</option>
<option value="days30">Last 30 days</option>
</select>
</div>
<Grid style={{ height: 640 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
title: string;
created: Date;
}
const titles = [
'Quote request',
'Shipping question',
'Refund request',
'How-to question',
'Account locked',
'Payment error',
'Invoice request',
'Feature suggestion',
];
const today = (): Date => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days: number): Date => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d: Date): string =>
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// The received dates are built from "today", so whenever you open it, rows fall into periods such as Today and Last 7 days.
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Inquiry', width: 170 },
{
id: 'created',
title: 'Received',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8] as string,
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// A period is applied as a "start date ~ today" range (between, ends included), compared as calendar dates.
function from(period: string): Date | null {
const t = today();
if (period === 'today') return t;
if (period === 'week') return daysAgo(6);
if (period === 'month') return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30') return daysAgo(29);
return null;
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('filterChanged', () =>
log(`Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`),
);
return () => off();
}
export const actions = {
period: (api: GridApi<Row>, value?: string) => {
const start = from(value ?? 'all');
api.setFilterModel(
start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null,
);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Period"
:disabled="!api"
@change="api && actions.period(api, ($event.target as HTMLSelectElement).value)"
>
<option value="all" selected>All</option>
<option value="today">Today</option>
<option value="week">Last 7 days</option>
<option value="month">This month</option>
<option value="days30">Last 30 days</option>
</select>
</div>
<Grid style="height: 640px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
title: string;
created: Date;
}
const titles = [
'Quote request',
'Shipping question',
'Refund request',
'How-to question',
'Account locked',
'Payment error',
'Invoice request',
'Feature suggestion',
];
const today = (): Date => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days: number): Date => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d: Date): string =>
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// The received dates are built from "today", so whenever you open it, rows fall into periods such as Today and Last 7 days.
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Inquiry', width: 170 },
{
id: 'created',
title: 'Received',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8] as string,
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// A period is applied as a "start date ~ today" range (between, ends included), compared as calendar dates.
function from(period: string): Date | null {
const t = today();
if (period === 'today') return t;
if (period === 'week') return daysAgo(6);
if (period === 'month') return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30') return daysAgo(29);
return null;
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('filterChanged', () =>
log(`Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`),
);
return () => off();
}
export const actions = {
period: (api: GridApi<Row>, value?: string) => {
const start = from(value ?? 'all');
api.setFilterModel(
start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null,
);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Period"
[disabled]="!api"
(change)="api && actions.period(api, $any($event.target).value)"
>
<option value="all" selected>All</option>
<option value="today">Today</option>
<option value="week">Last 7 days</option>
<option value="month">This month</option>
<option value="days30">Last 30 days</option>
</select>
</div>
<mx-grid style="height: 640px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
title: string;
created: Date;
}
const titles = [
'Quote request',
'Shipping question',
'Refund request',
'How-to question',
'Account locked',
'Payment error',
'Invoice request',
'Feature suggestion',
];
const today = (): Date => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
const daysAgo = (days: number): Date => {
const d = today();
d.setDate(d.getDate() - days);
return d;
};
const text = (d: Date): string =>
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
// The received dates are built from "today", so whenever you open it, rows fall into periods such as Today and Last 7 days.
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Inquiry', width: 170 },
{
id: 'created',
title: 'Received',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? text(value) : ''),
},
],
rows: Array.from({ length: 18 }, (_, i) => ({
id: i + 1,
title: titles[i % 8] as string,
created: daysAgo(i === 0 ? 0 : i * 2 + (i % 2)),
})),
rowKey: 'id',
};
// A period is applied as a "start date ~ today" range (between, ends included), compared as calendar dates.
function from(period: string): Date | null {
const t = today();
if (period === 'today') return t;
if (period === 'week') return daysAgo(6);
if (period === 'month') return new Date(t.getFullYear(), t.getMonth(), 1);
if (period === 'days30') return daysAgo(29);
return null;
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const off = api.on('filterChanged', () =>
log(`Visible rows ${api.getVisibleRowCount()} / total ${api.getRowCount()}`),
);
return () => off();
}
export const actions = {
period: (api: GridApi<Row>, value?: string) => {
const start = from(value ?? 'all');
api.setFilterModel(
start
? {
match: 'all',
items: [{ columnId: 'created', operator: 'between', value: [start, today()] }],
}
: null,
);
},
};