0

I have JSON data that's rendered via a DataTable and in one of the columns the text shows up as capitalized, and I want it to be like This where only the first letter is uppercase.

As per other posts on SO, it was recommended that .toUppercase and .slice be used. Based on what I have I feel that my code should be working, but for some reason it isn't. Since I'm working with DataTables I have had to go over a few hurdles to get results, so I'm wondering if DT is at the root of the problem as well.

JS snippet:

$(document).ready(function() {
        $('#matters-table').DataTable({
            columns: [
                { data: "0" },
                { data: "1" },
                { data: "Status" }, // is in all caps when rendered
                { data: "3" }
            ],
            columnDefs: [
                {"targets":2,"render": function(data) {
                    return data[0].toUpperCase() + data.slice(1);
                }}
            ],
            data: mattsText,
            ... // --------------- irrelevant info.

Any thoughts on this one?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Bodrov
  • 840
  • 1
  • 15
  • 29
  • what do you mean by "it isn't (working)"? Please share your input data, the expected output, and the actual output. – Robin Zigmond Feb 20 '19 at 22:52
  • Expected output: Lookslikethis --- Actual output: LOOKSLIKETHIS . The text looks like the actual output by default. – Bodrov Feb 20 '19 at 22:53
  • and what's the input? (I ask because it's possible it's already all capitalised, even if you don't think it is.) – Robin Zigmond Feb 20 '19 at 22:54
  • Please [edit] your question, preferably reducing the code to a [mcve]. Note that you can use [Stack Snippets](https://meta.stackoverflow.com/q/358992/215552) to do so. – Heretic Monkey Feb 20 '19 at 22:54
  • The input is in all caps by default. Sorry for the confusion. – Bodrov Feb 20 '19 at 22:55
  • What do you get when you do do console.log(data); ? – Shaharyar Kirmani Feb 20 '19 at 22:56
  • Please also link to the posts that suggest using `toUpperCase` and `slice`. It looks like you're implementing it differently, since those functions should be run on the same string, and you're applying the first to `data[0]` and the second to `data` (no `[0]`)... – Heretic Monkey Feb 20 '19 at 22:58
  • well if the input is all in caps, you want to convert all but the first letter to lowercase. If you do `return data[0].toUpperCase() + data.slice(1).toLowerCase();` then you'll get the casing you want no matter what mixture of cases the input is in – Robin Zigmond Feb 20 '19 at 22:58
  • Possible duplicate of [Convert string to title case with JavaScript](https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) – Heretic Monkey Feb 20 '19 at 22:59

1 Answers1

1

Use the following capitalize function as your render function (note the added split):

capitalize = str => str.split('')[0].toUpperCase() + str.slice(1);

console.log(capitalize('hello world'));
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84