1

I am able to render an AG-grid by following the example code laid out on the JustPy website. However when it comes to formatting cells or columns the AG-grid documentation shows using valueFormatter and arrow functions. For example to format a number to 2 decimals in AG-grid would like like:

valueFormatter: params => params.data.number.toFixed(2)

I'm am struggling to see how I could translate this some kind of string to pass to to the valueFormatter property of the JustPy grid component. In the code snippet below the styling works for cellClass and cellClassRules but the formatting line does not work and many attempts at different strings are all failing. If anyone has gotten this to work and can share any example to get me going it would be greatly appreciated.

update: Thank you @thirtydot that recommendation sent me in the right direction. But I am still tripping up because getDataPath is a single option to evalutate but here I am looking to evaluate a function that is under valueFormatter which it itself is a property of columnDefs. Then also there are multiple fields that each have their own columndefs and valueformatter properties. So am I to evaluate ALL 'ColumnDefs', or evaluate 'valueFormatter'? Can I do it generically like below or do I need to evaluate each one (i.e. evaluate['columnDefs[9]'])? If anyone has had success with this please let me know. I feel like I am much closer than I started but still far away. The code below produces an ag-grid with the correct conditional formatting but the valueformatter is not evaluating and thus not returning formatted numbers.


    def grid_test1():
    wp = jp.WebPage()
    grid = ooi.jp.ag_grid(a=wp)  # a=wp adds the grid to WebPage wp
    grid.theme = "ag-theme-balham-dark"
    grid.options.columnDefs[0].cellClass = ['text-white', 'bg-blue-500', 'hover:bg-blue-200']
    grid.options.columnDefs[9].valueFormatter = {'''function(params) { '$' + formatNumber(params.value); }'''}
    grid.options.columnDefs[15].valueFormatter = {'''function(params) { params.data.number.toFixed(2); }'''}
    for col_def in grid.options.columnDefs[1:]:
        col_def.cellClassRules = {
            'font-bold': 'x < 20',
            'bg-red-300': 'x < 20',
            'bg-yellow-300': 'x >= 20 && x < 50',
            'bg-green-300': 'x >= 50'
        }
    grid.evaluate = ['valueFormatter']    
    return wp

  • Follow the `getDataPath` example here to pass a function: https://justpy.io/grids_tutorial/field_evaluation/ – thirtydot Oct 10 '22 at 12:38
  • @thirtydot Thanks for the reply. That example helps but what is still through me for a loop is that example is evaluating the the function that is used to define the getDataPath option in AG-grid so then they call grid.evaluate=['getDataPath'] to evaluate the one function tied to getDataPath. But I am looking to evaluate multiple fields tied to different formatting functions. All of which are under columnDefs[].valueFormatter. so struggling to see how to adjust for that use case. – Brandon Bettencourt Oct 11 '22 at 20:31
  • Ok I am still tripping up because getDataPath is a single option to evalutate but here I am looking to evaluate a function that is under valueFormatter which it itself is a property of columnDefs. Then also there are multiple fields that each have their own columndefs. So am I to evaluate ALL 'ColumnDefs', or evaluate 'valueFormatter'? Can I do it generically like below or do I need to evaluate each one (i.e. evaluate['columnDefs[9]']) grid.options.columnDefs[9].valueFormatter = {'''function(params) { '$' + formatNumber(params.value); }'''} grid.evaluate = ['valueFormatter'] – Brandon Bettencourt Oct 12 '22 at 20:29
  • Based on [this](https://github.com/justpy-org/justpy/discussions/445), [this](https://github.com/justpy-org/justpy/discussions/448), and the [current source code](https://github.com/justpy-org/justpy/blob/2051206f60c52e7d89ba973b4ed0eee9b004212a/justpy/gridcomponents.py), it's not currently possible to do what you want. I wouldn't recommend using ag-grid through such a "weird" API (JustPy), it will be annoying. – thirtydot Oct 16 '22 at 01:33

0 Answers0