@@ -4,39 +4,56 @@ using WebIO, JSExpr, JSON, Dates
4
4
using Observables: @map
5
5
6
6
function showtable (x; dark = false )
7
+ iit = TableTraits. isiterabletable (x)
7
8
if Tables. istable (typeof (x))
8
9
return _showtable (x, dark)
9
- elseif TableTraits . isiterabletable (x)
10
+ elseif ismissing (iit) || iit
10
11
it = IteratorInterfaceExtensions. getiterator (x)
11
12
return _showtable (Tables. DataValueUnwrapper (it), dark)
12
13
end
13
14
throw (ArgumentError (" Argument is not a table." ))
14
15
end
15
16
16
17
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
20
19
21
- function _showtable_sync (table, dark)
22
- schema = Tables. schema (table)
23
- names = schema. names
24
- types = schema. types
25
20
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
27
33
w = Scope (imports= [" https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js" ,
28
34
" https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css" ,
29
35
" https://unpkg.com/ag-grid-community/dist/styles/ag-theme-balham$(dark ? " -dark" : " " ) .css" ,])
30
36
31
37
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
38
44
) for (i, n) in enumerate (names)]
39
45
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)
40
57
options = Dict (
41
58
:rowData => table2json (rows, names, types),
42
59
:columnDefs => coldefs,
@@ -47,39 +64,16 @@ function _showtable_sync(table, dark)
47
64
)
48
65
49
66
handler = @js function (agGrid)
50
- gridOptions = $ options
67
+ @var gridOptions = $ options
51
68
gridOptions. rowData = JSON. parse (gridOptions. rowData)
52
69
this. table = @new agGrid. Grid (this. dom. querySelector (" #grid" ), gridOptions)
53
70
gridOptions. columnApi. autoSizeColumns ($ names)
54
71
end
55
72
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
61
73
end
62
74
63
75
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)
83
77
rowparams = Observable (w, " rowparams" , Dict (" startRow" => 1 ,
84
78
" endRow" => 100 ,
85
79
" successCallback" => @js v -> nothing ))
@@ -89,7 +83,7 @@ function _showtable_async(table, dark)
89
83
end
90
84
91
85
onjs (requestedrows, @js function (val)
92
- ($ rowparams[]). successCallback (JSON. parse (val), $ (length (rows) ))
86
+ ($ rowparams[]). successCallback (JSON. parse (val), $ (tablelength ))
93
87
end )
94
88
95
89
options = Dict (
@@ -108,22 +102,16 @@ function _showtable_async(table, dark)
108
102
$ rowparams[] = rowParams
109
103
end
110
104
,
111
- " rowCount" => length (rows)
105
+ " rowCount" => tablelength
112
106
)
113
107
)
114
108
115
109
handler = @js function (agGrid)
116
- gridOptions = $ options
117
- # gridOptions.rowData = JSON.parse(gridOptions.rowData)
110
+ @var gridOptions = $ options
118
111
this. table = @new agGrid. Grid (this. dom. querySelector (" #grid" ), gridOptions)
119
112
gridOptions. columnApi. autoSizeColumns ($ names)
120
113
end
121
114
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
127
115
end
128
116
129
117
# directly write JSON instead of allocating temporary dicts etc
0 commit comments