I have Javascript array which contains User object. I have created this array from modelAttribute.
var userList = '${userList}'; // userList is a spring model attribute
userList contains list of User objects. I am accessing it as
for(i=0;i<userList.length;i++)
{
if(searchKey == "" || userList[i].indexOf(searchKey) != -1)
{
$('#userTable').dataTable().fnAddData( [
userList[i].firstName,
userList[i].lastName,
userList[i].institution,
userList[i].email] );
}
}
But I am getting values as undefined. Initially I used Ajax call for same and it worked fine .
$.getJSON("lookup/users", {name:searchKey,userType:"requester"}, function(userList) {
// It works fine
for(i=0;i<userList.length;i++)
{
$('#userTable').dataTable().fnAddData( [
userList[i].firstName,
userList[i].lastName,
userList[i].institution,
userList[i].email] );
}
});
How can I access it now ?
EDIT:
console.log("userList :" + userList); gives
userList : [org.test.dto.UserDTO@11d1c59, org.test.dto.UserDTO@302f39, org.test.dto.UserDTO@16c57b1]
-
try console.log(userList) to see what it actually containshaynar– haynar2011年12月13日 06:38:43 +00:00Commented Dec 13, 2011 at 6:38
-
yes, try finding out the exact structure of the variable userList after you populate it. The structure could be different from what you think it is. Use console.log or put a break point in firebug after the assignment and check your watch block) or simply type userList in firebug console.techfoobar– techfoobar2011年12月13日 06:47:36 +00:00Commented Dec 13, 2011 at 6:47
-
@VijayakrishnanK: i checked it contains object. Please check my edit.Ajinkya– Ajinkya2011年12月13日 06:49:20 +00:00Commented Dec 13, 2011 at 6:49
-
1what does "typeof userList" give? If it gives you a string, you will not be able to read it out as an array. In this case, you'll need to change the way data is being fed (a good way will be emulate how your AJAX handler lookup/users returns the data in JSON form).techfoobar– techfoobar2011年12月13日 07:16:27 +00:00Commented Dec 13, 2011 at 7:16
4 Answers 4
var userList = '${userList}';
userList is not an array.
Take out the quotes if it's supposed to render a javascript array.
4 Comments
Error: missing ] after element list Source File: http://localhost:7009/portal/request/form/add Line: 1006, Column: 56 Source Code: var userList = [org.test.dto.UserDTO@3c96a9, .....@ anywhere. It looks to me like you need to serialize your data from whatever framework you're using to generate this list. org.test.dto.UserDTO@11d1cd59 may be an object in your framework, but it's just an undefined / illegal name in javascript. Take a look at what the server responded in your getJSON function - you'll see properly serialized JSON you can use as a template.spring framework. Is there any other simple alternative to this ?firstName, lastName, etc. and put those values into the array somehow. Where that happens in spring, I can't say...Your if condition should probably be:
if(searchKey == "" || userList[i].firstName.indexOf(searchKey) != -1 || userList[i].lastName.indexOf(searchKey) != -1) {
...
}
Comments
I see an error in your code. for(i=0; i < users.length;i++) is it works?
Used Ajax and Json for same and stored result in JS variable
userList = null;
$.getJSON("lookup/users", {name:"",userType:"requester"}, function(users) {
userList = users;
});