闪亮的动态按钮

发布于 2025-01-31 18:22:25 字数 5706 浏览 3 评论 0原文

这里的情况很棘手。 我正在尝试生成一个UI来管理应用程序中的项目。 我要做的是,每个用户都有他的项目列表存储在某个地方,并且从我生成的项目列表中,对于每个项目,一个包含四个按钮的div(后来的buttontitle,continueproj,deleteproj和shareproj)。单击时,这些按钮中的每一个都应在相应的项目上触发功能。

为了处理这一点,每次我进入与此UI相对应的面板时,我都会用insertui进行空白并生成一个UI,并填写每个操作的列表。

for (i in 1:(length(list_ui))){
  removeUI(
     selector = paste0('#',i)
   )
}
tit_obsList <<- list()
con_obsList <<- list()
del_obsList <<- list()
sha_obsList <<- list()
list_ui <<- list()

if(length(USER$list_of_projects !=0)){
for (i in 1:length(USER$list_of_projects)){
  insertUI(
    selector = '#placeholder',
    ui = tags$div(
      #class = "ProjectPanel",
      style = "background: #ffffff;
                     display: block; 
                     margin-left: auto; 
                     margin-right: auto;
                     width:1250px;
                     height:175px;
                     background-color: #ffffff;
                     color: #4a86e8;
                     border: #434343;",
      fluidRow(column(width = 11,
                      actionButton(inputId = paste0("buttonTitle_",USER$list_of_projects[i]),label = USER$list_of_projects[i],style = "background : #4a86e8;background-color: transparent;border-color : transparent; color: #4a86e8; font-size : 30px;")),
                      #tags$h3(paste(USER$list_of_projects[i]))), 
               column(width = 1,
                      tags$button(id = paste0("continueProj_",USER$list_of_projects[i]),class = "btn action-button",img(src = "icons/continue.gif",height = "40px"),style = "background-color: transparent;"))),
      fluidRow(column(width = 1,offset = 11,
                      tags$button(id = paste0("deleteProj_",USER$list_of_projects[i]),class = "btn action-button",img(src = "icons/delete_blue.png",height = "40px"),style = "background-color: transparent;"))),
      fluidRow(column(width = 1,offset = 11,
                      tags$button(id = paste0("shareProj_",USER$list_of_projects[i]),class = "btn action-button",img(src = "icons/share.gif",height = "40px"),style = "background-color: transparent;"))),
      id = i))
   #addPopover(session,paste0("deleteProj_",USER$list_of_projects[i]),"Hello")
  list_ui <<- c(list_ui,i)
  if (is.null(tit_obsList[[paste0("buttonTitle_",USER$list_of_projects[i])]])){
    # make sure to use <<- to update global variable obsList
    tit_obsList[[paste0("buttonTitle_",USER$list_of_projects[i])]] <<- observeEvent(input[[paste0("buttonTitle_",USER$list_of_projects[i])]], {
      continue_project(USER$list_of_projects[i])
    })
  }
  if (is.null(con_obsList[[paste0("continueProj_",USER$list_of_projects[i])]])){
    # make sure to use <<- to update global variable obsList
    con_obsList[[paste0("continueProj_",USER$list_of_projects[i])]] <<- observeEvent(input[[paste0("continueProj_",USER$list_of_projects[i])]], {
      continue_project(USER$list_of_projects[i])
    })
  }
  if (is.null(del_obsList[[paste0("deleteProj_",USER$list_of_projects[i])]])){
    # make sure to use <<- to update global variable obsList
    del_obsList[[paste0("deleteProj_",USER$list_of_projects[i])]] <<- observeEvent(input[[paste0("deleteProj_",USER$list_of_projects[i])]], {
    delete_project(USER$ID,USER$list_of_projects[i])
    removeUI(
      selector = paste0('#', i)
    )
    })
  }
  if (is.null(sha_obsList[[paste0("shareProj_",USER$list_of_projects[i])]])){
    # make sure to use <<- to update global variable obsList
    sha_obsList[[paste0("shareProj_",USER$list_of_projects[i])]] <- observeEvent(input[[paste0("shareProj_",USER$list_of_projects[i])]], {
      showModal(modalDialog(
        title = "You have decided to share your project",
        textInput("receiver_name","Please state the Login of the user you want to share your project with :"),
        hidden(p(id="enter_receiver","Please enter a user to share this project with ",style="font-size:10px;color:red;")),
        hidden(p(id="receiver_does_not_exist","This user does not exist",style="font-size:10px;color:red;")),
        hidden(p(id="receiver_has_access","This user already has access to this project",style="font-size:10px;color:red;")),
        footer = tagList(
          modalButton("Cancel"),
          actionButton("ok_shareProj", "OK")
          
        ),
        easyClose = TRUE
        )
      )
      if(input$receiver_name == ""){
        shinyjs::hide("receiver_does_not_exist")
        shinyjs::show("enter_receiver")
        shinyjs::hide("receiver_has_access")
      }
      else if(USER_in_database(input$receiver_name)==FALSE){
        shinyjs::show("receiver_does_not_exist")
        shinyjs::hide("enter_receiver")
        shinyjs::hide("receiver_has_access")
      }
      else if(has_access(input$receiver_name,USER$list_of_projects[i])){
        shinyjs::hide("receiver_does_not_exist")
        shinyjs::hide("enter_receiver")
        shinyjs::show("receiver_has_access")
      }
      else{
        invite_to_project(USER$ID,input$receiver_name,USER$list_of_projects[i])
        removeModal()
      }
    })
  
  }
}
  output$listproj <- renderText({unlist(USER$list_of_projects)})
  output$lenproj <- renderText({length(USER$list_of_projects)})
  output$uis <- renderText({unlist(tit_obsList)})
}

现在是无关紧要的是,如果我没有项目,我可以创建它们,所有这些都可以很好地工作:每个按钮都可以使用。 但是,如果我重新加载该应用程序,则所有UI都存在,但是只有最后一个按钮可以正常工作:如果我有3个项目P1,P2和P3,则只有P3按钮可以工作,或者更确切地说,如果我单击删除P2,则不会发生任何事情,但是现在,如果我单击以继续P3,则触发两个事件并在继续P3时删除P2。 另一个奇怪的问题是,如果我处于同一P1,P2,P3情况下,并且删除了P3,然后是P2和P2(后者)再次起作用,而P3却不在删除P3之前。这仅意味着最后一个作品。

我重复一遍,只有在我重新加载应用程序时才发生。如果未重新加载该应用程序,则所有项目都是功能性的,这是令人惊讶的,因为它们生成的列表是相同的,并且我称之为上面的代码的位置相同。

我希望有人可以帮助我解决这个问题,谢谢

quite a tricky situation here.
I am trying to generate a UI to manage projects in an App.
What I do is that each user has his list of projects stored somewhere, and from this list of project I generate, for each project, a div containing four buttons (later buttonTitle, continueProj, deleteProj and shareProj). Each of these buttons, when clicked, is supposed to trigger a function on the corresponding project.

To handle this, each time I get to the panel corresponding to this UI, I empty and generate an UI by project with insertUI, and fill a list for each action.

for (i in 1:(length(list_ui))){
  removeUI(
     selector = paste0('#',i)
   )
}
tit_obsList <<- list()
con_obsList <<- list()
del_obsList <<- list()
sha_obsList <<- list()
list_ui <<- list()

if(length(USER$list_of_projects !=0)){
for (i in 1:length(USER$list_of_projects)){
  insertUI(
    selector = '#placeholder',
    ui = tags$div(
      #class = "ProjectPanel",
      style = "background: #ffffff;
                     display: block; 
                     margin-left: auto; 
                     margin-right: auto;
                     width:1250px;
                     height:175px;
                     background-color: #ffffff;
                     color: #4a86e8;
                     border: #434343;",
      fluidRow(column(width = 11,
                      actionButton(inputId = paste0("buttonTitle_",USER$list_of_projects[i]),label = USER$list_of_projects[i],style = "background : #4a86e8;background-color: transparent;border-color : transparent; color: #4a86e8; font-size : 30px;")),
                      #tags$h3(paste(USER$list_of_projects[i]))), 
               column(width = 1,
                      tags$button(id = paste0("continueProj_",USER$list_of_projects[i]),class = "btn action-button",img(src = "icons/continue.gif",height = "40px"),style = "background-color: transparent;"))),
      fluidRow(column(width = 1,offset = 11,
                      tags$button(id = paste0("deleteProj_",USER$list_of_projects[i]),class = "btn action-button",img(src = "icons/delete_blue.png",height = "40px"),style = "background-color: transparent;"))),
      fluidRow(column(width = 1,offset = 11,
                      tags$button(id = paste0("shareProj_",USER$list_of_projects[i]),class = "btn action-button",img(src = "icons/share.gif",height = "40px"),style = "background-color: transparent;"))),
      id = i))
   #addPopover(session,paste0("deleteProj_",USER$list_of_projects[i]),"Hello")
  list_ui <<- c(list_ui,i)
  if (is.null(tit_obsList[[paste0("buttonTitle_",USER$list_of_projects[i])]])){
    # make sure to use <<- to update global variable obsList
    tit_obsList[[paste0("buttonTitle_",USER$list_of_projects[i])]] <<- observeEvent(input[[paste0("buttonTitle_",USER$list_of_projects[i])]], {
      continue_project(USER$list_of_projects[i])
    })
  }
  if (is.null(con_obsList[[paste0("continueProj_",USER$list_of_projects[i])]])){
    # make sure to use <<- to update global variable obsList
    con_obsList[[paste0("continueProj_",USER$list_of_projects[i])]] <<- observeEvent(input[[paste0("continueProj_",USER$list_of_projects[i])]], {
      continue_project(USER$list_of_projects[i])
    })
  }
  if (is.null(del_obsList[[paste0("deleteProj_",USER$list_of_projects[i])]])){
    # make sure to use <<- to update global variable obsList
    del_obsList[[paste0("deleteProj_",USER$list_of_projects[i])]] <<- observeEvent(input[[paste0("deleteProj_",USER$list_of_projects[i])]], {
    delete_project(USER$ID,USER$list_of_projects[i])
    removeUI(
      selector = paste0('#', i)
    )
    })
  }
  if (is.null(sha_obsList[[paste0("shareProj_",USER$list_of_projects[i])]])){
    # make sure to use <<- to update global variable obsList
    sha_obsList[[paste0("shareProj_",USER$list_of_projects[i])]] <- observeEvent(input[[paste0("shareProj_",USER$list_of_projects[i])]], {
      showModal(modalDialog(
        title = "You have decided to share your project",
        textInput("receiver_name","Please state the Login of the user you want to share your project with :"),
        hidden(p(id="enter_receiver","Please enter a user to share this project with ",style="font-size:10px;color:red;")),
        hidden(p(id="receiver_does_not_exist","This user does not exist",style="font-size:10px;color:red;")),
        hidden(p(id="receiver_has_access","This user already has access to this project",style="font-size:10px;color:red;")),
        footer = tagList(
          modalButton("Cancel"),
          actionButton("ok_shareProj", "OK")
          
        ),
        easyClose = TRUE
        )
      )
      if(input$receiver_name == ""){
        shinyjs::hide("receiver_does_not_exist")
        shinyjs::show("enter_receiver")
        shinyjs::hide("receiver_has_access")
      }
      else if(USER_in_database(input$receiver_name)==FALSE){
        shinyjs::show("receiver_does_not_exist")
        shinyjs::hide("enter_receiver")
        shinyjs::hide("receiver_has_access")
      }
      else if(has_access(input$receiver_name,USER$list_of_projects[i])){
        shinyjs::hide("receiver_does_not_exist")
        shinyjs::hide("enter_receiver")
        shinyjs::show("receiver_has_access")
      }
      else{
        invite_to_project(USER$ID,input$receiver_name,USER$list_of_projects[i])
        removeModal()
      }
    })
  
  }
}
  output$listproj <- renderText({unlist(USER$list_of_projects)})
  output$lenproj <- renderText({length(USER$list_of_projects)})
  output$uis <- renderText({unlist(tit_obsList)})
}

What is extraneous now is that, if I have no project, I can create them and all is working well : every button is functional.
But if I reload the app, all UIs are present, but only the last button is working : if I have 3 projects P1, P2 and P3, only P3 buttons work, or more precisely, if I click to delete P2, nothing happens, but now if I click to continue P3, both events are triggered and P2 is deleted while P3 is continued.
Another strange issue is that if I am in this same P1, P2, P3 situation and I delete P3, then P2 and only P2, the latter one, is functional again, while it was not before P3 was deleted. That means only the last one works.

I repeat that this only happens when I reload the app. If the app is not reloaded, all projects are functional, which is surprising because the list from which they are generated is the same and the place from which I call the code above is the same.

I hope someone can help me tackle this issue, thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文