0

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" style="width:200px" value="' + data.record.Name + '" />';
        } else {
            return '<input type="text" name="Name" style="width:200px" 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 via json_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.

adrien54
  • 1,620
  • 1
  • 26
  • 31
therabbityouknow
  • 233
  • 2
  • 5
  • 17
  • 3
    json is for data, not functions. It doesnt work like that – Steve Jul 31 '14 at 15:44
  • 1
    (J)ava(S)cript (O)bject (N)otation. It's not (J)(S)(F)unction(N) – Marc B Jul 31 '14 at 15:52
  • this might help you: http://stackoverflow.com/questions/2573548/given-a-string-describing-a-javascript-function-convert-it-to-a-javascript-func –  Jul 31 '14 at 15:54
  • i wonder if a data.replace() on javascript side would work just to remove to quotes – therabbityouknow Jul 31 '14 at 17:04
  • gonna build a string with foreach and use eval. Thx Stefan Baiu, you may want to add this as an answer so i can accept it. :) – therabbityouknow Jul 31 '14 at 17:41
  • nah, I can't take credit for it, I only shared a link to someone else's answer, you can still +1 his answer though ^^ –  Jul 31 '14 at 18:00

1 Answers1

0

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 awful eval(...) stuff. Caveat: it's still not a method of the given object.

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44