R - datatable formatting with javascript

R - datatable formatting with javascript

JedditeJeddite Posts: 3Questions: 1Answers: 0

I'm trying to format datatable in R using DT package. I have code like this:

library(data.table)
library(DT)
data<-data.table(rbind(c(1,2,3),c(4,5,6)))
colnames(data)<- c('A','B','c')

datatable(data, rownames=F, 
      colnames=c('A','B','C'),
      class='stripe cell-border hover', 
      options=list(
        pageLength=100, 
        dom='ltp', 
        initComplete = JS("
                          function(settings, json) {
                          $(this.api().table().body()).css({
                          'background-color': 'red',
                          'outline-color': 'red',
                          'margin':'100px',
                          'color': 'violet',
                          'text-align': 'center',
                          'font-family': 'Courier New',
                          'border-radius': '25px'
                          });
                          $(this.api().table().header()).css({
                          'background-color': '#000',
                          'color': '#fff',
                          'outline-color': 'red',
                          'margin':'100px',
                          'text-align': 'center',
                          'font-family': 'Courier New',
                          'border-radius': '25px'
                          });
                          }
                          ")
        ), 
      caption = htmltools::tags$caption(
        style = 'caption-side: top; text-align: center; color:black; 
        font-size:200% ;','Table'),
      filter=list(position = 'top')
      )

And I have problem with javascript in function JS(). It modifies background color (but only in header), font color and style. But commands to align text or round corners don't work.

Why does it work like that? And how can I modify code to format this other things?

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I'd suggest trying to get the styling to work without DataTables initially. You probably don't want to apply border-radius to the table body and header for example (I don't know what a browser will do with that - probably nothing).

    This is the border radius applied to a table without DataTables: http://live.datatables.net/poyoweti/1/edit .

    Allan

  • JedditeJeddite Posts: 3Questions: 1Answers: 0

    Yes, I don't want to apply border-radius to the table body and header, but to whole table. Problem is that I don't know how to do it properly. In your link there is effect I want to achive, but I don't know how to apply it in my code.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I can't really say without being able to see your code :smile:

    Allan

  • JedditeJeddite Posts: 3Questions: 1Answers: 0

    This in first post is whole my code in R.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I was meaning the Javascipt includes, stylesheets, markup, etc.

    I've given a link showing how it could be done. As to why it isn't working for you, I'm not sure. I would need a similarly functioning page to be able to take a look, debug and inspect it.

    Allan

This discussion has been closed.