How to convert kendo filter object to string - kendo-grid

When applied filter in kendo grid i am getting filter as object like below
filter : { filters : [], logic : "and"}
But in some kendo examples filter is displaying like
filter : "producatname~eq~test"
How to change filter from object to string

Assuming you are writing JavaScript code in a web page and want to convert the filter data to a string suitable for sending or saving, you can use transport.parameterMap to convert filters to a string.
const grid = $("#grid").data("kendoGrid");
const parameterMap = grid.dataSource.transport.parameterMap;
const filterString = parameterMap({filter: grid.dataSource.filter()}).filter
On the MVC side you can then use Kendo.Mvc.Infrastructure.FilterDescriptorFactory.Create(string) to parse the string.

Related

Why doesn't the json model's odata property have columns?

I am creating a JSON model in UI5 from a json object. But it is missing the columns array in the JSON model. I checked the model in the console and the odata property had no columns. Check image for proof.
https://ibb.co/hLmYpZb
Hence I want to know how I can get the column to show up.
Here is my code.
var emptyModel = {
"columns": []
};
this.setModel(new JSONModel(JSON.stringify(emptyModel)), "selcontent");
I expect column to show up in the JSON model under the odata property.
JSONModel awaits a plain object or a URL string as the first argument.
Either the URL where to load the JSON from or a JS object (source)
The result of the stringified emptyModel is "{"columns":[]}" which is neither an object nor a URL.

Ambiguous format output in nodejs

I am having output in following format as
"[{"a":"a1"},{"a":"a2"}]"
I want to actually extract it in array of json:
[
{
"a":"a1"
},
{
"a":"a2"
}
]
How to convert it?
You have tagged this with Node-RED - so my answer assumes that is the environment you are working in.
If you are passing a message to the Debug node and that is what you see in the Debug sidebar, that indicates your msg.payload is a String with the contents of [{"a":"a1"},{"a":"a2"}] - the Debug sidebar doesn't escape quotes when displaying strings like that.
So you likely already have exactly what you want - it just depends what you want to do with it next.
If you want to access the contents you need to parse it to a JavaScript Object. You can do this by passing your message through a JSON node.
Assuming your input contains the double quotes in the beginning and end, it is not possible to directly JSON.parse() the string.
In your case, you need to remove the first and last character (the double quotes) from your string before parsing it.
const unformattedString = '"[{"a":"a1"},{"a":"a2"}]"'
const formattedString = unformattedString.substr(1, unformattedString.length - 2)
const json = JSON.parse(formattedString)
The variable json now contains your JSON object.
I would suggest a different method which will get your work done without using any third party library.
var a = '[{"a":"a1"},{"a":"a2"}]';
var b = JSON.parse(a);
console.log(b); // b will return [{"a":"a1"},{"a":"a2"}]
Another way which is eval function which is generally not recommended
var d = eval(a);
If you want to use JQuery instead use :
var c = $.parseJSON(a);

want to bind string using comma separated using for loop

string json="[{\"ParentId\":\"a9764da3147845c184bd272cef6a5937\",\"Path\":\"/LMS/Cabinet/bdc2cd8e1da3451c84e332d1aa74f605\",\"CreatedBY\":\"admin\",\"IsActive\":\"Y\",\"CabinetName\":\"LMS\",\"FolderTag\":\"IT mobile computing,Comm Skills\",\"Name\":\"JAVA\",\"UpdatedBY\":\"\",\"Type\":\"Folder\",\"IsDelete\":\"N\",\"UpdatedON\":\"\",\"Id\":\"bdc2cd8e1da3451c84e332d1aa74f605\",\"CreatedON\":\"2015_09_08-11:19:50\",\"TemplateId\":\"dd42c8a71a954c1d948ef35492ee1242\" },{\"ParentId\":\"a9764da3147845c184bd272cef6a5937\",\"Path\":\"/LMS/Cabinet/3aae020c256f4dc1ab705af67bede2c7\",\"CreatedBY\":\"admin\",\"IsActive\":\"Y\",\"CabinetName\":\"LMS\",\"FolderTag\":\"IT mobile computing,Comm Skills\",\"Name\":\"Spring\",\"UpdatedBY\":\"\",\"Type\":\"Folder\",\"IsDelete\":\"N\",\"UpdatedON\":\"\",\"Id\":\"3aae020c256f4dc1ab705af67bede2c7\",\"CreatedON\":\"2015_09_04-16:58:05\",\"TemplateId\":\"dd42c8a71a954c1d948ef35492ee1242\"},{\"ParentId\":\"a9764da3147845c184bd272cef6a5937\",\"Path\":\"/LMS/Cabinet/c139b33a22a94a25bf624b94450aee3e\",\"CreatedBY\":\"admin\",\"IsActive\":\"Y\",\"CabinetName\":\"LMS\",\"FolderTag\":\"Social Skills\",\"Name\":\"SQL\",\"UpdatedBY\":\"\",\"Type\":\"Folder\",\"IsDelete\":\"N\",\"UpdatedON\":\"\",\"Id\":\"c139b33a22a94a25bf624b94450aee3e\",\"CreatedON\":\"2015_09_04-16:54:44\",\"TemplateId\":\"dd42c8a71a954c1d948ef35492ee1242\"}]";
This is my JSON string and I want to only "Name" field with comma separated into another variable.
For example, example:
string res={"JAVA","SPRING","SQL"}
What language are you trying to do this in? My first guess is javascript but i wasnt sure.
If its JavaScript, then you can get starten by turning the json string into a JavaScript object like this:
var jsonElements = JSON.parse (json);
From there its just looping and collecting the name property from each object.
Now we'll do this from C# instead
Ok, since you are doing the parsing from C# you would want this code instead:
string json = "[{\"ParentId\":\"a9764da3147845c184bd272cef6a5937\",\"Path\":\"/LMS/Cabinet/bdc2cd8e1da3451c84e332d1aa74f605\",\"CreatedBY\":\"admin\",\"IsActive\":\"Y\",\"CabinetName\":\"LMS\",\"FolderTag\":\"IT mobile computing,Comm Skills\",\"Name\":\"JAVA\",\"UpdatedBY\":\"\",\"Type\":\"Folder\",\"IsDelete\":\"N\",\"UpdatedON\":\"\",\"Id\":\"bdc2cd8e1da3451c84e332d1aa74f605\",\"CreatedON\":\"2015_09_08-11:19:50\",\"TemplateId\":\"dd42c8a71a954c1d948ef35492ee1242\" },{\"ParentId\":\"a9764da3147845c184bd272cef6a5937\",\"Path\":\"/LMS/Cabinet/3aae020c256f4dc1ab705af67bede2c7\",\"CreatedBY\":\"admin\",\"IsActive\":\"Y\",\"CabinetName\":\"LMS\",\"FolderTag\":\"IT mobile computing,Comm Skills\",\"Name\":\"Spring\",\"UpdatedBY\":\"\",\"Type\":\"Folder\",\"IsDelete\":\"N\",\"UpdatedON\":\"\",\"Id\":\"3aae020c256f4dc1ab705af67bede2c7\",\"CreatedON\":\"2015_09_04-16:58:05\",\"TemplateId\":\"dd42c8a71a954c1d948ef35492ee1242\"},{\"ParentId\":\"a9764da3147845c184bd272cef6a5937\",\"Path\":\"/LMS/Cabinet/c139b33a22a94a25bf624b94450aee3e\",\"CreatedBY\":\"admin\",\"IsActive\":\"Y\",\"CabinetName\":\"LMS\",\"FolderTag\":\"Social Skills\",\"Name\":\"SQL\",\"UpdatedBY\":\"\",\"Type\":\"Folder\",\"IsDelete\":\"N\",\"UpdatedON\":\"\",\"Id\":\"c139b33a22a94a25bf624b94450aee3e\",\"CreatedON\":\"2015_09_04-16:54:44\",\"TemplateId\":\"dd42c8a71a954c1d948ef35492ee1242\"}]";
dynamic jsonObj = (JArray)JsonConvert.DeserializeObject(json);
List<string> names = new List<string>();
foreach (JObject item in jsonObj)
{
names.Add(item["Name"].ToString());
}
Deserialize the string and cast it to a JArray. Then foreach element in the array, we look at the Name property of the element. I tested this code in visual studio and it appears to work.

Formatting date field in KendoUI Grid coming as object

I am trying to format a date field in KendoUI Grid where the field is coming as an object:
"callStart":{"date":"2014-01-24 12:04:36","timezone_type":3,"timezone":"Europe\/Berlin"}
I have tried:
{ field: "callStart", title: "Fecha", width: "100px", format: "{0:yyyy-MM-dd}" }
but still showing:
[object Object]
Any idea?
Thanks!
Don't know if you already solved it, but I'll answer late anyways. The reason why it's not working is because you are probably trying to bind the entire object callStart into the field column. The field expects only a date object, but you are not giving it one.
Also, your object seems to still be in JSON string format, you need to parse the object out as a first step (if it really is just raw JSON). Then the next step, you can:
Parse callStart on the columns themselves (with kendo Templates)
Parse callStart on the datasource itself through its schema
Option 1: Parse on the column field itself using a template
{
field: "callStart",
title: "Fecha",
width: "100px",
template: "#= kendo.toString(kendo.parseDate(callStart.date, 'yyyy-MM-dd HH:mm:ss'), 'MM/dd/yyyy') #"
}
The advantage of this option is that your data source object still maintains its original form, but filtering and sorting may get a bit tricky.
Option 2: Parse the object through the dataSource schema
var gridDS = new kendo.data.DataSource({
data: result,
schema: {
parse: function (result) {
for (var i = 0; i < result.length; i++) {
var resultItem = result[i];
resultItem.callStart = kendo.parseDate(result[i].callStart.date, 'yyyy-MM-dd HH:mm:ss');
}
return result;
}
},
//etc...
});
Each data source object goes through the parse function and you can do whatever processing you need to do to turn it into a JS date or kendo Date object. The advantage is that you can control the exact type for that column and makes filtering/sorting easier.
You'll probably have to do some tweaking to get your desired output, but these are the general options you need to pick from.

XPath for Json String

I have JSON string like below :
{
"ConfigurationJsonResult":
[
2246,
2247,
2248,
2249,
2250,
2251,
2252,
2253,
2254,
2255
]
}
My problem is i want to get 1st value i.e 2246 using xPath.
I have tried using /ConfigurationJsonResult[1] but it gives me [2246,2247.....2255], i just want 2246. How to achieve it.
You can write a mapping function to convert xpath to jsonpath and use converted string to access the data.
function xpathToJsonPath(xpath) {
var jsonPath = xpath.replace("//","..");
//Simillarly other mappings
return jsonPath;
}
var data = jsonPath(object, xpathToJsonPath("$..author"));
Mappings are given the link you have shared - http://goessner.net/articles/JsonPath/
You can do plenty of improvements on this like extending this as a function to prototype etc.