aggrid.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import "ag-grid-community";
  2. import { convertDynamicProperties } from "../../static/utils/dynamic_properties.js";
  3. export default {
  4. template: "<div></div>",
  5. mounted() {
  6. this.update_grid();
  7. },
  8. methods: {
  9. update_grid() {
  10. this.$el.textContent = "";
  11. this.gridOptions = { ...this.options };
  12. for (const column of this.html_columns) {
  13. if (this.gridOptions.columnDefs[column].cellRenderer === undefined) {
  14. this.gridOptions.columnDefs[column].cellRenderer = (params) => (params.value ? params.value : "");
  15. }
  16. }
  17. convertDynamicProperties(this.gridOptions, true);
  18. // Code for CheckboxRenderer https://blog.ag-grid.com/binding-boolean-values-to-checkboxes-in-ag-grid/
  19. function CheckboxRenderer() {}
  20. CheckboxRenderer.prototype.init = function (params) {
  21. this.params = params;
  22. this.eGui = document.createElement("input");
  23. this.eGui.type = "checkbox";
  24. this.eGui.checked = params.value;
  25. this.checkedHandler = this.checkedHandler.bind(this);
  26. this.eGui.addEventListener("click", this.checkedHandler);
  27. };
  28. CheckboxRenderer.prototype.checkedHandler = function (e) {
  29. let checked = e.target.checked;
  30. let colId = this.params.column.colId;
  31. this.params.node.setDataValue(colId, checked);
  32. };
  33. CheckboxRenderer.prototype.getGui = function (params) {
  34. return this.eGui;
  35. };
  36. CheckboxRenderer.prototype.destroy = function (params) {
  37. this.eGui.removeEventListener("click", this.checkedHandler);
  38. };
  39. this.gridOptions.components = {
  40. checkboxRenderer: CheckboxRenderer,
  41. };
  42. this.api = agGrid.createGrid(this.$el, this.gridOptions);
  43. this.api.addGlobalListener(this.handle_event);
  44. },
  45. run_grid_method(name, ...args) {
  46. return runMethod(this.api, name, args);
  47. },
  48. run_row_method(row_id, name, ...args) {
  49. return runMethod(this.api.getRowNode(row_id), name, args);
  50. },
  51. handle_event(type, args) {
  52. if ((type === "gridReady" || type === "gridSizeChanged") && this.auto_size_columns) {
  53. this.api.sizeColumnsToFit();
  54. }
  55. this.$emit(type, {
  56. value: args.value,
  57. oldValue: args.oldValue,
  58. newValue: args.newValue,
  59. context: args.context,
  60. rowIndex: args.rowIndex,
  61. data: args.data,
  62. toIndex: args.toIndex,
  63. firstRow: args.firstRow,
  64. lastRow: args.lastRow,
  65. clientWidth: args.clientWidth,
  66. clientHeight: args.clientHeight,
  67. started: args.started,
  68. finished: args.finished,
  69. direction: args.direction,
  70. top: args.top,
  71. left: args.left,
  72. animate: args.animate,
  73. keepRenderedRows: args.keepRenderedRows,
  74. newData: args.newData,
  75. newPage: args.newPage,
  76. source: args.source,
  77. visible: args.visible,
  78. pinned: args.pinned,
  79. filterInstance: args.filterInstance,
  80. rowPinned: args.rowPinned,
  81. forceBrowserFocus: args.forceBrowserFocus,
  82. colId: args.column?.colId,
  83. selected: args.node?.selected,
  84. rowHeight: args.node?.rowHeight,
  85. rowId: args.node?.id,
  86. });
  87. },
  88. },
  89. props: {
  90. options: Object,
  91. html_columns: Array,
  92. auto_size_columns: Boolean,
  93. },
  94. };