Select only filtered rows using select all button that comes with select extension in shiny's DT package

I am trying to select only filtered rows using select all button that comes with select extension in shiny's DT package but it selects all the rows.
Here's the sample shiny app

Below is the reproducible code for the app:

library(DT)
data(mpg)
# Define UI for application 
ui <- fluidPage(

    # Application title
    titlePanel("Select only filtered rows using selectall button"),
    
    br(),
    br(),
    
    DT::dataTableOutput("table")
        
    
)

# Define server logic 
server <- function(input, output) {

    output$table <- DT::renderDataTable({
        datatable(mpg, escape=F,
                  rownames=F,
                  filter = 'top',
                  #  colnames = c("Data Type","Variable","Description", "Filename"),
                  class = "compact hover row-border",
                  extensions = c('Scroller','Select', 'Buttons'),
                  
                  options = list(
                      select = list(style = "multi", items = "row"),
                      columnDefs = list(list(className = 'dt-center', targets = "_all")),
                      language = list(
                          info = 'Showing _START_ to _END_ of _TOTAL_ variables'),
                      deferRender = TRUE,
                      scrollY = 500,
                      scroller = TRUE,
                      dom = "Blfrtip",
                      buttons = c('selectAll', 'selectNone')
                  ),
                  selection="none"
        ) }, server = F
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

I think I may have to add some custom javascript to fix this but I am not good with it.
Can anyone help or give any suggestions.

Thanks

Somehow I managed to figure out the solution for my question. Posting it here, so it might help others. I got help from couple of places. Datatable document and stackoverflow.

Using these helps, I extended my selectall button functionality and also extended it for deselectall button (deselect any filtered rows).

Here's the updated shiny app

Below is the updated code:

library(shiny)
library(DT)
data(mpg)
# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Select only filtered rows using selectall button"),
    
    br(),
    br(),
    
    DT::dataTableOutput("table")
    
        
    
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$table <- DT::renderDataTable({
        datatable(mpg, escape=F,
                  rownames=F,
                  filter = 'top',
                  #  colnames = c("Data Type","Variable","Description", "Filename"),
                  class = "compact hover row-border",
                  extensions = c('Scroller','Select', 'Buttons'),
                  
                  options = list(
                      select = list(style = "multi", items = "row"),
                      columnDefs = list(list(className = 'dt-center', targets = "_all")),
                      language = list(
                          info = 'Showing _START_ to _END_ of _TOTAL_ variables'),
                      deferRender = TRUE,
                      scrollY = 500,
                      scroller = TRUE,
                      dom = "Blfrtip",
                      buttons = list(list(extend='selectAll',className='selectAll',
                                text="select all rows",
                                action=DT::JS("function () {
                                var table = $('#DataTables_Table_0').DataTable();
                                table.rows({ search: 'applied'}).deselect();
                                table.rows({ search: 'applied'}).select();
                }")
                                ), list(extend='selectNone',
                                        text="DeselectAll",
                                        action=DT::JS("function () {
                                var table = $('#DataTables_Table_0').DataTable();
                                table.rows({ search: 'applied'}).select();
                                table.rows({ search: 'applied'}).deselect();
                }")
                                ))
                      
                  ),
                  selection="none"
        ) }, server = F
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

Hope this help others.