|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 6 | + <title>Tabulator Example — 04 Editing</title> |
| 7 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css" /> |
| 8 | + <link rel="stylesheet" href="../dist/css/tabulator.css" /> |
| 9 | + <script src="../dist/js/tabulator.js"></script> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | + <main> |
| 13 | + <h1>04 · Inline Editing & Validation</h1> |
| 14 | + <p> |
| 15 | + Double-click (or click) a cell to edit it. This shows the <code>input</code>, |
| 16 | + <code>number</code>, <code>list</code>, <code>star</code>, <code>tickCross</code> |
| 17 | + and the built-in <code>date</code> editors, plus validators. Edits are logged below. |
| 18 | + <a href="index.html">← back to index</a> |
| 19 | + </p> |
| 20 | + |
| 21 | + <div id="example-table"></div> |
| 22 | + |
| 23 | + <h2>Edit log</h2> |
| 24 | + <pre id="log" aria-live="polite">No edits yet — change a cell above.</pre> |
| 25 | + </main> |
| 26 | + |
| 27 | + <script> |
| 28 | + var tabledata = [ |
| 29 | + { id: 1, name: "Oli Bob", location: "United Kingdom", progress: 12, gender: "male", rating: 1, dob: "2005-08-14", driver: true }, |
| 30 | + { id: 2, name: "Mary May", location: "Germany", progress: 90, gender: "female", rating: 4, dob: "1989-02-01", driver: false }, |
| 31 | + { id: 3, name: "Chris L.", location: "France", progress: 42, gender: "male", rating: 0, dob: "1980-11-23", driver: true }, |
| 32 | + { id: 4, name: "Margret M.",location: "Sweden", progress: 16, gender: "female", rating: 5, dob: "1998-06-30", driver: true }, |
| 33 | + ]; |
| 34 | + |
| 35 | + var logEl = document.getElementById("log"); |
| 36 | + |
| 37 | + var table = new Tabulator("#example-table", { |
| 38 | + data: tabledata, |
| 39 | + layout: "fitColumns", |
| 40 | + columns: [ |
| 41 | + // Plain text input with a "required" validator. |
| 42 | + { title: "Name", field: "name", editor: "input", validator: "required" }, |
| 43 | + |
| 44 | + // List editor that auto-populates its options from existing column values. |
| 45 | + { title: "Location", field: "location", editor: "list", |
| 46 | + editorParams: { autocomplete: true, allowEmpty: true, listOnEmpty: true, valuesLookup: true } }, |
| 47 | + |
| 48 | + // Number editor constrained to 0-100, shown as a progress bar. |
| 49 | + { title: "Progress", field: "progress", sorter: "number", formatter: "progress", |
| 50 | + editor: "number", editorParams: { min: 0, max: 100 }, |
| 51 | + validator: ["min:0", "max:100"] }, |
| 52 | + |
| 53 | + // List editor with an explicit value map. |
| 54 | + { title: "Gender", field: "gender", editor: "list", |
| 55 | + editorParams: { values: { male: "Male", female: "Female", unknown: "Unknown" } } }, |
| 56 | + |
| 57 | + // Star editor — click a star to set the rating. |
| 58 | + { title: "Rating", field: "rating", formatter: "star", hozAlign: "center", |
| 59 | + width: 130, editor: "star" }, |
| 60 | + |
| 61 | + // Built-in date editor using a native <input type="date"> (no external libs). |
| 62 | + { title: "Date Of Birth", field: "dob", sorter: "date", hozAlign: "center", |
| 63 | + editor: "date" }, |
| 64 | + |
| 65 | + // Boolean toggled with a tick/cross editor. |
| 66 | + { title: "Driver", field: "driver", hozAlign: "center", formatter: "tickCross", |
| 67 | + editor: "tickCross" }, |
| 68 | + ], |
| 69 | + }); |
| 70 | + |
| 71 | + // In Tabulator 6 you bind event callbacks with table.on(...). |
| 72 | + // cellEdited fires after a cell is successfully edited. |
| 73 | + table.on("cellEdited", function (cell) { |
| 74 | + var msg = cell.getField() + " (row " + cell.getRow().getIndex() + "): " |
| 75 | + + JSON.stringify(cell.getOldValue()) + " → " + JSON.stringify(cell.getValue()); |
| 76 | + logEl.textContent = msg + "\n" + logEl.textContent; |
| 77 | + }); |
| 78 | + </script> |
| 79 | +</body> |
| 80 | +</html> |
0 commit comments