Home > Web Front-end > H5 Tutorial > body text

Detailed explanation of HTML5 address book sample code for obtaining information of multiple specified people

黄舟
Release: 2017-03-25 10:36:17
Original
2254 people have browsed it

This article mainly introduces the detailed explanation of HTML5+ address book to obtain the information of multiple specified people. It is of great practical value and friends in need can refer to it.

This article introduces the HTML5 address book to obtain the information of multiple specified people. The details are as follows:

1. Obtaining the information of multiple people: Before importing the information of multiple people in the address book, it is necessary to solve the problem of obtaining information. Information about multiple individuals. I obtained the IDs and displayNames of all contacts in the address book through the application of plus.contacts.getAddressBook and address.find, and then displayed them through the address book acquisition page I wrote.

1. To solve this problem, first you have to write a js address book yourself, so that the initials of all your contacts can be separated, and you can jump to the initials you want next to them.

2. Solve the problem of obtaining all contact information

plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) { //获取通讯录信息
                // 可通过addressbook进行通讯录操作
                addressbook.find(null, function(contacts) {
                    var username = new Array();
                    var LinkList = new LinkedList();
                    if(contacts.length > 0) { //获取当前通讯录里面所有人
                        for(var i = 0; i < contacts.length; i  ) {
                            username[i] = contacts[i].displayName   "-"   contacts[i].id; //连接id和username,为后面筛选最准备
                        }
                        //这下面的代码是把所有联系人的信息分类,这就涉及到了自己写的JS页面代码
                        LinkList = sortPY(username); //把联系人数组分类
                        //LinkList.show();
                        createLiCheckBox(LinkList); //分类信息显示至页面,我使用checkBox进行多个联系人选择
                    }

                }, function(e) {
                    alert("Find contact error: "   e.message);
                });

            }, function(e) {

    });
Copy after login

2. Import multiple selected personal information from the address book: To solve this problem, you must first create the address book page. The contact's ID is placed on the page (hidden using display), so that when I get the selected checkBox, I can directly get the ID and put these IDs into an array. Then filter out the contact information of these IDs through the application of plus.contacts.getAddressBook and address.find.

1. To solve the problem of using checkBox to get the contact id, here I used JQuery.

//筛选已经被选中的checkbox
    $("input:checked").each(function() {
            var index = $(this).parent().prev().children(&#39;label&#39;).text(); //获取id
            var name = $(this).parent().prev().children(&#39;p&#39;).text(); //获取姓名
            username.push(name);
            usernameIndex.push(index);
    });
Copy after login

2. To solve the problem, put these indexes into find to filter the information, and take out the contact information under the specific ID

plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) { //获取通讯录信息
            for(var j = 0; j < username.length; j  ) {//循环所选取的联系人,记得循环一定要放在这里,一开始我放在        plus.contacts.getAddressBook外面是错误
                        addressbook.find(null, function(contacts) {
                            console.log("进入查询");
                            for(var i = 0; i < contacts.length; i  ) {//无论是否为多个信息,一定要循环数组
                                console.log("进入循环");
                                //var id = contacts[i].id;
                                var displayname = contacts[i].displayName;
                                var phone = "";
                                var emails = "";
                                var dates = "";
                                var remark = "";
                                if(contacts[i].phoneNumbers.length > 0) {//这里需要判断是否为空,为空的数组没有index=0;
                                    phone = contacts[i].phoneNumbers[0].value;
                                } else {
                                    phone = contacts[i].phoneNumbers;
                                }

                                if(contacts[i].emails.length > 0) {//这里需要判断是否为空,为空的数组没有index=0;
                                    emails = contacts[i].emails[0].value;
                                } else {
                                    emails = contacts[i].emails;
                                }

                                var dateNum = new Date(contacts[i].birthday);//这里的birthday是number类型!!!官方手册坑爹?
                                dates = dateNum.getFullYear()   "."   (dateNum.getMonth()   1)   "."   dateNum.getDate();
                                remark = contacts[i].note;

                                var getContact = {//把所有信息放到一个json里面
                                    contactName: displayname,
                                    sex: "",
                                    department: "",
                                    positions: "",
                                    tel: "",
                                    phone: phone,
                                    eMail: emails,
                                    birthday: dates,
                                    hobby: "",
                                    remark: remark
                                };

                                //这下面是我的业务代码了,这里大家可以写自己的信息
                                //createContactTable(db);
                                //InsertContact(db, getContact); //多个信息插入有线程安全的问题出现!!!!!!!
                            }
                            //console.log(username.length);                         
                        }, function(e) {
                            console.log("查询错误");
                        }, {  
                                                         //这里面的筛选非常重要!!!这样才能选出匹配的信息
                            filter: [{
                                logic: "or",
                                field: "id",
                                value: usernameIndex[j]
                            }],
                            multi: false
                        });
                    }
                }, function(e) {
                    console.log("打开通讯录错误");
                });
Copy after login

Related articles:

PHP implements the online address book function (source code attached), the address book source code

js implements the index sliding display effect and sliding display anchor point effect of the address book

Detailed code example of using XML data island combined with Dom to create an address book

The above is the detailed content of Detailed explanation of HTML5 address book sample code for obtaining information of multiple specified people. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template