# GeeGrid 실행 예제

언어: [한국어](./index.ko.md) | [English](./index.en.md)  
API 레퍼런스: [/docs/api/](/docs/api/)  
전체 AI 문서: [/ai-ko.txt](/ai-ko.txt)

다음 예제는 GeeGrid v3 실제 사용 규칙을 따릅니다. mock 데이터로 화면을 먼저 확인한 뒤 검증된 실제 API를 연결합니다.

# 예제 1: 객체 행 Grid

~~~html
<!doctype html>
<html lang="ko">
<head>
  <meta charset="utf-8">
  <title>사용자 Grid</title>
  <link rel="stylesheet" href="/js/geegrid_v3/geegrid.css">
  <link rel="stylesheet" href="/js/geegrid_v3/geegrid-template.css">
</head>
<body>
  <textarea id="keywordsText" rows="3"
            placeholder="한 줄에 하나씩 입력"></textarea>
  <button id="searchBtn" type="button">검색</button>

  <geegrid id="userGrid"
           pageable="true"
           rows="20"
           handler="load_users"
           sort_handler="sort_users">
    <column id="id" type="number" width="80px">ID</column>
    <column id="name" type="html" width="180px"
            sortable="true" orderBy="name">이름</column>
  </geegrid>

  <script src="/js/geegrid_v3/gee.common.js"></script>
  <script src="/js/geegrid_v3/gee.util.js"></script>
  <script src="/js/geegrid_v3/gee.grid.js"></script>
  <script src="/js/geegrid_v3/gee.object.js"></script>
  <script src="/js/geegrid_v3/gee.search.js"></script>

  <script>
    (function () {
      var grid = new GeeGrid('userGrid');
      var mockRows = [
        { id: 1, name: '홍길동' },
        { id: 2, name: '김철수' }
      ];

      function applyResult(ret) {
        var records = Array.isArray(ret.records) ? ret.records : [];

        grid.setTotalCount(Number(ret.count || records.length));
        grid.setCurrentPage(Number(ret.page || 1));
        grid.setViewLength(Number(ret.rows || 20));
        grid.setObjectData(records);
        grid.makePaging();
      }

      GeeGrid.on('load_users', function () {
        applyResult({
          result: 'success',
          count: mockRows.length,
          page: 1,
          rows: 20,
          records: mockRows
        });
      });

      GeeGrid.on('sort_users', function () {
        GeeGrid.invoke('load_users', {});
      });

      document.getElementById('searchBtn')
        .addEventListener('click', function () {
          GeeGrid.invoke('load_users', {});
        });

      GeeGrid.invoke('load_users', {});
    }());
  </script>
</body>
</html>
~~~

# 예제 2: 배열 행

~~~js
var grid = new GeeGrid('userGrid');

grid.setTotalCount(2);
grid.setCurrentPage(1);
grid.setViewLength(20);
grid.setArrayData(
  ['id', 'name'],
  [
    [1, '홍길동'],
    [2, '김철수']
  ]
);
grid.makePaging();
~~~

# 예제 3: JSON 레코드

~~~js
var records = [
  { id: 1, name: '홍길동', status: 'READY' }
];

grid.setTotalCount(records.length);
grid.setCurrentPage(1);
grid.setViewLength(20);
grid.setJsonData(records);
grid.makePaging();
~~~

검증 기준:

- 생성자 id와 DOM id가 같아야 합니다.
- handler와 sort_handler는 GeeGrid.on 등록 이름과 같아야 합니다.
- 콜백 속성에는 이름만 작성합니다.
- 실제 데이터 모양에 맞는 setter를 사용합니다.
- total count, page, view length, data, paging 순서를 유지합니다.
- 실제 API 전에 mock 흐름을 확인합니다.

