Last active
January 14, 2025 09:28
-
-
Save archiewood/1cb564568247f2b9c87c7689eb794c12 to your computer and use it in GitHub Desktop.
AgGrid Evidence component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { onMount } from 'svelte'; | |
import { AllCommunityModule, ModuleRegistry, createGrid } from 'ag-grid-community'; | |
export let data = []; | |
ModuleRegistry.registerModules([AllCommunityModule]); | |
function calculateColumnWidth(data) { | |
return Object.keys(data).map(field => { | |
const value = data[field]; | |
if (value instanceof Date ) { | |
return { field, width: 120}; | |
} else { | |
const length = String(value).length; | |
return { field, width: Math.max(100, length * 10) }; | |
} | |
}); | |
} | |
let gridOptions; | |
onMount(() => { | |
gridOptions = { | |
rowData: data, | |
columnDefs: calculateColumnWidth(data[0]), | |
defaultColDef: { | |
resizable: true, | |
}, | |
rowHeight: 25, | |
}; | |
const myGridElement = document.querySelector('#myGrid'); | |
new createGrid(myGridElement, gridOptions); | |
}); | |
</script> | |
<div id="myGrid" class="ag-theme-alpine" style="height: 500px; width: 100%;"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment