Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
var resultTable = $('#templateRegArea').DataTable({
"columns": [
{ data: "ID" },
{ data: "Name" },
{ data: "Description" },
],
});
resultTable.rows.add(response.d).draw();
dataSet = response.d;
I am trying to bind a table 'templateRegArea' with the values present in "response.d" in a Jquery datatable.
response.d
I have attached the value of "response". The problem I am facing is the data are not getting loaded into the table.
HELP :(
Your server-side script produces objects, when jQuery DataTables requires array of arrays or array of objects. For example:
{
"d": [{
"ID": "1",
"Name": "John",
"Description": "Test"
}, {
"ID": "2",
"Name": "Bob",
"Description": "Test"
}]
}
When you correct the data structure as shown below, initialization code should be changed to:
var resultTable = $('#templateRegArea').DataTable({
"data": response.d,
"columns": [
{ "data": "ID" },
{ "data": "Name" },
{ "data": "Description" }
]
});
With the existing data structure you can use the code below, but that is only good for one data row.
var resultTable = $('#templateRegArea').DataTable({
"data": [response.d],
"columns": [
{ "data": "ID" },
{ "data": "Name" },
{ "data": "Description" }
]
});
Related
I'm trying to loop my child array but it's not working. It's working fine which it's first items but not working on the child elements.
"candidateApplication": [
{
"label": "Are you currently attending school?",
"id": "ap_currentschool",
"value": "",
"required": "N",
"type": "radio",
"display": null,
"list": [
{
"searchCode": "yes",
"searchValue": "yes"
},
{
"searchCode": "no",
"searchValue": "no"
}
],
"onSelect": [
{
"yes": [
{
"label": "Estimated Completion Date",
"id": "ap_compdate",
"value": "",
"required": "N",
"type": "date",
"display": null,
"list": null,
"onSelect": null
}
],
"no": null
}
]
},
]
I am easily access my data to looping lists like obj.candidateApplication.list But I am not able to loop through obj.candidateApplication.onSelect.yes.
Can anybody please guide me how can I fix that issue.
enter image description here
You are accessing obj.candidateApplication.onSelect.yes which is undefined because obj.candidateApplication.onSelect is an array of objects.
You probably want to loop over obj.candidateApplication.onSelect and then for each object you can access the yes property as you wish:
$.each(onSelect, function (index, element) {
$.each(element.yes, function (x, y) {
// your code here
})
});
Edit: As pointed by Heritic Monkey in the comments, obj.candidateApplication is an array as well, but in the picture attached to the question, there's already a local variable called onSelect, loop through that instead of obj.candidateApplication.onSelect because it's undefined. (at least it should be, we don't know what your code is doing)
I have implemented a jquery datatable in asp mvc. The records are being fetched correctly, and the sorting working correctly. The data table operates via the server side.
However, I encountered a small issue. When I am on page 3 of the datatable, I perform a sorting but the datable refreshes, returns to page 1 and only sorts records on page 1. What I want to achieve is to make sorting only on the current page that I am.
I have tried making the stateSave to true like: But the issue persists.
var table = $('#employeetable').DataTable({
"ajax": {
"url": "/Employee/GetList",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "Name", "name": "Name" },
{ "data": "Office", "name": "Office" },
{ "data": "Position", "name": "Position" },
{ "data": "Age", "name": "Age" },
{ "data": "Salary", "name": "Salary" },
],
"serverSide": true,
"order": [0, "asc"],
"processing": true,
"stateSave": true
});
Can someone please help to resolve this ?
When you sort with DataTables, you sort the entire data set, not just the visible values. So any change in the sort order, would naturally be a new list, so would return you to page 1. It's not possible to just search within the current page, thereby keeping the pagination identical. See this thread here, where more details are given as to why this isn't (and shouldn't be) possible.
I am working with facebook JS SDK which returns user's information in JSON format. I know how to get the response like response.email which returns email address. But how to get an element from a nested array object? Example: user's education history may contain multiple arrays and each array will have an element such as "name" of "school". I want to get the element from the last array of an object.
This is a sample JSON I got:-
"education": [
{
"school": {
"id": "162285817180560",
"name": "Jhenaidah** School"
},
"type": "H**hool",
"year": {
"id": "14404**5610606",
"name": "2011"
},
"id": "855**14449421"
},
{
"concentration": [
{
"id": "15158**968",
"name": "Sof**ering"
},
{
"id": "20179020**7859",
"name": "Dig**ty"
}
],
"school": {
"id": "10827**27428",
"name": "Univer**g"
},
"type": "College",
"id": "9885**826013"
},
{
"concentration": [
{
"id": "108196**810",
"name": "Science"
}
],
"school": {
"id": "2772**996993",
"name": "some COLLEGE NAME I WANT TO GET"
},
"type": "College",
"year": {
"id": "1388*****",
"name": "2013"
},
"id": "8811215**16"
}]
Let's say I want to get "name": "some COLLEGE NAME I WANT TO GET" from the last array. How to do that with Javascript? I hope I could explain my problem. Thank you
Here is a JsFiddle Example
var json = '{}' // your data;
// convert to javascript object:
var obj = JSON.parse(json);
// get last item in array:
var last = obj.education[obj.education.length - 1].school.name;
// result: some COLLEGE NAME I WANT TO GET
If your json above was saved to an object called json, you could access the school name "some COLLEGE NAME I WANT TO GET" with the following:
json.education[2].school.name
If you know where that element is, then you can just select it as already mentioned by calling
var obj = FACEBOOK_ACTION;
obj.education[2].school.name
If you want to select specifically the last element, then use something like this:
obj.education[ obj.education.length - 1 ].scool.name
Try this,
if (myData.hasOwnProperty('merchant_id')) {
// do something here
}
where JSON myData is:
{
amount: "10.00",
email: "someone#example.com",
merchant_id: "123",
mobile_no: "9874563210",
order_id: "123456",
passkey: "1234"
}
This is a simple example for your understanding. In your scenario of nested objects, loop over your JSON data and use hasOwnProperty to check if key name exists.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i have array value. need to convert this array value into json format. example is given bleow
Sample Array
[Management Portal!#!#Production Issue Handling!#!#/IONSWeb/refDataManagement/searchDynamicScripts.do, Management Portal!#!# Event Browser!#!#/IONSWeb/orderManagement/eventBrowser.do, Management Portal!#!# Order Workflow!#!#/IONSWeb/orderManagement/SearchOrdersWorkflow.do, ADMINISTRATION!#!#Admin Message!#!#/IONSWeb/userManagement/getMessageForBroadcast.do, ADMINISTRATION!#!#Audit!#!#/IONSWeb/userManagement/auditManagement.do, ADMINISTRATION!#!#Locks!#!#/IONSWeb/userManagement/lockSearch.do, ADMINISTRATION!#!#Queue!#!#/IONSWeb/GroupManagement/begin.do, ADMINISTRATION!#!#Role!#!#/IONSWeb/userManagement/goToRolePage.do, ADMINISTRATION!#!#Routing Rule!#!#/IONSWeb/ruleManagement/showRules.do, ADMINISTRATION!#!#Task Code!#!#/IONSWeb/ManageTaskCode/begin.do, ADMINISTRATION!#!#Trigger OutEvent!#!#/IONSWeb/triggerOutEvent.jsp, ADMINISTRATION!#!#User!#!#/IONSWeb/userManagement/begin.do, ADMINISTRATION!#!#Refresh Application Cache!#!#/IONSWeb/userManagement/refreshApplnCache.do]
sample Json
{
"name": "Administration",
"sub": [
{
"name": "Add Order",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"name": "Infrastructure sonet Add Order ",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"name": "fGNS Add Order",
"url": "/IONSWeb/userManagement/auditManagement.do"
}
]
}
Please anyone help on this
I think you want to do something like this. Split the string, get out the first element, that will be the name, and iterate through all the elements. Every even value will be the name, and every odd an url.
When it is odd, then add it to the sub array. Thats it.
var string = 'Management Portal!#!#Production Issue Handling!#!#/IONSWeb/refDataManagement/searchDynamicScripts.do, Management Portal!#!# Event Browser!#!#/IONSWeb/orderManagement/eventBrowser.do, Management Portal!#!# Order Workflow!#!#/IONSWeb/orderManagement/SearchOrdersWorkflow.do, ADMINISTRATION!#!#Admin Message!#!#/IONSWeb/userManagement/getMessageForBroadcast.do, ADMINISTRATION!#!#Audit!#!#/IONSWeb/userManagement/auditManagement.do, ADMINISTRATION!#!#Locks!#!#/IONSWeb/userManagement/lockSearch.do, ADMINISTRATION!#!#Queue!#!#/IONSWeb/GroupManagement/begin.do, ADMINISTRATION!#!#Role!#!#/IONSWeb/userManagement/goToRolePage.do, ADMINISTRATION!#!#Routing Rule!#!#/IONSWeb/ruleManagement/showRules.do, ADMINISTRATION!#!#Task Code!#!#/IONSWeb/ManageTaskCode/begin.do, ADMINISTRATION!#!#Trigger OutEvent!#!#/IONSWeb/triggerOutEvent.jsp, ADMINISTRATION!#!#User!#!#/IONSWeb/userManagement/begin.do, ADMINISTRATION!#!#Refresh Application Cache!#!#/IONSWeb/userManagement/refreshApplnCache.do';
var pieces = string.split('!#!#');
var first = pieces[0];
//Get out the first one, that will be the key
pieces.shift();
//Create the object
var object = {
'name': first,
'sub': []
};
//Iterate through elements
var i = 0;
var sub = [];
$.each(pieces, function (idx, piece) {
if (i % 2 == 0) {
sub['name'] = piece;
} else {
sub['url'] = piece;
object.sub.push(sub);
}
i++;
});
console.log(object);
Try something like this:
Fiddle: https://jsfiddle.net/ug85d7o7/6/
var jsonData = [],
item, name,
subItem, subUrl,
i, j,
a = [
"Management Portal!#!#Production Issue Handling!#!#/IONSWeb/refDataManagement/searchDynamicScripts.do",
"Management Portal!#!# Event Browser!#!#/IONSWeb/orderManagement/eventBrowser.do",
"Management Portal!#!# Order Workflow!#!#/IONSWeb/orderManagement/SearchOrdersWorkflow.do",
"ADMINISTRATION!#!#Admin Message!#!#/IONSWeb/userManagement/getMessageForBroadcast.do",
"ADMINISTRATION!#!#Audit!#!#/IONSWeb/userManagement/auditManagement.do",
"ADMINISTRATION!#!#Locks!#!#/IONSWeb/userManagement/lockSearch.do",
"ADMINISTRATION!#!#Queue!#!#/IONSWeb/GroupManagement/begin.do",
"ADMINISTRATION!#!#Role!#!#/IONSWeb/userManagement/goToRolePage.do",
"ADMINISTRATION!#!#Routing Rule!#!#/IONSWeb/ruleManagement/showRules.do",
"ADMINISTRATION!#!#Task Code!#!#/IONSWeb/ManageTaskCode/begin.do",
"ADMINISTRATION!#!#Trigger OutEvent!#!#/IONSWeb/triggerOutEvent.jsp",
"ADMINISTRATION!#!#User!#!#/IONSWeb/userManagement/begin.do",
"ADMINISTRATION!#!#Refresh Application Cache!#!#/IONSWeb/userManagement/refreshApplnCache.do"
];
for(i=0; i<a.length; i++)
{
item = a[i].split("!#!#");
name = item[0];
subName = item[1];
subUrl = item[2];
subItem = null;
for (j=0; j<jsonData.length; j++)
{
if (jsonData[j].Name == name)
{
subItem = jsonData[j].sub;
break;
}
}
if (!subItem)
{
jsonData.push({"Name" : name, "sub" : [] });
subItem = jsonData[jsonData.length-1].sub;
}
subItem.push({"Name" : subName, "url" : subUrl });
}
alert(JSON.stringify(jsonData));
Result:
[
{
"Name": "Management Portal",
"sub": [
{
"Name": "Production Issue Handling",
"url": "/IONSWeb/refDataManagement/searchDynamicScripts.do"
},
{
"Name": " Event Browser",
"url": "/IONSWeb/orderManagement/eventBrowser.do"
},
{
"Name": " Order Workflow",
"url": "/IONSWeb/orderManagement/SearchOrdersWorkflow.do"
}
]
},
{
"Name": "ADMINISTRATION",
"sub": [
{
"Name": "Admin Message",
"url": "/IONSWeb/userManagement/getMessageForBroadcast.do"
},
{
"Name": "Audit",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"Name": "Locks",
"url": "/IONSWeb/userManagement/lockSearch.do"
},
{
"Name": "Queue",
"url": "/IONSWeb/GroupManagement/begin.do"
},
{
"Name": "Role",
"url": "/IONSWeb/userManagement/goToRolePage.do"
},
{
"Name": "Routing Rule",
"url": "/IONSWeb/ruleManagement/showRules.do"
},
{
"Name": "Task Code",
"url": "/IONSWeb/ManageTaskCode/begin.do"
},
{
"Name": "Trigger OutEvent",
"url": "/IONSWeb/triggerOutEvent.jsp"
},
{
"Name": "User",
"url": "/IONSWeb/userManagement/begin.do"
},
{
"Name": "Refresh Application Cache",
"url": "/IONSWeb/userManagement/refreshApplnCache.do"
}
]
}
]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a javascript object in the below format. I want to combine Name and Status for each block and store in an array.
{
"datatype": "local",
"data": [
{
"Name": "John",
"status": "To Be Processed",
"LastUpdatedDate": "2013-05-31 08:40:55.0"
},
{
"Name": "Paul",
"status": "To Be Processed",
"LastUpdatedDate": "2013-06-02 16:03:00.0"
}
}
Desired Output
John + To Be Processed
Paul + To Be Processed
Please Help ?
NOTE: Your json is not valid. It should be
"data": [{...},{...}] // ] is mising
var data = {
"datatype": "local",
"data": [
{
"Name": "John",
"status": "To Be Processed",
"LastUpdatedDate": "2013-05-31 08:40:55.0"
},
{
"Name": "Paul",
"status": "To Be Processed",
"LastUpdatedDate": "2013-06-02 16:03:00.0"
}]
};
var len = data.data.length;
var txt = "";
for(var i=0;i<len;i++){
txt += "<div>"+data.data[i].Name+" "+data.data[i].status+"</div>";
}
$("body").append(txt);
DEMO here.
You can use $.map() to create a list of processed values
var data = {
"datatype": "local",
"data": [{
"Name": "John",
"status": "To Be Processed",
"LastUpdatedDate": "2013-05-31 08:40:55.0"
}, {
"Name": "Paul",
"status": "To Be Processed",
"LastUpdatedDate": "2013-06-02 16:03:00.0"
}]
};
var result = $.map(data.data, function(obj){
return obj.Name + ' ' + obj.status
});
console.log(result)