Skip to content

Commit

Permalink
chore(deps): update Slickgrid-React to v5.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Dec 14, 2024
1 parent e445629 commit b1c2682
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 2 deletions.
6 changes: 4 additions & 2 deletions bootstrap5-i18n-demo/src/examples/slickgrid/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import { useEffect } from 'react';
import { Link, Navigate, Route, Routes as BaseRoutes, useLocation } from 'react-router-dom';

import { NavBar } from '../../NavBar';
Expand Down Expand Up @@ -43,6 +43,7 @@ import Example39 from './Example39';
import Example40 from './Example40';
import Example41 from './Example41';
import Example42 from './Example42';
import Example43 from './Example43';

const routes: Array<{ path: string; route: string; component: any; title: string; }> = [
{ path: 'example1', route: '/example1', component: <Example1 />, title: '1- Basic Grid / 2 Grids' },
Expand All @@ -63,7 +64,7 @@ const routes: Array<{ path: string; route: string; component: any; title: string
{ path: 'example16', route: '/example16', component: <Example16 />, title: '16- Row Move Plugin' },
{ path: 'example17', route: '/example17', component: <Example17 />, title: '17- Remote Model' },
{ path: 'example18', route: '/example18', component: <Example18 />, title: '18- Draggable Grouping' },
{ path: 'example19', route: '/example19', component: <Example19 />, title: '19- Row Detail' },
{ path: 'example19', route: '/example19', component: <Example19 />, title: '19- Row Detail View' },
{ path: 'example20', route: '/example20', component: <Example20 />, title: '20- Pinned Columns/Rows' },
{ path: 'example21', route: '/example21', component: <Example21 />, title: '21- Grid AutoHeight (full height)' },
{ path: 'example22', route: '/example22', component: <Example22 />, title: '22- with Bootstrap Tabs' },
Expand All @@ -86,6 +87,7 @@ const routes: Array<{ path: string; route: string; component: any; title: string
{ path: 'example40', route: '/example40', component: <Example40 />, title: '40- Infinite Scroll from JSON data' },
{ path: 'example41', route: '/example41', component: <Example41 />, title: '41- Drag & Drop' },
{ path: 'example42', route: '/example42', component: <Example42 />, title: '42- Custom Pagination' },
{ path: 'example43', route: '/example43', component: <Example43 />, title: '43- Create Grid from CSV' },
];

export default function Routes() {
Expand Down
155 changes: 155 additions & 0 deletions bootstrap5-i18n-demo/src/examples/slickgrid/Example43.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { ExcelExportService } from '@slickgrid-universal/excel-export';
import { type Column, type GridOption, SlickgridReact, toCamelCase } from 'slickgrid-react';
import { useState } from 'react';

export default function Example43() {
const [gridCreated, setGridCreated] = useState(false);
const [gridOptions, setGridOptions] = useState<GridOption>();
const [columnDefinitions, setColumnDefinitions] = useState<Column[]>([]);
const [dataset, setDataset] = useState<any[]>([]);
const templateUrl = new URL('./data/users.csv', import.meta.url).href;
const [uploadFileRef, setUploadFileRef] = useState('');
const [showSubTitle, setShowSubTitle] = useState(true);

function destroyGrid() {
setGridCreated(false);
}

function handleFileImport(event: any) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e: any) => {
const content = e.target.result;
dynamicallyCreateGrid(content);
};
reader.readAsText(file);
}
}

function handleDefaultCsv() {
const staticDataCsv = `First Name,Last Name,Age,Type\nBob,Smith,33,Teacher\nJohn,Doe,20,Student\nJane,Doe,21,Student`;
dynamicallyCreateGrid(staticDataCsv);
setUploadFileRef('');
}

function dynamicallyCreateGrid(csvContent: string) {
// dispose of any previous grid before creating a new one
setGridCreated(false);

const dataRows = csvContent?.split('\n');
const colDefs: Column[] = [];
const outputData: 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);
colDefs.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[colDefs[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) {
outputData.push(dataEntryObj);
} else {
outputData.push({ ...dataEntryObj, id: rowIndex });
}
}
});

setGridOptions({
gridHeight: 300,
gridWidth: 800,
enableFiltering: true,
enableExcelExport: true,
externalResources: [new ExcelExportService()],
headerRowHeight: 35,
rowHeight: 33,
});

setDataset(outputData);
setColumnDefinitions(colDefs);
setGridCreated(true);
}

function toggleSubTitle() {
setShowSubTitle(!showSubTitle);
const action = !showSubTitle ? 'remove' : 'add';
document.querySelector('.subtitle')?.classList[action]('hidden');
}

return (
<div id="demo-container" className="container-fluid">
<h2>
Example 43: Dynamically Create Grid from CSV / Excel import
<span className="float-end font18">
see&nbsp;
<a target="_blank"
href="https://github.com/ghiscoding/slickgrid-react/blob/master/src/examples/slickgrid/Example43.tsx">
<span className="mdi mdi-link-variant"></span> code
</a>
</span>
<button
className="ms-2 btn btn-outline-secondary btn-sm btn-icon"
type="button"
data-test="toggle-subtitle"
onClick={() => toggleSubTitle()}
>
<span className="mdi mdi-information-outline" title="Toggle example sub-title details"></span>
</button>
</h2>

{showSubTitle && <div className="subtitle">
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.
</div>}

<div>A default CSV file can be download <a id="template-dl" href={templateUrl}>here</a>.</div>

<div className="d-flex mt-5 align-items-end">
<div className="file-upload" style={{ maxWidth: '300px' }}>
<label htmlFor="formFile" className="form-label">Choose a CSV file…</label>
<input className="form-control" type="file" data-test="file-upload-input" value={uploadFileRef} onChange={($event) => handleFileImport($event)} />
</div>
<span className="mx-3">or</span>
<div>
<button id="uploadBtn" data-test="static-data-btn" className="btn btn-outline-secondary" onClick={() => handleDefaultCsv()}>
Use default CSV data
</button>
<button className="btn btn-outline-secondary ms-1" onClick={() => destroyGrid()}>Destroy Grid</button>
</div>
</div>

<hr />

<div className="grid-container-zone">
{gridCreated &&
<SlickgridReact
gridId="grid43"
columnDefinitions={columnDefinitions}
gridOptions={gridOptions}
dataset={dataset}
/>}
</div>
</div >
)
}

0 comments on commit b1c2682

Please sign in to comment.