dt闪亮,保留用户定义的分页设置

时间:2018-10-01 07:08:36

标签: r pagination shiny dt

有一个烦人的问题,我无法解决这个问题,因此我决定提出这个问题,也许有人能够找到可行的解决方案。我阅读了很多论坛,其中一些(stackoverflow f.e.)提供了一些见解,因为其他用户也遇到了这个问题。无论如何,我无法实现可靠的解决方案。

我正在构建一个闪亮的外观,它使用数据表的许多不同实例来显示各种数据。用户可以与所有数据进行交互,并且可以修改,删除和向表中添加新记录。表的内容保存为反应值。

如果我根据点Nr使用选项设置。 2在此链接上:https://rstudio.github.io/DT/options.html  只要使用固定整数,一切都可以正常工作。我可以设置所需的页面长度,并且表格始终按应有的方式呈现。我想实现,用户可以选择所需的任何页面长度设置,并由于更新表后面的无功值集而在重新渲染表后保留此页面长度和页码设置。在stackoverflow上也有类似的问题,我已经尝试了所有推荐的解决方案,但似乎没有任何效果。

我的方法是(我只提供服务器端,显然我有一个DT对象 ui端称为“ workForcePoolOutput”以及所需的按钮等): •创建一个变量来保存先前设置的用户选择。

---------- Shiny App Server Side Code ----------

server <- shinyServer(function(input, output,session) {

#Workforce Table constants
 workForcePoolOutput_previousPageLength <- NULL
 workForcePoolOutput_previousPage <- NULL

•设置表格

output$workforcePoolOutput <- DT::renderDataTable(DT::datatable(values$workForceData[,1:15], rownames = FALSE, filter = list(position = "top",clear = TRUE),
 options = list(pageLength = if(is.null(workForcePoolOutput_previousPageLength)){10} else{workForcePoolOutput_previousPageLength},
 displayStart = if(is.null(workForcePoolOutput_previousPage)){0} else {workForcePoolOutput_previousPage},
 stateSave = TRUE)))

•我现在介绍用于向Workforce数据库添加新值的代码

 #Observing the action button for new work force
 observeEvent(input$newWorkForceButton, {
# Updating the holding variables of the previous table settings
workForcePoolOutput_previousPageLength <<- input$workforcePoolOutput_state$length
workForcePoolOutput_previousPage <<- input$workforcePoolOutput_rows_current[1] - 1

#......... some function logic, works fine not related to the problem

#updating the reactive value
temp <- rbind(values$workForceData,new_fella)
values$workForceData <- temp

#Print the previous user setting for checking
print(workForcePoolOutput_previousPage)
print(workForcePoolOutput_previousPageLength)

#Rerender the table, this part may not be necessary as I have the same outside of the
#"observeButton" function. I tried to include or exclude this part here, no real change in behaviour
output$workforcePoolOutput <- DT::renderDataTable(DT::datatable(values$workForceData[,1:15], rownames = FALSE, filter = list(position = "top",clear = TRUE),
                                                                options = list(pageLength = if(is.null(workForcePoolOutput_previousPageLength)){10} else {workForcePoolOutput_previousPageLength}, displayStart = if(is.null(workForcePoolOutput_previousPage)){0} else {workForcePoolOutput_previousPage}, stateSave = TRUE)))

现在,动态调整分页似乎真的很困难。奇怪的是,  如果我早上启动计算机并启动应用程序,那么一切似乎都在工作  或以下,但相对稳定。我可以围着桌子玩,添加新员工和分页  数据正确显示。  之后,我关闭了应用程序并重新启动它,事情开始表现得很奇怪。

我附上一些截图以描述行为。例如,我看到一个表,其中显示  每页10条记录,我在看第二页。我将新记录添加到表中,然后  表被重新呈现。在重新渲染R之前,将先前保存的设置写入控制台  并据此正确保存了所有内容。每页10条记录,并从  第10条记录,又称第2页。但是,该表将重新呈现,每页25条记录。

这只是一个例子,我开发的应用程序包含很多相似的表,我的用户不喜欢  他们不能在不丢失分页设置的情况下修改表内容的事实。

如果您能提供一些见解,我将不胜感激!谢谢!

Image_1 Image_2

0 个答案:

没有答案