0

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]   
4
  • try console.log(userList) to see what it actually contains Commented 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. Commented Dec 13, 2011 at 6:47
  • @VijayakrishnanK: i checked it contains object. Please check my edit. Commented Dec 13, 2011 at 6:49
  • 1
    what 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). Commented Dec 13, 2011 at 7:16

4 Answers 4

2
var userList = '${userList}';

userList is not an array.

Take out the quotes if it's supposed to render a javascript array.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried removing quotes. It gave me error 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, .....
@YetAnotherCoder - if that's the actual source, I don't think that's even valid javascript. I get an error if I try to use @ 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.
That might be the cause. I am using spring framework. Is there any other simple alternative to this ?
@YetAnotherCode - I'm not familiar with spring so you might want to ask a new question. You'd just need to pull the 4 attributes out you're trying to get: firstName, lastName, etc. and put those values into the array somehow. Where that happens in spring, I can't say...
1

Your if condition should probably be:

if(searchKey == "" || userList[i].firstName.indexOf(searchKey) != -1 || userList[i].lastName.indexOf(searchKey) != -1) {
    ...
}

Comments

0

I see an error in your code. for(i=0; i < users.length;i++) is it works?

2 Comments

Yes. Can you please elaborate it ?
if $.getJSON return userList is right, and users are same as userList. may be the error is this for(i=0; i< users.length; i++) => for(var i = 0; i < users.length; i++)
0

Used Ajax and Json for same and stored result in JS variable

userList = null; 
    $.getJSON("lookup/users", {name:"",userType:"requester"}, function(users) {
             userList = users;
         });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.