简体   繁体   中英

generate javascript code with php

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?

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM