UNPKG

@adaptabletools/adaptable

Version:

Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

82 lines (81 loc) 2.22 kB
export const prepareDataSource = (json, _file) => { if (!Array.isArray(json) || !json.length) { return { primaryKey: undefined, columns: [], data: [], }; } let columns = []; let data = []; let primaryKey; if (Array.isArray(json[0])) { columns = json[0]; for (let i = 1, len = json.length; i < len; i++) { const it = json[i]; const obj = {}; for (let j = 0; j < columns.length; j++) { obj[columns[j]] = it[j]; } data.push(obj); } } else { columns = Object.keys(json[0]); data = json; } primaryKey = columns[0]; return { primaryKey, columns, data, }; }; const typeToColType = { string: 'text', number: 'number', boolean: 'boolean', date: 'date', }; export const getColTypeFromValue = (value) => { const dataType = typeof value; let columnDataType = typeToColType[dataType] || typeToColType.string; if (value instanceof Date) { columnDataType = typeToColType.date; } if (Array.isArray(value) && value.length && typeof value[0] === 'number') { columnDataType = 'numberArray'; } return columnDataType; }; export const prepareGridOptions = (dataSourceInfo, defaultGridOptions) => { const firstItem = dataSourceInfo.data[0]; const columnDefs = dataSourceInfo.columns.map((columnName) => { const firstItemValue = firstItem[columnName]; const columnType = getColTypeFromValue(firstItemValue); return { headerName: columnName, field: columnName, type: columnType, filter: true, sortable: true, enableRowGroup: true, resizable: true, editable: true, }; }); const gridOptions = { ...defaultGridOptions, defaultColDef: { floatingFilter: true, }, rowData: dataSourceInfo.data, columnDefs, cellSelection: true, rowSelection: { mode: 'multiRow', }, rowHeight: 30, }; return gridOptions; };