To search on the contacts database, you can use the contacts. Find () method as follows:
Navigator. Contacts. Find (contactfields, onsuccess, onerror, options );
This example shows the following content:
1st parameters are used to pass a set of contact fields to the method. These fields are also returned. For example, if you want a personal name, email address, or phone number, you should specify it here. If you only want the contact ID, you can pass a null value to this parameter. If you want to return all fields, you can pass them to ["*"].
The 2nd parameters are the success callback function, which receives a correct contact object to save the contact information to be searched.
The first parameter is the error callback function, which will contain any errors thrown.
4th parameters are a set of options that can be passed to the find method. One of the most commonly used is the filter option, which is a string that can filter and query the contacts database.
The following is an example of finding the address book:
Function onsuccess (contacts ){
Document. Write (contacts. Length + 'contacts found .');
For (VAR I = 0; I <contacts. length; I ++ ){
For (var j = 0; j <contacts [I]. emails. length; j ++ ){
Document. Write ("email =" + contacts [I]. emails [J]. Email );
}
}
}
Function onerror (contacterror ){
Alert ('ooops! ');
}
// Search for all contacts with Gmail in the email address
VaR myoptions = new contactfindoptions ();
Myoptions. Filter = "Gmail ";
VaR myfields = ["emails"];
Navigator. Contacts. Find (myfields, onsuccess, onerror, myoptions );
Example 2:
$ ("# Page-contacts"). Live ("pageinit", function (){
VaR $ page = $ (this );
VaR fields = ["displayname", "phonenumbers", "Photos"];
Navigator. Contacts. Find (fields, function (contacts ){
Console. Log ("Address Book retrieved..." + contacts. Length );
VaR Results = [];
$. Each (contacts, function (I, c ){
Console. Log ("address book:" + C. displayname );
Results. push ("<li> <a href =''> });
$ (". Content-container", $ page). append (results. Join ("")). Listview ("refresh ");
}, Function (){
Alert ("An error occurred while searching the address book! ");
}, New contactfindoptions ());
});
Search for contacts