Skip to content

Commit 779a0ac

Browse files
committed
support TableTraits interface
also show coltype as tooltip
1 parent 06ef085 commit 779a0ac

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

REQUIRE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ WebIO
33
JSExpr
44
Tables
55
JSON
6+
TableTraits
7+
IteratorInterfaceExtensions

src/TableView.jl

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
module TableView
2-
using Tables, WebIO, JSExpr, JSON, Dates
2+
using Tables, TableTraits, IteratorInterfaceExtensions
3+
using WebIO, JSExpr, JSON, Dates
34

45
function showtable(x; dark = false)
5-
w = Scope(imports=["https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js",
6-
"https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css",
7-
"https://unpkg.com/ag-grid-community/dist/styles/ag-theme-balham$(dark ? "-dark" : "").css",])
6+
if Tables.istable(typeof(x))
7+
return _showtable(x, dark)
8+
elseif TableTraits.isiterabletable(x)
9+
it = IteratorInterfaceExtensions.getiterator(x)
10+
return _showtable(Tables.DataValueUnwrapper(it), dark)
11+
end
12+
throw(ArgumentError("Argument is not a table."))
13+
end
814

9-
schema = Tables.schema(x)
15+
function _showtable(table, dark)
16+
schema = Tables.schema(table)
1017
names = schema.names
1118
types = schema.types
19+
rows = Tables.rows(table)
20+
21+
w = Scope(imports=["https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js",
22+
"https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css",
23+
"https://unpkg.com/ag-grid-community/dist/styles/ag-theme-balham$(dark ? "-dark" : "").css",])
1224

1325
coldefs = [(
1426
headerName = n,
27+
headerTooltip = types[i],
1528
field = n,
1629
type = types[i] <: Union{Missing, T where T <: Number} ? "numericColumn" : nothing,
1730
filter = types[i] <: Union{Missing, T where T <: Dates.Date} ? "agDateColumnFilter" :
1831
types[i] <: Union{Missing, T where T <: Number} ? "agNumberColumnFilter" : nothing
1932
) for (i, n) in enumerate(names)]
2033

2134
options = Dict(
22-
:rowData => table2json(x),
35+
:rowData => table2json(rows, names, types),
2336
:columnDefs => coldefs,
2437
:enableSorting => true,
2538
:enableFilter => true,
@@ -35,22 +48,17 @@ function showtable(x; dark = false)
3548
end
3649
onimport(w, handler)
3750
w.dom = dom"div#grid"(className = "ag-theme-balham$(dark ? "-dark" : "")",
38-
style=Dict(:position => "absolute",
39-
:top => "0",
40-
:left => "0",
41-
:width => "100%",
42-
:height => "100%",
43-
:minHeight => "200px"))
51+
style=Dict(:width => "100%",
52+
"min-width" => "400px",
53+
:height => "800px"))
4454
w
4555
end
4656

4757
# directly write JSON instead of allocating temporary dicts etc
48-
function table2json(table)
49-
names = Tables.schema(table).names
50-
58+
function table2json(rows, names, types)
5159
io = IOBuffer()
5260
print(io, '[')
53-
for row in Tables.rows(table)
61+
for row in rows
5462
print(io, '{')
5563
i = 1
5664
for col in Tables.eachcolumn(row)

0 commit comments

Comments
 (0)