1

I would like to dynamically generate the columns definition on a Datatable. The columns definitions are:

"columns": [
            { "data": "id", "orderable": false },
            { "data": "code" },
            { "data": "name" },
            { "data": "created", "render":
                    function (data) {
                        var date = new Date(data);

                        return date.toLocaleString();
                    }
            },
            { "data": "modified", "render":
                    function (data) {
                        var date = new Date(data);

                        return date.toLocaleString();
                    }
            }]

I tried generating the javascript code using an array of php objects and then json_encode it, like the following:

$formatted_cols = [];

foreach ($cols as $idx => $col){
    $temp = [];

    $temp['data'] = $col;

    if(in_array($col, array('id', 'actions'))){
        $temp['orderable'] = 'false';
    }

    if(in_array($col, array('created', 'modified'))){
        $temp['render'] = "
            function (data) {
                var date = new Date(data);

                return date.toLocaleString();
            }
        ";
    }

    $formatted_cols[] = $temp;
}

And then I do the following in the place where the code would normally appear:

echo json_encode($formatted_cols);

But the code came out like this:

[
  {
    "data": "id",
    "orderable": "false"
  },
  {
    "data": "code"
  },
  {
    "data": "name"
  },
  {
    "data": "created",
    "render": "\r\n            function (data) {\r\n                var date 
= new Date(data);\r\n    \r\n                return 
date.toLocaleString();\r\n            }\r\n        "
  },
  {
    "data": "modified",
    "render": "\r\n            function (data) {\r\n                var date 
= new Date(data);\r\n    \r\n                return date.toLocaleString();\r\n            }\r\n        "
   }
]

As you can see, with a bunch of \r\n and stuff. Anybody can help me get the desired output please?

Thanks in advance for any help

UPDATE

I removed the line breaks but still the functions are inside double quotes

{
    "data": "modified",
    "render": "function (data) {var date = new Date(data);return 
    date.toLocaleString();}"
  }

How do I remove those quotes?

3 Answers 3

1

Try using nl2br(). It'll take away all those \r and \n

http://php.net/manual/es/function.nl2br.php

Sign up to request clarification or add additional context in comments.

Comments

0

The \r\n "stuff" represents a carriage-return + linefeed combination, in other words a line break.

You're going to get these in the JSON data if you're trying to encode multi-line strings. JSON itself does not support multi-line strings without them like PHP does.

Comments

0

Remove your newlines, like this:

{ "data": "created", "render": "function (data) { var date = new Date(data); return date.toLocaleString(); }"  },

But you'll still be left with a string, which isn't the sort of thing you should work with, even though you can convert it to a function in JS. It's pretty ugly. Even if it worked, it's not great to generate JS in PHP - try to find another method if you can. Let PHP serve only the data, and have JS integrate functionality into it, if possible.

2 Comments

If this is JavaScript that will work. If this needs to be JSON it won't.
This will generate errors inside php code because the function is not inside double quotes

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.