2

I have rows of data and some columns may be empty. I want to insert a - on columns with no data. Is there a way to place default value of cells if no data is returned for that column?

Something like this?

columnDefs: [
    {name: 'ColumnA', field: 'columnA', default: '-'}
]

I've tried this and it didnt work:

columnDefs: [
    {name: 'ColumnA', 
    field: 'columnA', 
    valueSetter: params=>{
       if(params.value === ''){
          return '-'
        }
    }}
]
Martina Carter
  • 409
  • 5
  • 18

2 Answers2

8
columnDefs: [
    {name: 'ColumnA', 
    field: 'columnA', 
    valueFormatter: params=>{
       if(params.value === ''){
          return '-'
        }
    }}
]
Martina Carter
  • 409
  • 5
  • 18
  • 2
    This solved your problem then? If so, please be sure to Mark it as the answer when you’re able to. And, if you can, highlight what changed to help readers hone in on the relevant part. – Jeremy Caney Mar 12 '21 at 19:49
  • This didn't quite work in my situation, but did lead me to a solution (upvoted!). This really checks for an empty string, but not necessarily a null value. Adjusting to `if(!params.value){...}` fixed it. Ref: https://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript – TTS Nov 16 '22 at 23:24
1

if use like that your code be better for other team member.

const isNull = (field) => {
    return field == null || field == "" ? '---' : field
}

const columnDefs = [
 { 
   field: 'fieldName',
   headerName: "header Name",
   valueFormatter: params => isNull(params.data.name)
  },
  { 
   field: 'sortName',
   headerName: "header Name",
   valueFormatter: params => isNull(params.data.sortName)
  }
]