Skip to content

Commit

Permalink
feat: upgrade to Slickgrid-Universal v5.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Dec 14, 2024
1 parent a1e9a7d commit 0235ad2
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 117 deletions.
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
"@fnando/sparkline": "^0.3.10",
"@formkit/tempo": "^0.1.2",
"@slickgrid-universal/binding": "^5.10.2",
"@slickgrid-universal/common": "^5.10.2",
"@slickgrid-universal/composite-editor-component": "^5.10.2",
"@slickgrid-universal/custom-tooltip-plugin": "^5.10.2",
"@slickgrid-universal/excel-export": "^5.10.2",
"@slickgrid-universal/graphql": "^5.10.2",
"@slickgrid-universal/odata": "^5.10.2",
"@slickgrid-universal/row-detail-view-plugin": "^5.10.2",
"@slickgrid-universal/rxjs-observable": "^5.10.2",
"@slickgrid-universal/text-export": "^5.10.2",
"@slickgrid-universal/vanilla-bundle": "^5.10.2",
"@slickgrid-universal/vanilla-force-bundle": "^5.10.2",
"@slickgrid-universal/common": "^5.11.0",
"@slickgrid-universal/composite-editor-component": "^5.11.0",
"@slickgrid-universal/custom-tooltip-plugin": "^5.11.0",
"@slickgrid-universal/excel-export": "^5.11.0",
"@slickgrid-universal/graphql": "^5.11.0",
"@slickgrid-universal/odata": "^5.11.0",
"@slickgrid-universal/row-detail-view-plugin": "^5.11.0",
"@slickgrid-universal/rxjs-observable": "^5.11.0",
"@slickgrid-universal/text-export": "^5.11.0",
"@slickgrid-universal/vanilla-bundle": "^5.11.0",
"@slickgrid-universal/vanilla-force-bundle": "^5.11.0",
"bulma": "^1.0.2",
"dompurify": "^3.2.2",
"dompurify": "^3.2.3",
"moment-mini": "^2.29.4",
"rxjs": "^7.8.1",
"whatwg-fetch": "^3.6.20"
Expand All @@ -33,10 +33,10 @@
"@rollup/plugin-dynamic-import-vars": "^2.1.5",
"@types/fnando__sparkline": "^0.3.7",
"@types/moment": "^2.13.0",
"@types/node": "^22.10.1",
"@types/node": "^22.10.2",
"@types/whatwg-fetch": "^0.0.33",
"sass": "^1.82.0",
"sass": "^1.83.0",
"typescript": "^5.7.2",
"vite": "^6.0.2"
"vite": "^6.0.3"
}
}
2 changes: 2 additions & 0 deletions src/app-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Example27 from './examples/example27';
import Example28 from './examples/example28';
import Example29 from './examples/example29';
import Example30 from './examples/example30';
import Example31 from './examples/example31';

export class AppRouting {
constructor(private config: RouterConfig) {
Expand Down Expand Up @@ -65,6 +66,7 @@ export class AppRouting {
{ route: 'example28', name: 'example28', view: './examples/example28.html', viewModel: Example28, title: 'Example28', },
{ route: 'example29', name: 'example29', view: './examples/example29.html', viewModel: Example29, title: 'Example29', },
{ route: 'example30', name: 'example30', view: './examples/example30.html', viewModel: Example30, title: 'Example30', },
{ route: 'example31', name: 'example31', view: './examples/example31.html', viewModel: Example31, title: 'Example31', },
{ route: '', redirect: 'example01' },
{ route: '**', redirect: 'example01' }
];
Expand Down
11 changes: 11 additions & 0 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ <h4 class="is-size-4 has-text-white">Slickgrid-Universal</h4>
</a>
</div>
</div>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
31-45
</a>

<div class="navbar-dropdown">
<a class="navbar-item" onclick.delegate="loadRoute('example31')">
Example31 - Create Grid from CSV
</a>
</div>
</div>
</div>
</div>
</nav>
Expand Down
5 changes: 5 additions & 0 deletions src/examples/data/users.csv
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
49 changes: 49 additions & 0 deletions src/examples/example31.html
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>
102 changes: 102 additions & 0 deletions src/examples/example31.ts
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);
}
}
Loading

0 comments on commit 0235ad2

Please sign in to comment.