javascript - Remove Quotes from json_encoded string value

233

I am working on a project to implement jTable into a PHP framwework class. It is going quite well. Now I am stuck at a problem where I would like to be able to make custom input fieds on the edit dialog.

We are already using the select2 plugin, now I want to implement this on the edit dialogs of jTable. As far as I understood it is possible to add custom fields to the edit dialog like this:

Name: {
  title: 'Name',
  width: '20%',
  input: function (data) {
    if (data.record) {
      return '<input type="text" name="Name" value="' + data.record.Name + '" />';
    } else {
      return '<input type="text" name="Name" value="enter your name here" />';
    }
  }
}

Notice the code above is in JavaScript. Basically what I do is that I build this javascript in php array and viajson_encode I send it to the client.

My problem here is that when I do

$column_array['name']['input'] = function (data) {if ....and so on}

I get on the javascript side

input: "function (data) {... so on"

Please notice the double quotes, it is a string and not a function anymore.

What I would need is to remove this 2 double quotes after the input. (well that's my idea so far)

OR if any one got some experience with jTable] and knows a better way to implement custom fields like select2, chosen, jQuery multiselect, elfinder and stuff like that.

I can give tomorrow some code if needed, cause I am not at work anymore today.

460

Answer

Solution:

Based on this concept:

// Create a function that takes two arguments and returns the sum of those arguments
var fun = new Function("a", "b", "return a + b");
// Call the function
fun(2, 6);
Output: 8

you may change your JSON a bit in to

Name: {
    title: 'Name',
    width: '20%',
    input: { name: "input",
             param: "data",
             body: "if (data.record) {
                   return '<input type=\".... "
            }
    ....

then you may define a function and execute it

var d = new Function(Name.input.name, Name.input.param, Name.input.body);
d(otherdata);

this will omit the awfuleval(...) stuff. Caveat: it's still not a method of the given object.

People are also looking for solutions to the problem: php - Laravel 4: prevent lazy load hasMany relation when foreign_key is null (optional relation)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.