GenieFramework / StippleUI.jl

StippleUI is a library of reactive UI elements for Stipple.jl.
MIT License
82 stars 15 forks source link

Anonymous Javascript function in table event does not trigger handler in the backend #111

Open PGimenez opened 9 months ago

PGimenez commented 9 months ago

I'm trying to add interactivity to a table such that when a click is made on a row, the row id and its content are stored in two variables idand rowcontent. I have two handlers attached to each of the variables to display their contents when they change. When I select a row, the variable rowcontentis updated in the front end but its value is not propagated to the back. This is the event handler definition

     table(:data, var"v-on:row-click"="function(event,row) {id=row.__id;rowcontent=row}")

However, if I do rowcontent = String(row) then it works.

Moreover, I have two buttons to clear the selection. One clears the variables directly, whereas the other uses an anonymous function. Both buttons work so the issue seems not to be in the use of an anonymous function.

Perhaps it's that assigning one variable to the other like rowcontent=rowdoes not trigger the handler in table event functions?

Here's the MWE code:

using GenieFramework
using DataFrames
@genietools

@app begin
    @in id = 0
    @in rowcontent = ""
    @in placeholder = "placeholder"
    @out data = DataTable(DataFrame(a = rand(5) , b = rand(5)))
    @onchange id begin
        @show id
    end
    @onchange rowcontent begin
        @show rowcontent
    end
end

function ui()
    [
     p("Row: {{id}}, {{rowcontent}}")
     table(:data, var"v-on:row-click"="function(event,row) {id=row.__id;rowcontent=row}")
    btn("Remove selection",@click("id=0; rowcontent=placeholder"))
    btn("Anonymous remove selection",@click("function() {id=0; rowcontent=placeholder}"))
   ]
end
@page("/", ui)
up()
hhaensel commented 8 months ago

It does trigger the backend, but it fails assigning the value to rowcontent, because rowcontent is initialised to be of type String, but rowcontent on the client side is an object. Whenever you are not sure what will be transmitted by the program, you can initialise the app variable of type Any.

@app begin
    @in id = 0
    @in rowcontent::Any = ""
    <...>
end

Then click the row and you will find that rowcontent is of type Dict{String, Any} and you can chose this type for initialisation.

You could, of course, also initalise rowcontent as row = Dict{Symbol, Any}(), and everything will work out as well.

hhaensel commented 7 months ago

@PGimenez coudl you verify and close? Otherwise please comment