JQueryMobile 【電話薄】動態獲取數據並按姓名排序

代碼:php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css">
    <link rel="stylesheet" href="css/jquery.idcode.css">
    <link rel="stylesheet" href="css/animate.css">
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/jquery.mobile-1.4.5.min.js"></script>
    <script src="js/jquery.idcode.js"></script>
    <script src="js/wow.min.js"></script>
    <script>
        new WOW().init();
    </script>
</head>
<body>

<div data-role="page" id="pageone" data-theme="b">
    <div data-role="header" data-fullscreen="true">
        <h1>通信錄</h1>
        <a href="" class="ui-btn ui-icon-plus ui-btn-right ui-btn-icon-right ui-btn-icon-notext ui-corner-all ui-shadow "
                ></a>
    </div>
    <div data-role="main" class="ui-content">
        <h2>個人通信錄</h2>
        <form class="ui-filterable">
            <input id="myFilter" data-type="search" placeholder="搜索..">
        </form>
        <ul id="myList" data-role="listview" data-filter="true" data-input="#myFilter" data-autodividers="true"
            data-inset="true">
           <!-- <li><a href="#">Adele</a></li>-->

        </ul>
    </div>
</div>
<script>
    $(document).ready(function(){
        $.ajax({
            type:"post",
            url:'php/json.php',
            data:{},
            datatype:'json',
            success:function(data){
                var ul=$('#myList');
                var dic=JSON.parse(data);
                var array=[];
                for(var i=0;i<dic.length;i++){

                    array.push(dic[i].tname);//獲取數據並加入數組
                    array.sort(//排序
                            function compareFunction(param1,param2){
                                return param1.localeCompare(param2);
                            }
                    );
                }
               // console.log(array)
                for (var j = 0; j < array.length; j++) {
                    var li=$('<li></li>');
                    var a=$('<a></a>');
                    li.append(array[j]);
                    li.addClass('aaa');
                    ul.append(li);
                    $('#myList').listview('refresh');
                }
                $('.aaa').click(function(){
                    alert(1)
                })
            }
        });

    })
</script>
</body>
</html>
相關文章
相關標籤/搜索