Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
PGimenez opened this issue Sep 22, 2023 · 2 comments
Labels
question Further information is requested

Comments

@PGimenez
Copy link
Member

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()

@PGimenez PGimenez added the question Further information is requested label Sep 22, 2023
@hhaensel
Copy link
Member

hhaensel commented Nov 5, 2023

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
Copy link
Member

@PGimenez coudl you verify and close?
Otherwise please comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants