Skip to content

Commit f7f1dab

Browse files
committed
better support for queryverse
1 parent 7e807a2 commit f7f1dab

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

src/TableView.jl

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,56 @@ using WebIO, JSExpr, JSON, Dates
44
using Observables: @map
55

66
function showtable(x; dark = false)
7+
iit = TableTraits.isiterabletable(x)
78
if Tables.istable(typeof(x))
89
return _showtable(x, dark)
9-
elseif TableTraits.isiterabletable(x)
10+
elseif ismissing(iit) || iit
1011
it = IteratorInterfaceExtensions.getiterator(x)
1112
return _showtable(Tables.DataValueUnwrapper(it), dark)
1213
end
1314
throw(ArgumentError("Argument is not a table."))
1415
end
1516

1617
function _showtable(table, dark)
17-
length(Tables.rows(table)) > 10_000 ? _showtable_async(table, dark) :
18-
_showtable_sync(table, dark)
19-
end
18+
tablelength = Base.IteratorSize(table) == Base.HasLength() ? length(Tables.rows(table)) : nothing
2019

21-
function _showtable_sync(table, dark)
22-
schema = Tables.schema(table)
23-
names = schema.names
24-
types = schema.types
2520
rows = Tables.rows(table)
26-
21+
schema = Tables.schema(table)
22+
if schema === nothing
23+
names = []
24+
types = []
25+
for (i, c) in enumerate(Tables.eachcolumn(first(rows)))
26+
push!(names, string(i))
27+
push!(types, typeof(c))
28+
end
29+
else
30+
names = schema.names
31+
types = schema.types
32+
end
2733
w = Scope(imports=["https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js",
2834
"https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css",
2935
"https://unpkg.com/ag-grid-community/dist/styles/ag-theme-balham$(dark ? "-dark" : "").css",])
3036

3137
coldefs = [(
32-
headerName = n,
33-
headerTooltip = types[i],
34-
field = n,
35-
type = types[i] <: Union{Missing, T where T <: Number} ? "numericColumn" : nothing,
36-
filter = types[i] <: Union{Missing, T where T <: Dates.Date} ? "agDateColumnFilter" :
37-
types[i] <: Union{Missing, T where T <: Number} ? "agNumberColumnFilter" : nothing
38+
headerName = n,
39+
headerTooltip = types[i],
40+
field = n,
41+
type = types[i] <: Union{Missing, T where T <: Number} ? "numericColumn" : nothing,
42+
filter = types[i] <: Union{Missing, T where T <: Dates.Date} ? "agDateColumnFilter" :
43+
types[i] <: Union{Missing, T where T <: Number} ? "agNumberColumnFilter" : nothing
3844
) for (i, n) in enumerate(names)]
3945

46+
tablelength === nothing || tablelength > 10_000 ? _showtable_async!(w, names, types, rows, coldefs, tablelength, dark) :
47+
_showtable_sync!(w, names, types, rows, coldefs, tablelength, dark)
48+
49+
w.dom = dom"div#grid"(className = "ag-theme-balham$(dark ? "-dark" : "")",
50+
style = Dict("width" => "100%",
51+
"min-width" => "400px",
52+
"height" => "800px"))
53+
w
54+
end
55+
56+
function _showtable_sync!(w, names, types, rows, coldefs, tablelength, dark)
4057
options = Dict(
4158
:rowData => table2json(rows, names, types),
4259
:columnDefs => coldefs,
@@ -47,39 +64,16 @@ function _showtable_sync(table, dark)
4764
)
4865

4966
handler = @js function (agGrid)
50-
gridOptions = $options
67+
@var gridOptions = $options
5168
gridOptions.rowData = JSON.parse(gridOptions.rowData)
5269
this.table = @new agGrid.Grid(this.dom.querySelector("#grid"), gridOptions)
5370
gridOptions.columnApi.autoSizeColumns($names)
5471
end
5572
onimport(w, handler)
56-
w.dom = dom"div#grid"(className = "ag-theme-balham$(dark ? "-dark" : "")",
57-
style=Dict(:width => "100%",
58-
"min-width" => "400px",
59-
:height => "800px"))
60-
w
6173
end
6274

6375

64-
function _showtable_async(table, dark)
65-
schema = Tables.schema(table)
66-
names = schema.names
67-
types = schema.types
68-
rows = Tables.rows(table)
69-
70-
w = Scope(imports=["https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js",
71-
"https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css",
72-
"https://unpkg.com/ag-grid-community/dist/styles/ag-theme-balham$(dark ? "-dark" : "").css",])
73-
74-
coldefs = [(
75-
headerName = n,
76-
headerTooltip = types[i],
77-
field = n,
78-
type = types[i] <: Union{Missing, T where T <: Number} ? "numericColumn" : nothing,
79-
filter = types[i] <: Union{Missing, T where T <: Dates.Date} ? "agDateColumnFilter" :
80-
types[i] <: Union{Missing, T where T <: Number} ? "agNumberColumnFilter" : nothing
81-
) for (i, n) in enumerate(names)]
82-
76+
function _showtable_async!(w, names, types, rows, coldefs, tablelength, dark)
8377
rowparams = Observable(w, "rowparams", Dict("startRow" => 1,
8478
"endRow" => 100,
8579
"successCallback" => @js v -> nothing))
@@ -89,7 +83,7 @@ function _showtable_async(table, dark)
8983
end
9084

9185
onjs(requestedrows, @js function (val)
92-
($rowparams[]).successCallback(JSON.parse(val), $(length(rows)))
86+
($rowparams[]).successCallback(JSON.parse(val), $(tablelength))
9387
end)
9488

9589
options = Dict(
@@ -108,22 +102,16 @@ function _showtable_async(table, dark)
108102
$rowparams[] = rowParams
109103
end
110104
,
111-
"rowCount" => length(rows)
105+
"rowCount" => tablelength
112106
)
113107
)
114108

115109
handler = @js function (agGrid)
116-
gridOptions = $options
117-
# gridOptions.rowData = JSON.parse(gridOptions.rowData)
110+
@var gridOptions = $options
118111
this.table = @new agGrid.Grid(this.dom.querySelector("#grid"), gridOptions)
119112
gridOptions.columnApi.autoSizeColumns($names)
120113
end
121114
onimport(w, handler)
122-
w.dom = dom"div#grid"(className = "ag-theme-balham$(dark ? "-dark" : "")",
123-
style=Dict(:width => "100%",
124-
"min-width" => "400px",
125-
:height => "800px"))
126-
w
127115
end
128116

129117
# directly write JSON instead of allocating temporary dicts etc

0 commit comments

Comments
 (0)