본문으로 건너뛰기Skip to content
예제 첫화면Examples home

오늘·최근 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.

활용Intermediate
직접 써 보기Live preview오늘·최근 7일·이번 달로 거르기Filter by today, last 7 days, this month

코드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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<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>
main.js
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));
example.js
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);
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<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>
main.js
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));
example.js
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);
  },
};