1

I have DataTable loaded dynamically, all columns are coming from Database in JSON format which aoColumns required.

I can render any column like this:

"aoColumns": [
{
    "sName": "I_CPN",
    "fnRender": function (oObj) 
    {
        return a button
    }
}]

Where sName is my Column name, this is static, I can get above content for aoColumns dynamically from C# code in JSON format. My problem is I am not able to put fnRender in JSON object.

How can I get fnRender so that I can render my Column as button.

BanksySan
  • 27,362
  • 33
  • 117
  • 216
dPatel1582
  • 124
  • 10

1 Answers1

1

JSON format can not carry function objects. Just some limited set of plain datatypes (see http://www.json.org) and everything else that can be serialized into string and deserialized back.

So you might transfer the function as string and then use eval on the JavaScript front-end to turn it back to code. See e.g. JSON.parse vs. eval() for examples of how to use eval.

Although this is possible it somehow breaks the original JSON as simple data container design.

You can also write some kind of generic fnRender function that will for instance take the column name and find corresponding button element using the getElementById() function.

Your example code is to small to provide better answer (in order to generalize we must have at least 2 examples :)

Community
  • 1
  • 1
xmojmr
  • 8,073
  • 5
  • 31
  • 54
  • 1
    Hello xmojmr, You are correct, we cannot pass function with JSON object as this is serialized into string. also eval() is not good to use. So I followed different approach, I have generated java-script as string in C# portion and dump it to view when my dynamic table require script. As part of the script is dynamic so I did it C# side where I can easily interact with database. – dPatel1582 May 13 '14 at 18:18