I am dynamically building the colModel for my jQGrid using fields from a DataTable. Nearly all of it works as I hoped. However, I am unable to use a custom summaryType because I can't serialize without the quotes and jQgrid doesn't look for the method if in quotes.
Presently, if I don't remove the ", I get the following error when loading the grid:
Uncaught jqGrid Grouping No such method: mysum
If I remove the quotes in the table, I get the following error when serializing:
Invalid JSON primitive: mysum.
What is the best way to tackle this problem?
You will probably have to create a solution in the view using javascript. If you manually serialize the colModel to send to the view, you will have to manually deserialize. You will not be able to parse it with a JSON parser because it will not be a valid JSON string.
One possible approach would be to use eval() on the string like this:
myObject.property = eval("mysum");
It should replace the string with the function. I am not sure if it meets your needs, but will avoid both errors you listed above.
Related
I have been trying to populate my variable bar with json fields from curl's POSTFIELDS attribute when invoking my workflow from an API using PHP. Below is a simple json passed when invoking the endpoint not as part of the URL but hidden POSTed data:
{"salesValue":5000,"authorId":2}
The properties above should be injected in Formatter Node where I generate the SQL statetement used by the ODBC driver to query our back-end database. I have been told that I can only do this, for now, by using the SCRIPT Node as I do not recall C# as having support for manipulating JSON Object out of the box. If I am behind with regards to that someone please lead me to an answer.
Question is: does Flowger support JSON Serialization, Deserialization, Decoding and/or encoding? There is a framework called JSON.Net for example. Can I use this if I want to manipulate my fgRequestBody property frfom my variable bar?
Try the below steps to get the desired results:
1 - Add a variable bar with two special properties: FgRequestBody and FgRequestContentType. Make sure that you specify the content type in the workflow, which will be application/json in your instance.
2 - Add a 'JSON Convert' directly after the start node and point your variable bar FgRequestBody to the input of Json on the Json Convert. This will convert json to xml.
3 - Add a 'XFormat' node and plug the xml output from the Json Convert to the 'XML Document' property. Right click on the node and add a new custom property with the name of the field that you would like to extract. In the custom property value, add the xpath to the value. In the Expression property of the node, add your sql statement e.g.
select * from tableName where name = '{customProperty}'
The results from this will be your sql query.
Troubleshooting Tip:
Use Postman Add-In (Chrome) or RESTClient (Firefox) to verify the results. You should see the node generation in the activity log within Flowgear. If you do not see this, then add a AllowedOrigin of * in your Flowgear Site properties. See the following for reference to this:http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
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.
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 :)
The overall objective is to get a Json representation of the query results of the SqlQuery executed. This Json will be used to create visualizations/reports on the browser using js based charting tools.
Now, controls like gridview are able to read the column names as well as the data and give us an html representation of the data. So I think it should be possible to write code such that it can read from a sql data reader and come up with a json representation.
I could not find anything in my searches which does what I want. How do I go about doing this? any pointers?
You could use an SqlDataAdapter to fill a DataSet. This blog post describes a way of converting a DataTable or DataSet into its JSON representation.
You could use the Json.Net serializer. It supports serializing a Dictionary<string,object> to a JSON object.
Another big shot would be using NHibernate and serializing the resulting objects.
Here is another link to using the Json.Net serializer for DataSets:
If you scroll down to the comments on this page you see a much shorter solution using the Dictionary approach.
I'm trying to define aoColumns using ajax and a C# webmethod. I am treating it very similarly to how I am passing in server-side data, using a List> data structure that I add rows of List to. My problem is that this results in a string like:
{\"aoColumns\":[[\"\\\"bVisible\\\": False\"],[\"\\\"bVisible\\\": True\"],[\"\\\"bVisible\\\": True\"],[\"\\\"bVisible\\\": True\"],[\"\\\"bVisible\\\": True\"],[\"\\\"bVisible\\\": True\"],[\"\\\"bVisible\\\": True\"],[\"\\\"bVisible\\\": True\"]]}
Which is nearly correct, except that the column definitions are using square brackets instead of {}. How would I go about generating the correct JSON text? Any help is greatly appreciated!
I am assuming you are using data tables from http://www.datatables.net/. Please correct me if I am wrong.
I am not sure I understand if you are having trouble creating the JSON string to return to the AJAX call or converting it into something usable on the server-side.
If you are going to create a JSON string in a web method, I would suggest using a Dictionary type, since they are so close to JSON strings. To convert a Dictionary type into a JSON string, use this:
var dictionary = new Dictionary<string, string>()
// add values here...
return new JavaScriptSerializer().Serialize(dictionary);
If you are converting a JSON string into a Dictionary object, use this:
var dictionary = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(jsonString);
Another thing I like to do is convert the dictionary into an array if I am going to be working with any keys or values since getting them from the dictionary can be a pain when you do not know the exact key value you want to work with.
For reference, the JavaScriptSerializer is part of the System.Web.Script.Serialization.JavaScriptSerializer namespace and in the System.Web.Extensions assembly.
This is the javascript code i tried to export my grid data in json. it throws an exception and i can't figure out why. when i downloaded the jqgrid i checked the import/export module.
I want to insert the json in a hidden field in order to get the data on the server side for validating and saving.
$('#proveObjekt2').val(JSON.stringify($("#rowed5").jqGridExport("jsonstring")));
...
<asp:HiddenField ID="proveObjekt2" runat="server" />
You use parameters of jqGridExport in the wrong form. The parameter must be an object with three possible properties: exptype, root and ident. Because default values of the parameters are
{
exptype : "xmlstring",
root: "grid",
ident: "\t"
}
the call $("#rowed5").jqGridExport("jsonstring") return XML instead of JSON. You should use
jQuery("#rowed5").jqGridExport({exptype:"jsonstring"})
or
jQuery("#rowed5").jqGrid('jqGridExport',{exptype:"jsonstring"})
instead.