Reputation: 96
I have the following code that I am using successfully to produce a list of all contacts on the phone or the results of a search of all contacts. I have it working with the name and phone number, where multiple records for one contact show up as multiple list entries, just the way I want it. The problem I am having, and maybe I am missing something obvious here, is that I can't get any other fields to show up in the list. I have tried adding an "or" to the j variable with the next field and while it returns the same results, as soon as I add the next field to the list it all breaks. I do consistently get the results, but I cannot get the results to display. I also tried to figure out a way to add another "for" loop, but as expected they always just filtered down the already filtered results, and usually just gave me one result. The js is below, any help is appreciated. I can't really do a fiddle as its Cordova.
// search below
var fields = ["givenName", "familyName", "name", "emails", "phoneNumbers", "addresses", "organizations"],
options = new ContactFindOptions();
var filter = $('#contacts_filter')[0].value
// set Options
options.filter = (filter && filter !== "Search All") ? filter : "";
options.limit = 15; //doesn't work for some reason
options.multiple = true;
//find function
navigator.contacts.find(fields, function (foundContacts) {
//if their are results
if (foundContacts.length > 0) {
$("#contact_list").html("<h5 style='text-align: center'>" + foundContacts.length + " results found.</h5>");
for (var i = 0; i < foundContacts.length; i++) {
if(null != foundContacts[i].phoneNumbers )
{
for(var j=0; j < (foundContacts[i].phoneNumbers.length); j++)
{
$('#contact_list').append("<li><h2>" + foundContacts[i].name.familyName + ", " + foundContacts[i].name.givenName + "</h2><p>" + foundContacts[i].addresses[j].streetAddress + "</p><p>" + foundContacts[i].phoneNumbers[j].value + "</p></li>");
}
}
}
} else {
$("#contact_list").html("<h5 style='text-align: center'>No Contacts Found!</h5>");
}
edit: extraneous id removed.
Upvotes: 1
Views: 1164
Reputation: 96
I figured out, through researching on the phoneGap forums. that I wasn't allowing for null answers in my loops. So adding if(foundContacts[i].addresses != null
solved the problem. Thanks for everyones help.
Upvotes: 1
Reputation: 627
if you're positive you're getting the results and that it not displaying then it's probably not an issue with your existing code but your dynamically added results. Make sure you're refreshing if you're using jquery mobile; as a side note, i've done something similar to your code before, and if you're having problems with each consecutive entry after the first one its because you aren't emptying the object i handled this by doing something like this in my code:
navigator.contacts.displayName.empty();
however i'm sure if you return false; it could potentially stop the data from bubbling up in the contacts object as well.
make sure you only use your id once as well, otherwise make it a class, every time it gets through that loop you're adding the same "contact_name" id.
Upvotes: 0