# GeeGrid Executable Examples

Language: [English](./index.en.md) | [Korean](./index.ko.md)  
API Reference: [/docs/api/](/docs/api/)  
Complete AI context: [/ai-en.txt](/ai-en.txt)

These examples use verified GeeGrid v3 conventions. Start with mock data, then connect the verified project API.

# Example 1: Complete object-row Grid

Save as an HTML file in a project that serves the GeeGrid v3 assets.

~~~html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>User 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="Enter one keyword per line"></textarea>
  <label>
    <input id="caseSensitiveChk" type="checkbox">
    Case sensitive
  </label>
  <button id="searchBtn" type="button">Search</button>

  <geegrid id="userGrid"
           width="100%"
           height="420px"
           numbering="true"
           pageable="true"
           rows="20"
           handler="load_users"
           sort_handler="sort_users">
    <column id="id"
            type="number"
            width="80px"
            align="right">ID</column>
    <column id="name"
            type="html"
            width="180px"
            sortable="true"
            orderBy="name">Name</column>
    <column id="department"
            type="html"
            width="180px"
            sortable="true"
            orderBy="department">Department</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: 'Alice Kim', department: 'Development' },
        { id: 2, name: 'Daniel Lee', department: 'Quality' }
      ];

      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 () {
        var query = new URLSearchParams({
          keywords: document.getElementById('keywordsText').value,
          caseSensitive:
            document.getElementById('caseSensitiveChk').checked ? '1' : '0'
        });
        var page = grid.makePageParams().replace(/^&/, '');

        if (new URLSearchParams(location.search).get('mock') !== '0') {
          applyResult({
            result: 'success',
            count: mockRows.length,
            page: 1,
            rows: 20,
            records: mockRows
          });
          return;
        }

        fetch('/api/users?' + query.toString() + '&' + page)
          .then(function (response) {
            return response.json();
          })
          .then(function (ret) {
            applyResult(ret);
          });
      });

      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>
~~~

Default behavior uses mock data. Add mock=0 to the query string only after replacing /api/users with the verified project endpoint.

# Example 2: Array rows

~~~html
<geegrid id="arrayGrid">
  <column id="id" type="number">ID</column>
  <column id="name" type="html">Name</column>
</geegrid>
~~~

~~~js
var arrayGrid = new GeeGrid('arrayGrid');

arrayGrid.setTotalCount(2);
arrayGrid.setCurrentPage(1);
arrayGrid.setViewLength(20);
arrayGrid.setArrayData(
  ['id', 'name'],
  [
    [1, 'Alice Kim'],
    [2, 'Daniel Lee']
  ]
);
arrayGrid.makePaging();
~~~

# Example 3: JSON records

~~~html
<geegrid id="statusGrid">
  <column id="id" type="number">ID</column>
  <column id="name" type="html">Name</column>
  <column id="status" type="html">Status</column>
</geegrid>
~~~

~~~js
var statusGrid = new GeeGrid('statusGrid');
var records = [
  { id: 1, name: 'Alice Kim', status: 'READY' }
];

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

# Validation rules

- Constructor id must match the DOM id.
- handler and sort_handler values must match names registered with GeeGrid.on.
- Callback attributes contain names only.
- Choose the setter by actual row shape.
- Apply total count, page, view length, data, and paging in that order.
- Test mock flow before real API integration.
- Do not invent API URLs, authentication headers, response fields, or internal Runtime behavior.

