1

Using Datatables with a .json source file, I am trying to manipulate the data before it shows in the table. I'm trying to simply remove spaces and replace with a dash in this example.

There are two ways I understand to do some data manipulation. One is columnDefs the other is using dataSrc and return the data. Both fail when i try to utilize .split or .replace or even .toLowerCase()...

For example, I have columnDefs added like so:

 columnDefs: [
            {
                "render": function ( data, type, row ) {
                  console.log(data);
                  var cn = data.split(" ").join("-").toLowerCase();
                  return cn;
                },
                "targets": 1
            }
        ],

The console shows:

Uncaught TypeError: data.split is not a function

How do we manipulate the data with replace or the like?

My data looks like:

{
    "Licensee": "Whistles for Woods",
    "Contact Name": "Bob",
    "Street": "2230 Trail",
    "Suite / PO Box": 0,
    "City": "Alturas",
    "ST": "CA",
    "Zip Code": 997733,
    "Telephone": 0,
    "Email Address": "[email protected]",
    "Website Address": "www.domain.com",
    "Fax": "No fax",
    "Products": "whistle with custom logo",
    "Categories": "Miscellaneous"
  },
4
  • 1
    How is your data formatted? If the data value is not a string or an array than it wont have the functions you mentioned. You will likely have to perform more in depth data manipulation to achieve what you want. Commented May 23, 2019 at 16:29
  • @MichaelSorensen thanks for the comment. I added it in there. It is a string. Commented May 23, 2019 at 16:32
  • ah... you made me think there! Trying console.log(typeof data) told me most of the items are string, however, there was a number in there... I made data.toString() and now I'm able to manipulate it... I didn't think that would be necessary, however, this is a good check for sure since this isn't clean data always... Commented May 23, 2019 at 16:43
  • 1
    Nice! I was just about to suggest that lol. In that case I would just make an if statement like if(typeof data === 'string'){ //do split and join } and continue on with my day :) Commented May 23, 2019 at 16:46

1 Answer 1

1

As discussed in the comments

We just want to make sure that we are indeed manipulating strings and not any other data type. So in this case we would change the code to look something like this:

 columnDefs: [
        {
            "render": function ( data, type, row ) {
              if(typeof data === 'string'){ 
                  //only if string manipulate
                  data = data.split(" ").join("-").toLowerCase();
              }
              // OR data = data.toString(); whichever is more convenient!
              return data;
            },
            "targets": 1
        }
    ],
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.