Skip to content

Commit

Permalink
Number rows from 1.
Browse files Browse the repository at this point in the history
Only allow sorting and filtering if complete set.
  • Loading branch information
jdunkerley committed Mar 14, 2023
1 parent 8f1ed77 commit a84b0e5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class TableVisualization extends Visualization {
this.dom.removeChild(this.dom.lastChild)
}

const style = ".ag-theme-alpine { --ag-grid-size: 3px; --ag-list-item-height: 20px; display: inline; }";
const style =
'.ag-theme-alpine { --ag-grid-size: 3px; --ag-list-item-height: 20px; display: inline; }'
const styleElem = document.createElement('style')
styleElem.innerHTML = style
this.dom.appendChild(styleElem)
Expand All @@ -120,52 +121,73 @@ class TableVisualization extends Visualization {
sortable: true,
filter: true,
resizable: true,
minWidth: 50
minWidth: 50,
},
enableRangeSelection: true
enableRangeSelection: true,
}
this.agGrid = new agGrid.Grid(tabElem, this.agGridOptions)
}

let parsedData = (typeof data === 'string') ? JSON.parse(data) : data
let parsedData = typeof data === 'string' ? JSON.parse(data) : data

let columnDefs = []
let rowData = undefined

if (parsedData.error !== undefined) {
this.agGridOptions.api.setColumnDefs([{
field: "error",
width: 1000,
cellStyle: {'white-space': 'normal'}
}])
this.agGridOptions.api.setRowData([{error: parsedData.error}])
this.agGridOptions.api.setColumnDefs([
{
field: 'error',
width: 1000,
cellStyle: { 'white-space': 'normal' },
},
])
this.agGridOptions.api.setRowData([{ error: parsedData.error }])
} else if (parsedData.json !== undefined && isMatrix(parsedData.json)) {
this.agGridOptions.api.setColumnDefs(parsedData.json.map((_, i) => ({field: i.toString()})))
const data = parsedData.json[0].map((_, i) => parsedData.json.map(r => toRender(r[i])))
this.agGridOptions.api.setRowData(data)
columnDefs = parsedData.json.map((_, i) => ({ field: i.toString() }))
rowData = parsedData.json[0].map((_, i) => parsedData.json.map(r => toRender(r[i])))
} else if (parsedData.json !== undefined && isObjectMatrix(parsedData.json)) {
let firstKeys = Object.keys(data[0])
this.agGridOptions.api.setColumnDefs(firstKeys.map((key) => ({field: key})))
const data = parsedData.json.map(obj => firstKeys.reduce((acc, key) => ({...acc, [key]: toRender(obj[key])}), {}))
this.agGridOptions.api.setRowData(data)
columnDefs = firstKeys.map(key => ({ field: key }))
rowData = parsedData.json.map(obj =>
firstKeys.reduce((acc, key) => ({ ...acc, [key]: toRender(obj[key]) }), {})
)
} else if (parsedData.json !== undefined && Array.isArray(parsedData.json)) {
const data = parsedData.json.map((row, i) => ({row: i+1, value: toRender(row)}))
this.agGridOptions.api.setColumnDefs([{field: "row"}, {field: "value"}])
this.agGridOptions.api.setRowData(data)
columnDefs = [{ field: 'row' }, { field: 'value' }]
rowData = parsedData.json.map((row, i) => ({ row: i + 1, value: toRender(row) }))
} else if (parsedData.json !== undefined) {
const data = [{ value: toRender(parsedData.json) }]
this.agGridOptions.api.setColumnDefs([{field: "value"}])
this.agGridOptions.api.setRowData(data)
columnDefs = [{ field: 'value' }]
rowData = [{ value: toRender(parsedData.json) }]
} else {
const headers = [...(parsedData.indices_header ? parsedData.indices_header : []), ...parsedData.header]
const rows = parsedData.data && parsedData.data.length > 0 ? parsedData.data[0].length : (parsedData.indices && parsedData.indices.length > 0 ? parsedData.indices[0].length : 0)
const data = Array.apply(null, Array(rows)).map((_, i) => {
const headers = [
...(parsedData.indices_header ? parsedData.indices_header : []),
...parsedData.header,
]
columnDefs = headers.map(h => ({ field: h }))

const rows =
parsedData.data && parsedData.data.length > 0
? parsedData.data[0].length
: parsedData.indices && parsedData.indices.length > 0
? parsedData.indices[0].length
: 0
rowData = Array.apply(null, Array(rows)).map((_, i) => {
const row = {}
const shift = parsedData.indices ? parsedData.indices.length : 0
headers.map((h, j) => row[h] = toRender(j < shift ? parsedData.indices[j][i] : parsedData.data[j - shift][i]))
headers.map(
(h, j) =>
(row[h] = toRender(
j < shift ? parsedData.indices[j][i] : parsedData.data[j - shift][i]
))
)
return row
})
this.agGridOptions.api.setColumnDefs(headers.map((h) => ({field: h})))
this.agGridOptions.api.setRowData(data)
}

const allData = parsedData.all_rows_count !== data.length
this.agGridOptions.defaultColDef.filter = allData
this.agGridOptions.defaultColDef.sortable = allData
this.agGridOptions.api.setColumnDefs(columnDefs)
this.agGridOptions.api.setRowData(rowData)
this.agGridOptions.api.sizeColumnsToFit()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ prepare_visualization y max_rows=1000 = Helpers.recover_errors <|
dataframe = x.take (First max_rows)
all_rows_count = x.row_count
included_rows = dataframe.row_count
index = Dataframe_Column.from_vector "#" (Vector.new included_rows i->i)
index = Dataframe_Column.from_vector "#" (Vector.new included_rows i->(i+1))

make_json dataframe [index] all_rows_count

Expand Down

0 comments on commit a84b0e5

Please sign in to comment.