dataeditor.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { GridCellKind } from "@glideapps/glide-data-grid"
  2. export function getDEColumn(columns, col) {
  3. let c = columns[col];
  4. c.pos = col;
  5. return c;
  6. }
  7. export function getDERow(data, row) {
  8. return data[row];
  9. }
  10. export function locateCell(row, column) {
  11. if (Array.isArray(row)) {
  12. return row[column.pos];
  13. } else {
  14. return row[column.id];
  15. }
  16. }
  17. export function formatCell(value, column) {
  18. const editable = column.editable || true
  19. switch (column.type) {
  20. case "int":
  21. case "float":
  22. return {
  23. kind: GridCellKind.Number,
  24. data: value,
  25. displayData: value + "",
  26. readonly: !editable,
  27. allowOverlay: editable,
  28. }
  29. case "datetime":
  30. // value = moment format?
  31. case "str":
  32. return {
  33. kind: GridCellKind.Text,
  34. data: value,
  35. displayData: value,
  36. readonly: !editable,
  37. allowOverlay: editable,
  38. }
  39. case "bool":
  40. return {
  41. kind: GridCellKind.Boolean,
  42. data: value,
  43. readonly: !editable,
  44. }
  45. default:
  46. console.log("Warning: column.type is undefined for column.title=" + column.title)
  47. return {
  48. kind: GridCellKind.Text,
  49. data: value,
  50. displayData: column.type
  51. }
  52. };
  53. };
  54. export function formatDataEditorCells(col, row, columns, data) {
  55. if (row < data.length && col < columns.length) {
  56. const column = getDEColumn(columns, col);
  57. const rowData = getDERow(data, row);
  58. const cellData = locateCell(rowData, column);
  59. return formatCell(cellData, column);
  60. }
  61. return { kind: GridCellKind.Loading };
  62. }