-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade to Slickgrid-Universal v5.11.0
- Loading branch information
1 parent
a1e9a7d
commit 0235ad2
Showing
7 changed files
with
286 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
First Name,Last Name,Age,User Type | ||
John,Doe,20,Student | ||
Bob,Smith,33,Assistant Teacher | ||
Jane,Doe,21,Student | ||
Robert,Ken,42,Teacher |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<h3 class="title is-3"> | ||
Example 31 - Dynamically Create Grid from CSV / Excel import | ||
<div class="subtitle code-link"> | ||
<span class="is-size-6">see</span> | ||
<a class="is-size-5" | ||
target="_blank" | ||
href="https://github.com/ghiscoding/slickgrid-universal/blob/master/examples/vite-demo-vanilla-bundle/src/examples/example31.ts"> | ||
<span class="mdi mdi-link-variant"></span> code | ||
</a> | ||
</div> | ||
</h3> | ||
|
||
<div> | ||
<h6 class="italic content example-details"> | ||
Allow creating a grid dynamically by importing an external CSV or Excel file. | ||
This script demo will read the CSV file and will consider the first row as the column header and create the column definitions accordingly, | ||
while the next few rows will be considered the dataset. | ||
Note that this example is demoing a CSV file import but in your application you could easily implemnt an Excel file uploading. | ||
</h6> | ||
|
||
<div> | ||
A default CSV file can be download <a id="template-dl">here</a>. | ||
</div> | ||
|
||
<div class="flex mt-5"> | ||
<div class="file no-margin"> | ||
<label class="file-label"> | ||
<input type="file" id="fileInput" data-test="file-upload-input" accept=".csv,.txt" class="file-input" /> | ||
<span class="file-cta"> | ||
<span class="file-icon"> | ||
<i class="mdi mdi-upload"></i> | ||
</span> | ||
<span class="file-label"> Choose a CSV file… </span> | ||
</span> | ||
</label> | ||
</div> | ||
<span class="mx-3">or</span> | ||
<div> | ||
<button id="uploadBtn" data-test="static-data-btn" class="button is-small"> | ||
Use default CSV data | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<hr> | ||
|
||
<div class="grid-container-zone"> | ||
</div> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { type Column, type GridOption, toCamelCase } from '@slickgrid-universal/common'; | ||
import { BindingEventService } from '@slickgrid-universal/binding'; | ||
import { ExcelExportService } from '@slickgrid-universal/excel-export'; | ||
import { Slicker, type SlickVanillaGridBundle } from '@slickgrid-universal/vanilla-bundle'; | ||
|
||
import { ExampleGridOptions } from './example-grid-options.js'; | ||
import './example04.scss'; | ||
|
||
export default class Example31 { | ||
staticDataCsv = `First Name,Last Name,Age,Type\nBob,Smith,33,Teacher\nJohn,Doe,20,Student\nJane,Doe,21,Student`; | ||
private _bindingEventService: BindingEventService; | ||
sgb: SlickVanillaGridBundle; | ||
|
||
constructor() { | ||
this._bindingEventService = new BindingEventService(); | ||
} | ||
|
||
attached() { | ||
const uploadInputElm = document.getElementById('fileInput') as HTMLInputElement; | ||
const staticBtnElm = document.getElementById('uploadBtn') as HTMLButtonElement; | ||
this._bindingEventService.bind(uploadInputElm, 'change', this.handleFileImport.bind(this)); | ||
this._bindingEventService.bind(staticBtnElm, 'click', () => this.dynamicallyCreateGrid(this.staticDataCsv)); | ||
|
||
const templateUrl = new URL('./data/users.csv', import.meta.url).href; | ||
(document.getElementById('template-dl') as HTMLAnchorElement).href = templateUrl; | ||
} | ||
|
||
dispose() { | ||
this.sgb?.dispose(); | ||
this._bindingEventService.unbindAll(); | ||
} | ||
|
||
handleFileImport(event: any) { | ||
const file = event.target.files[0]; | ||
if (file) { | ||
const reader = new FileReader(); | ||
reader.onload = (e: any) => { | ||
const content = e.target.result; | ||
this.dynamicallyCreateGrid(content); | ||
}; | ||
reader.readAsText(file); | ||
} | ||
} | ||
|
||
dynamicallyCreateGrid(csvContent: string) { | ||
// dispose of any previous grid before creating a new one | ||
this.sgb?.dispose(); | ||
|
||
const dataRows = csvContent?.split('\n'); | ||
const columnDefinitions: Column[] = []; | ||
const dataset: any[] = []; | ||
|
||
// create column definitions | ||
dataRows.forEach((dataRow, rowIndex) => { | ||
const cellValues = dataRow.split(','); | ||
const dataEntryObj: any = {}; | ||
|
||
if (rowIndex === 0) { | ||
// the 1st row is considered to be the header titles, we can create the column definitions from it | ||
for (const cellVal of cellValues) { | ||
const camelFieldName = toCamelCase(cellVal); | ||
columnDefinitions.push({ | ||
id: camelFieldName, | ||
name: cellVal, | ||
field: camelFieldName, | ||
filterable: true, | ||
sortable: true, | ||
}); | ||
} | ||
} else { | ||
// at this point all column defs were created and we can loop through them and | ||
// we can now start adding data as an object and then simply push it to the dataset array | ||
cellValues.forEach((cellVal, colIndex) => { | ||
dataEntryObj[columnDefinitions[colIndex].id] = cellVal; | ||
}); | ||
|
||
// a unique "id" must be provided, if not found then use the row index and push it to the dataset | ||
if ('id' in dataEntryObj) { | ||
dataset.push(dataEntryObj); | ||
} else { | ||
dataset.push({ ...dataEntryObj, id: rowIndex }); | ||
} | ||
} | ||
}); | ||
|
||
const gridOptions: GridOption = { | ||
gridHeight: 300, | ||
gridWidth: 800, | ||
enableFiltering: true, | ||
enableExcelExport: true, | ||
externalResources: [new ExcelExportService()], | ||
headerRowHeight: 35, | ||
rowHeight: 33, | ||
}; | ||
|
||
// for this use case, we need to recreate the grid container div because the previous grid.dispose() drops it | ||
const gridContainerElm = document.createElement('div'); | ||
gridContainerElm.className = 'grid31'; | ||
document.querySelector('.grid-container-zone')!.appendChild(gridContainerElm); | ||
this.sgb = new Slicker.GridBundle(gridContainerElm, columnDefinitions, { ...ExampleGridOptions, ...gridOptions }, dataset); | ||
} | ||
} |
Oops, something went wrong.