Thursday, 8 August 2013

phonegap contacts next record for same contact

phonegap contacts next record for same contact

I have an app using phone gap that has a basic form in it (Company, name.
address, etc.). So the user doesn't always have to type it in, I have
linked it to the phones address book. The problem I have is two fold. If a
contact has more that one address, etc. I want the user to be able to tap
through the entries and use the one they want. I have that kind of
working, except my variable (j) won't reset to i += 1 when you start a new
search. Also for some address book records it doesn't pull up any address
fields even though there are multiple for the contact. I can't quite
figure out how the addressbook fields work. my noob javascript is below.
Thanks for any guidance or help.
var i = 0;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
$('#searchbut').bind('touchstart', function() {
var i = 0;
$('#contact').val('');
$('#email').val('');
$('#Phone').val('');
$('#address').val('');
$('#city').val('');
$('#state').val('');
$('#zipcode').val('');
$('#company').val('');
var fields = ['*'];
var options = {
filter: $( '#search' ).attr( 'value'),
multiple: true
};
navigator.contacts.find(fields, function( contacts ) {
$('#contact').attr('value', contacts[0].name.formatted );
$('#email').attr('value', contacts[0].emails[i].value );
$('#Phone').attr('value', contacts[0].phoneNumbers[i].value );
$('#address').attr('value',
contacts[0].addresses[i].streetAddress);
$('#city').attr('value', contacts[0].addresses[i].locality);
$('#state').attr('value', contacts[0].addresses[i].region);
$('#zipcode').attr('value', contacts[0].addresses[i].postalCode);
$('#company').attr('value', contacts[0].organizations[i].name);
}, function( error ) {
alert("Doh! Contact Link Failed!");
},
options );
} );
$('#nextbut').bind('touchstart', function() {
var fields = ['*'];
var options = {
filter: $( '#search' ).attr( 'value'),
multiple: true
};
navigator.contacts.find(fields, function( contacts ) {
var j = i += 1;
$('#contact').attr('value', contacts[0].name.formatted );
$('#email').attr('value', contacts[0].emails[j].value );
$('#Phone').attr('value', contacts[0].phoneNumbers[j].value );
$('#address').attr('value',
contacts[0].addresses[j].streetAddress);
$('#city').attr('value', contacts[0].addresses[j].locality);
$('#state').attr('value', contacts[0].addresses[j].region);
$('#zipcode').attr('value', contacts[0].addresses[j].postalCode);
$('#company').attr('value', contacts[0].organizations[j].name);
}, function( error ) {
alert("Doh! Contact Link Failed!");
},
options );
} );$('#prevbut').bind('touchstart', function() {
var fields = ['*'];
var options = {
filter: $( '#search' ).attr( 'value'),
multiple: true
};
navigator.contacts.find(fields, function( contacts ) {
var j = i -= 1;
$('#contact').attr('value', contacts[0].name.formatted );
$('#email').attr('value', contacts[0].emails[j].value );
$('#Phone').attr('value', contacts[0].phoneNumbers[j].value );
$('#address').attr('value',
contacts[0].addresses[j].streetAddress);
$('#city').attr('value', contacts[0].addresses[j].locality);
$('#state').attr('value', contacts[0].addresses[j].region);
$('#zipcode').attr('value', contacts[0].addresses[j].postalCode);
$('#company').attr('value', contacts[0].organizations[j].name);
}, function( error ) {
alert("Doh! Contact Link Failed!");
},
options );
} );
}

No comments:

Post a Comment