render string values comma separated adding space after comma and capitalize

render string values comma separated adding space after comma and capitalize

carlito27carlito27 Posts: 14Questions: 4Answers: 0

I'm trying to add a custom renderer for a column (regions) that has a string that looks like..

toronto,montreal,tokyo...

I would like to render it in the datatable as

Toronto, Montreal, Tokyo

So capitalize first letter and add a space after each comma. Is this possible via renderer?

I could use jquery to add the space after comma with
string.split(',');

And some css to capitalize but Ideally I would rather use renderer to customize everything.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947
    Answer ✓

    There is nothing built-in, that I know of, to Datatables or Javascript to do this automatically. I would look at using `str.split(',') as you noted then loop through that array and capitalize each word. There are lots of ways to do this but I would probably do something like this:

    var str = 'toronto,montreal,tokyo';
    str = str.split(',')
       .map(w => w.charAt(0).toUpperCase() + w.slice(1))
       .join(', ')
    console.log(str);   // str = Toronto, Montreal, Tokyo
    

    Kevin

  • carlito27carlito27 Posts: 14Questions: 4Answers: 0

    If anybody is having trouble with this I was able to fix it with the following code using render.

      { "data": "regions", 
      "render" : function( data, type, full ) {
        data = data.split(',')
           .map(w => w.charAt(0).toUpperCase() + w.slice(1))
           .join(' ')
        return data;                          
      }
      }
    

    Thanks again @kthorngren

This discussion has been closed.