jrowen / rhandsontable

A htmlwidgets implementation of Handsontable.js
http://jrowen.github.io/rhandsontable/
Other
385 stars 148 forks source link

Conditionally apply currency formatter to specific columns #405

Closed Alik-V closed 2 years ago

Alik-V commented 2 years ago

Hi, thanks for this brilliant package, it's seen a lot of use in my shiny apps!

I got a query regarding conditional currency formatting for specific cells in the table, could anyone recommend how it's best achieved using available tools in the package?

  library(tidyverse)
  library(rhandsontable)
  df <- tibble(
    params = paste0("Param ", 1:5),
    value = as.double(1:5)
  )
  rhandsontable(df, 
                col_highlight = 1,
                currency_rows = c(1,4)) %>%
    hot_cols(renderer = "function(instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);
             if (instance.params) {
             // define varaibles for use
             col_highlight = instance.params.col_highlight;
             col_highlight = col_highlight instanceof Array ? col_highlight : [col_highlight];
             currency_rows = instance.params.currency_rows;
             currency_rows = currency_rows instanceof Array ? currency_rows : [currency_rows];
             }
             if (instance.params && col_highlight.includes(col)) {
              td.style.background = 'lightyellow'; // yellow background
             }
             if (instance.params && col_highlight.includes(col) && currency_rows.includes(row)) {
             td.style.background = 'lightgreen'; //green background
             // how to conditionally format to USD here?
             }
             }
             ")
zburch commented 2 years ago
  library(tidyverse)
  library(rhandsontable)
  df <- tibble(
    params = paste0("Param ", 1:5),
    value = as.double(1:5)
  )
  rhandsontable(df, 
                col_highlight = 1,
                currency_rows = c(1,4)) %>%
    hot_cols(renderer = "function(instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);
             if (instance.params) {
             // define varaibles for use
             col_highlight = instance.params.col_highlight;
             col_highlight = col_highlight instanceof Array ? col_highlight : [col_highlight];
             currency_rows = instance.params.currency_rows;
             currency_rows = currency_rows instanceof Array ? currency_rows : [currency_rows];
             }
             if (instance.params && col_highlight.includes(col)) {
              td.style.background = 'lightyellow'; // yellow background
             }
             if (instance.params && col_highlight.includes(col) && currency_rows.includes(row)) {
             td.style.background = 'lightgreen'; //green background
             // how to conditionally format to USD here?
             td.innerHTML = `$${Number.parseFloat(value).toFixed(2).toLocaleString(\"en-US\")}`
             }
             }
             ")
Alik-V commented 2 years ago
 library(tidyverse)
 library(rhandsontable)
 df <- tibble(
   params = paste0("Param ", 1:5),
   value = as.double(1:5)
 )
 rhandsontable(df, 
               col_highlight = 1,
               currency_rows = c(1,4)) %>%
   hot_cols(renderer = "function(instance, td, row, col, prop, value, cellProperties) {
            Handsontable.renderers.NumericRenderer.apply(this, arguments);
            if (instance.params) {
            // define varaibles for use
            col_highlight = instance.params.col_highlight;
            col_highlight = col_highlight instanceof Array ? col_highlight : [col_highlight];
            currency_rows = instance.params.currency_rows;
            currency_rows = currency_rows instanceof Array ? currency_rows : [currency_rows];
            }
            if (instance.params && col_highlight.includes(col)) {
             td.style.background = 'lightyellow'; // yellow background
            }
            if (instance.params && col_highlight.includes(col) && currency_rows.includes(row)) {
            td.style.background = 'lightgreen'; //green background
            // how to conditionally format to USD here?
            td.innerHTML = `$${Number.parseFloat(value).toFixed(2).toLocaleString(\"en-US\")}`
            }
            }
            ")

That's brilliant, thanks a lot! Do you know how I would change the currency to, for example, GBP? Changing en-US to en-GB does not seem to do the trick

zburch commented 2 years ago
library(tidyverse)
library(rhandsontable)
df <- tibble(
  params = paste0("Param ", 1:5),
  value = as.double(1:5)
)
rhandsontable(df, 
              col_highlight = 1,
              currency_rows = c(1,4)) %>%
  hot_cols(renderer = "function(instance, td, row, col, prop, value, cellProperties) {
            Handsontable.renderers.NumericRenderer.apply(this, arguments);
            if (instance.params) {
            // define varaibles for use
            col_highlight = instance.params.col_highlight;
            col_highlight = col_highlight instanceof Array ? col_highlight : [col_highlight];
            currency_rows = instance.params.currency_rows;
            currency_rows = currency_rows instanceof Array ? currency_rows : [currency_rows];
            }
            if (instance.params && col_highlight.includes(col)) {
             td.style.background = 'lightyellow'; // yellow background
            }
            if (instance.params && col_highlight.includes(col) && currency_rows.includes(row)) {
            td.style.background = 'lightgreen'; //green background
            //GBP
            td.innerHTML = `&pound${Number.parseFloat(value).toFixed(2)}`
            }
            }
            ")
Alik-V commented 2 years ago

Zack, you are amazing, thank you for the help!