From 0212b26a7d6bbcdd041df8fe37c4eeca98a7de3d Mon Sep 17 00:00:00 2001 From: Adrian Salceanu Date: Wed, 28 Aug 2024 16:04:39 +0200 Subject: [PATCH] wip --- Project.toml | 6 ++-- demos/tables/app.jl | 2 +- demos/tables/app2.jl | 43 +++++++++++++++++++++++++ src/Tables.jl | 75 +++++++++++++++++++++++++++++++++----------- 4 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 demos/tables/app2.jl diff --git a/Project.toml b/Project.toml index 86509677..22f77e93 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StippleUI" uuid = "a3c5d34a-b254-4859-a8fa-b86abb7e84a3" authors = ["Adrian Salceanu "] -version = "0.23.3" +version = "0.23.4" [deps] Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" @@ -22,10 +22,10 @@ StippleUIDataFramesExt = "DataFrames" Colors = "0.12" DataFrames = "1" Dates = "1.6" -Genie = "5.23.8" +Genie = "5.30.4" OrderedCollections = "1" PrecompileTools = "1" -Stipple = "0.28.11" +Stipple = "0.28.13" Tables = "1" julia = "1.6" diff --git a/demos/tables/app.jl b/demos/tables/app.jl index e76d7047..38b57060 100644 --- a/demos/tables/app.jl +++ b/demos/tables/app.jl @@ -6,7 +6,7 @@ using DataFrames @app begin NO_OF_ROWS = 1000 - DATA = sort!(DataFrame(rand(NO_OF_ROWS, 2), ["x1", "x2"])) + DATA = sort!(DataFrame(rand(NO_OF_ROWS, 2), ["x1", "x2"]))::DataFrame # we only sort so that the changes are more visible when filtering and paginating ROWS_PER_PAGE = 10 @out df = DATA diff --git a/demos/tables/app2.jl b/demos/tables/app2.jl new file mode 100644 index 00000000..bf4410bc --- /dev/null +++ b/demos/tables/app2.jl @@ -0,0 +1,43 @@ +module App + +using GenieFramework +using DataFrames +@genietools + +@app begin + NO_OF_ROWS = 1000 + DATA = sort!(DataFrame(rand(NO_OF_ROWS, 2), ["x1", "x2"]))::DataFrame # we only sort so that the changes are more visible when filtering and paginating + ROWS_PER_PAGE = 10 + + @out df = DATA + @out dt = DataTable(DataFrame([]), DataTableOptions(DATA)) + @out pagination = DataTablePagination(rows_per_page = ROWS_PER_PAGE, rows_number = NO_OF_ROWS) + @out loading = false + @in filter = "" + + @onchange isready begin + dt = DataTable(DATA[1:ROWS_PER_PAGE, :]) + filter = "" + end + + @event request begin + loading = true + state = process_request(DATA, dt, pagination, filter) + dt = state.datatable + pagination = state.pagination + loading = false + end + + @onchange filter begin + loading = true + pagination.page = 1 + state = process_request(DATA, dt, pagination[], filter) + dt = state.datatable + pagination = state.pagination + loading = false + end +end + +@page("/", "ui.jl") + +end \ No newline at end of file diff --git a/src/Tables.jl b/src/Tables.jl index ff75ef70..ed8939a2 100644 --- a/src/Tables.jl +++ b/src/Tables.jl @@ -1,5 +1,6 @@ module Tables +using Base: @kwdef import Tables as TablesInterface using Genie, Stipple, StippleUI, StippleUI.API import Genie.Renderer.Html: HTMLString, normal_element, table, template, register_normal_element @@ -10,14 +11,14 @@ export cell_template, qtd, qtr register_normal_element("q__table", context = @__MODULE__) const ID = "__id" -const DATAKEY = "data" # has to be changed to `rows` for Quasar 2 +const DATAKEY = "data" # has to be changed to `rows` for Quasar 2 const DataTableSelection = Vector{Dict{String, Any}} struct2dict(s::T) where T = Dict{Symbol, Any}(zip(fieldnames(T), getfield.(Ref(s), fieldnames(T)))) #===# -Base.@kwdef mutable struct Column +@kwdef mutable struct Column name::String required::Bool = false label::String = name @@ -71,7 +72,7 @@ end julia> DataTablePagination(rows_per_page=50) ``` """ -Base.@kwdef mutable struct DataTablePagination +@kwdef mutable struct DataTablePagination sort_by::Symbol = :desc descending::Bool = false page::Int = 1 @@ -103,7 +104,7 @@ julia> dt.opts.columnspecs[r"^(a|b)\$"] = opts(format = jsfunction(raw"(val, row julia> model.table[] = dt ``` """ -Base.@kwdef mutable struct DataTableOptions +@kwdef mutable struct DataTableOptions addid::Bool = false idcolumn::String = "ID" columns::Union{Vector{Column},Nothing} = nothing @@ -111,11 +112,6 @@ Base.@kwdef mutable struct DataTableOptions end -mutable struct DataTable{T} - data::T - opts::DataTableOptions -end - """ DataTable(data::T, opts::DataTableOptions) @@ -140,12 +136,53 @@ julia> Tables.table([1 2 3; 3 4 5], header = ["a", "b", "c"]) julia> dt = DataTable(t1) ``` """ +mutable struct DataTable{T} + data::T + opts::DataTableOptions + filter::AbstractString + pagination::DataTablePagination +end + +# function DataTable{T}() where {T} +# DataTable{T}(T(), DataTableOptions(), "", DataTablePagination()) +# end + +function DataTable{T}(data::T, opts::DataTableOptions) where {T} + DataTable{T}(data, opts, "", DataTablePagination()) +end +function DataTable(data::T, opts::DataTableOptions) where {T} + DataTable{T}(data, opts) +end + function DataTable(data::T) where {T} DataTable{T}(data, DataTableOptions()) end +function DataTable{T}( + data::T + + ; + filter::AbstractString = "", + + # DataTableOptions + addid::Bool = false, + idcolumn::String = "ID", + columns::Union{Vector{Column},Nothing} = nothing, + columnspecs::Dict{Union{String, Regex}, Dict{Symbol, Any}} = Dict(), + + # DataTablePagination + sort_by::Symbol = :desc, + descending::Bool = false, + page::Int = 1, + rows_per_page::Int = 10, + rows_number::Union{Int,Nothing} = nothing, -function DataTable{T}() where {T} - DataTable{T}(T(), DataTableOptions()) + _filter::AbstractString = filter +) where {T} + DataTable{T}( data, + DataTableOptions(addid, idcolumn, columns, columnspecs), + filter, + DataTablePagination(sort_by, descending, page, rows_per_page, rows_number, _filter) + ) end #===# @@ -326,7 +363,7 @@ function cell_template(; columns isa Vector || columns === nothing || (columns = [columns]) if edit isa Bool # `columns` decides on which columns should be editable - # + # columns === nothing && (columns = [""]) if edit edit_columns = columns @@ -353,7 +390,7 @@ function cell_template(; ) ]) push!(cell_templates, t) - end + end isempty(edit_columns) && return cell_templates @@ -361,7 +398,7 @@ function cell_template(; if change_class == "text-red " change_class = change_style === nothing ? "text-red" : nothing end - + # in contrast to `props.value` `props.row[props.col.name]` can be written to value = "props.row[props.col.name]" # ref_rows are calculated from ref_table, if not defined explicitly @@ -386,11 +423,11 @@ function cell_template(; if inner_class !== nothing && isempty(inner_class) inner_class = nothing end - + # add standard settings from stipplecore.css table_style = Dict("font-weight" => 400, "font-size" => "0.9rem", "padding-top" => 0, "padding-bottom" => 0) inner_style = inner_style === nothing ? table_style : [table_style, inner_style] - + # add custom style for changed entries if ref_rows !== nothing && change_style !== nothing change_style_js = JSON3.write(render(change_style)) @@ -479,7 +516,7 @@ function table( fieldname::Symbol, columnskey::String = "$fieldname.columns", filter::Union{Symbol,String,Nothing} = nothing, paginationsync::Union{Symbol,String,Nothing} = nothing, - + columns::Union{Nothing,Bool,Integer,AbstractString,Vector{<:AbstractString},Vector{<:Integer}} = nothing, cell_class::Union{Nothing,AbstractString,AbstractDict,Vector} = nothing, cell_style::Union{Nothing,AbstractString,AbstractDict,Vector} = nothing, @@ -492,7 +529,7 @@ function table( fieldname::Symbol, change_style::Union{Nothing,AbstractString,AbstractDict,Vector} = nothing, change_inner_class::Union{Nothing,AbstractString,AbstractDict,Vector} = nothing, change_inner_style::Union{Nothing,AbstractString,AbstractDict,Vector} = nothing, - + kwargs...) :: ParsedHTMLString if !isa(edit, Bool) || edit || cell_class !== nothing || cell_style !== nothing @@ -503,7 +540,7 @@ function table( fieldname::Symbol, startswith(String(p[1]), "inner_") ? p : nothing end - table_template = cell_template(; ref_table, ref_rows, rowkey, + table_template = cell_template(; ref_table, ref_rows, rowkey, edit, columns, class = cell_class, style = cell_style, type = cell_type, inner_class, inner_style, change_class, change_style, change_inner_class, change_inner_style, cell_kwargs..., inner_kwargs... )