dataeditor.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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(
  47. "Warning: column.type is undefined for column.title=" + column.title,
  48. );
  49. return {
  50. kind: GridCellKind.Text,
  51. data: value,
  52. displayData: column.type,
  53. };
  54. }
  55. }
  56. export function formatDataEditorCells(col, row, columns, data) {
  57. if (row < data.length && col < columns.length) {
  58. const column = getDEColumn(columns, col);
  59. const rowData = getDERow(data, row);
  60. const cellData = locateCell(rowData, column);
  61. return formatCell(cellData, column);
  62. }
  63. return { kind: GridCellKind.Loading };
  64. }