如何使Shiny Datatable Column Filter搜索更友好

时间:2016-07-29 17:56:58

标签: javascript r shiny dt

我想知道如何让闪亮的数据表中的列过滤器搜索更加友好。使用其默认版本,您一次只能搜索一个字符,然后跳出,您必须重新点击过滤器框以键入另一个字符,这非常令人沮丧和耗时。我想知道是否有办法让它更友好。

目前我有一般搜索框的解决方案:

tags$script(JS("$(function(){
                        var myTable=$('#myTable').dataTable();
                        $('.dataTables_filter input')
                        .unbind('keypress keyup')
                        .bind('keypress keyup', function(e){
                        if ($(this).val().length < 3 && e.keyCode != 13) return;
                        myTable.fnFilter($(this).val());
                        });
                        });")),

有谁知道如何使列过滤器同样友好?

我的服务器.R是:

library(shiny)

library(DT)

source("update.R")

shinyServer(function(input,output,session){

dataInput<-reactive(main())

output$table<-DT::renderDataTable(
  withProgress(message="Compiling",
               datatable(dataInput()[which(dataInput()$Date<=input$dates[2]&dataInput()$Date>=input$dates[1]),],
                         extensions=c("Buttons","FixedHeader","AutoFill","ColReorder"),
                         filter="top",
                         rownames=FALSE,
                         options=list(searchHighlight=TRUE,
                                      search=list(caseInsensitive=TRUE),
                                      dom='Blfrtip',
                                      buttons=c("copy","csv","print","colvis"),
                                      autoWidth=TRUE,
                                      lengthMenu=c(25,50,100,1000),
                                      pageLength=25,
                                      fixedHeader=TRUE,
                                      autoFill=TRUE,
                                      colReorder=TRUE,
                                      style="width:100%"
                         ),
                         escape=FALSE) %>% formatCurrency(c(22:25,28:33),currency="",digits=0)
  )
)

output$download<-downloadHandler(
  filename=function(){
    paste("form_d_",Sys.Date(),".csv",sep="")
  },
  content=function(con){
    write.csv(dataInput(),con,row.names=FALSE)
  }
)

observeEvent(input$btn,
             session$sendCustomMessage("pager",input$pagination-1)
)
})

和我的ui.R是:

library(shiny)

library(DT)

shinyUI(
  fluidPage(

  titlePanel("", windowTitle="Form D Database"),

  tags$head(tags$style("#table  {white-space: nowrap;  }")),

  fluidRow(column(5,downloadButton("download",label=h5("Download all"))),
           column(3,dateRangeInput("dates",label=h4("Date:"),start="2008-05-30",min="2008-05-30")
           )
  ),

  mainPanel(helpText("These four buttons only work with the view. To download everything, click the 'Download all' button."),

        dataTableOutput("table"),

        tags$script(JS("$(function(){
                        var myTable=$('#myTable').dataTable();
                        $('.dataTables_filter input')
                        .unbind('keypress keyup')
                        .bind('keypress keyup', function(e){
                        if ($(this).val().length < 3 && e.keyCode != 13) return;
                        myTable.fnFilter($(this).val());
                        });
                        });")),

        numericInput("pagination",label=h4("Page:"),value=1),

        actionButton("btn","Go to Page"),

        tags$script(HTML(
          "Shiny.addCustomMessageHandler('pager',function(page) {
          $('#'+'table').find('table').DataTable().page(page).draw(false);
          })"))
  )
  )
)

其中main()是来自update.R的函数,返回data.frame

0 个答案:

没有答案