文章出處

一、DataTables 

個人覺得學習一門新的插件或者技術時候,官方文檔是最根本的,入門最快的地方,但是有時候看完官方文檔,一步步的動手寫例子,總會出現各種莫名其妙的錯誤,需要我們很好的進行研究出錯的地方。

官方網站(中文):http://datatables.club/

官方網站:https://www.datatables.net/

 二、簡單的例子

怎樣簡單地使用DataTables?使用下方簡單的幾行代碼,一個方法初始化table。

$(document).ready(function(){
        $('#myTable').DataTable();
    });

開始使用DataTables很簡單,只需要引入兩個文件, 一個css樣式文件和DataTables本身的腳本文件。在DataTables CDN上,可以使用下面這兩個文件:

js文件     //cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js

css文件      //cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css

在你的項目中使用 DataTables,只需要引入三個文件即可,jQuery庫,一個DT的核心js文件和一個DT的css文件, 完成以以下三步即可看到如下效果:

1、例子1:

(1)使用的是:Hbuilder

(2)項目結構:

 (3)index.html代碼

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>DataTables簡單用例</title>
        <!--樣式-->
        <link rel="stylesheet" type="text/css" href="DataTables-1.10.13/media/css/jquery.dataTables.css">
        <!-- jQuery -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.js"></script>
        <!-- DataTables -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.dataTables.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#example').DataTable();
            });
        </script>
    </head>

    <body>
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </tfoot>
        </table>

    </body>

</html>

(4)效果:

 

2、例子2:(PS:還是純粹的前端靜態頁面)

(1)結構和工具見例子1

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>DataTables簡單用例</title>
        <!--樣式-->
        <link rel="stylesheet" type="text/css" href="DataTables-1.10.13/media/css/jquery.dataTables.css">
        <!-- jQuery -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.js"></script>
        <!-- DataTables -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.dataTables.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#example').DataTable();
            });
        </script>
    </head>

    <body>
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>bob</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                </tr>
                <tr>
                    <th>JIM</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                </tr>
                <tr>
                    <th>Jack</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                </tr>
                <tr>
                    <th>qiao</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                    <th>123</th>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </tfoot>
        </table>

    </body>

</html>
View Code

(2)效果:

說明:search的輸入框是對所有屬性的全部搜索,并顯示搜索結果,最重要的是:這個搜索與表格進行了數據的雙向綁定,可以實時進行更新。

 

重要:DataTables已經讀取幾乎任何JSON數據源,可以通過ajax.datatables讀數據的能力已經從幾乎任何JSON數據源可以通過Ajax讀取數據的能力。

學習網站:https://datatables.net/examples/ajax/

 

3、例子3:(PS:數據源是數組arrays

(1)項目結構:(多了一個data的文件夾,文件夾里有一個arrays.txt文件,)

 

(2)arrays.txt內容

{
    "data": [
        [
            "Tiger Nixon",
            "System Architect",
            "Edinburgh",
            "5421",
            "2011\/04\/25",
            "$320,800"
        ],
        [
            "Garrett Winters",
            "Accountant",
            "Tokyo",
            "8422",
            "2011\/07\/25",
            "$170,750"
        ],
        [
            "Ashton Cox",
            "Junior Technical Author",
            "San Francisco",
            "1562",
            "2009\/01\/12",
            "$86,000"
        ],
        [
            "Cedric Kelly",
            "Senior Javascript Developer",
            "Edinburgh",
            "6224",
            "2012\/03\/29",
            "$433,060"
        ],
        [
            "Airi Satou",
            "Accountant",
            "Tokyo",
            "5407",
            "2008\/11\/28",
            "$162,700"
        ],
        [
            "Brielle Williamson",
            "Integration Specialist",
            "New York",
            "4804",
            "2012\/12\/02",
            "$372,000"
        ],
        [
            "Herrod Chandler",
            "Sales Assistant",
            "San Francisco",
            "9608",
            "2012\/08\/06",
            "$137,500"
        ],
        [
            "Rhona Davidson",
            "Integration Specialist",
            "Tokyo",
            "6200",
            "2010\/10\/14",
            "$327,900"
        ],
        [
            "Colleen Hurst",
            "Javascript Developer",
            "San Francisco",
            "2360",
            "2009\/09\/15",
            "$205,500"
        ],
        [
            "Sonya Frost",
            "Software Engineer",
            "Edinburgh",
            "1667",
            "2008\/12\/13",
            "$103,600"
        ],
        [
            "Jena Gaines",
            "Office Manager",
            "London",
            "3814",
            "2008\/12\/19",
            "$90,560"
        ],
        [
            "Quinn Flynn",
            "Support Lead",
            "Edinburgh",
            "9497",
            "2013\/03\/03",
            "$342,000"
        ],
        [
            "Charde Marshall",
            "Regional Director",
            "San Francisco",
            "6741",
            "2008\/10\/16",
            "$470,600"
        ],
        [
            "Haley Kennedy",
            "Senior Marketing Designer",
            "London",
            "3597",
            "2012\/12\/18",
            "$313,500"
        ],
        [
            "Tatyana Fitzpatrick",
            "Regional Director",
            "London",
            "1965",
            "2010\/03\/17",
            "$385,750"
        ],
        [
            "Michael Silva",
            "Marketing Designer",
            "London",
            "1581",
            "2012\/11\/27",
            "$198,500"
        ],
        [
            "Paul Byrd",
            "Chief Financial Officer (CFO)",
            "New York",
            "3059",
            "2010\/06\/09",
            "$725,000"
        ],
        [
            "Gloria Little",
            "Systems Administrator",
            "New York",
            "1721",
            "2009\/04\/10",
            "$237,500"
        ],
        [
            "Bradley Greer",
            "Software Engineer",
            "London",
            "2558",
            "2012\/10\/13",
            "$132,000"
        ],
        [
            "Dai Rios",
            "Personnel Lead",
            "Edinburgh",
            "2290",
            "2012\/09\/26",
            "$217,500"
        ],
        [
            "Jenette Caldwell",
            "Development Lead",
            "New York",
            "1937",
            "2011\/09\/03",
            "$345,000"
        ],
        [
            "Yuri Berry",
            "Chief Marketing Officer (CMO)",
            "New York",
            "6154",
            "2009\/06\/25",
            "$675,000"
        ],
        [
            "Caesar Vance",
            "Pre-Sales Support",
            "New York",
            "8330",
            "2011\/12\/12",
            "$106,450"
        ],
        [
            "Doris Wilder",
            "Sales Assistant",
            "Sidney",
            "3023",
            "2010\/09\/20",
            "$85,600"
        ],
        [
            "Angelica Ramos",
            "Chief Executive Officer (CEO)",
            "London",
            "5797",
            "2009\/10\/09",
            "$1,200,000"
        ],
        [
            "Gavin Joyce",
            "Developer",
            "Edinburgh",
            "8822",
            "2010\/12\/22",
            "$92,575"
        ],
        [
            "Jennifer Chang",
            "Regional Director",
            "Singapore",
            "9239",
            "2010\/11\/14",
            "$357,650"
        ],
        [
            "Brenden Wagner",
            "Software Engineer",
            "San Francisco",
            "1314",
            "2011\/06\/07",
            "$206,850"
        ],
        [
            "Fiona Green",
            "Chief Operating Officer (COO)",
            "San Francisco",
            "2947",
            "2010\/03\/11",
            "$850,000"
        ],
        [
            "Shou Itou",
            "Regional Marketing",
            "Tokyo",
            "8899",
            "2011\/08\/14",
            "$163,000"
        ],
        [
            "Michelle House",
            "Integration Specialist",
            "Sidney",
            "2769",
            "2011\/06\/02",
            "$95,400"
        ],
        [
            "Suki Burks",
            "Developer",
            "London",
            "6832",
            "2009\/10\/22",
            "$114,500"
        ],
        [
            "Prescott Bartlett",
            "Technical Author",
            "London",
            "3606",
            "2011\/05\/07",
            "$145,000"
        ],
        [
            "Gavin Cortez",
            "Team Leader",
            "San Francisco",
            "2860",
            "2008\/10\/26",
            "$235,500"
        ],
        [
            "Martena Mccray",
            "Post-Sales support",
            "Edinburgh",
            "8240",
            "2011\/03\/09",
            "$324,050"
        ],
        [
            "Unity Butler",
            "Marketing Designer",
            "San Francisco",
            "5384",
            "2009\/12\/09",
            "$85,675"
        ],
        [
            "Howard Hatfield",
            "Office Manager",
            "San Francisco",
            "7031",
            "2008\/12\/16",
            "$164,500"
        ],
        [
            "Hope Fuentes",
            "Secretary",
            "San Francisco",
            "6318",
            "2010\/02\/12",
            "$109,850"
        ],
        [
            "Vivian Harrell",
            "Financial Controller",
            "San Francisco",
            "9422",
            "2009\/02\/14",
            "$452,500"
        ],
        [
            "Timothy Mooney",
            "Office Manager",
            "London",
            "7580",
            "2008\/12\/11",
            "$136,200"
        ],
        [
            "Jackson Bradshaw",
            "Director",
            "New York",
            "1042",
            "2008\/09\/26",
            "$645,750"
        ],
        [
            "Olivia Liang",
            "Support Engineer",
            "Singapore",
            "2120",
            "2011\/02\/03",
            "$234,500"
        ],
        [
            "Bruno Nash",
            "Software Engineer",
            "London",
            "6222",
            "2011\/05\/03",
            "$163,500"
        ],
        [
            "Sakura Yamamoto",
            "Support Engineer",
            "Tokyo",
            "9383",
            "2009\/08\/19",
            "$139,575"
        ],
        [
            "Thor Walton",
            "Developer",
            "New York",
            "8327",
            "2013\/08\/11",
            "$98,540"
        ],
        [
            "Finn Camacho",
            "Support Engineer",
            "San Francisco",
            "2927",
            "2009\/07\/07",
            "$87,500"
        ],
        [
            "Serge Baldwin",
            "Data Coordinator",
            "Singapore",
            "8352",
            "2012\/04\/09",
            "$138,575"
        ],
        [
            "Zenaida Frank",
            "Software Engineer",
            "New York",
            "7439",
            "2010\/01\/04",
            "$125,250"
        ],
        [
            "Zorita Serrano",
            "Software Engineer",
            "San Francisco",
            "4389",
            "2012\/06\/01",
            "$115,000"
        ],
        [
            "Jennifer Acosta",
            "Junior Javascript Developer",
            "Edinburgh",
            "3431",
            "2013\/02\/01",
            "$75,650"
        ],
        [
            "Cara Stevens",
            "Sales Assistant",
            "New York",
            "3990",
            "2011\/12\/06",
            "$145,600"
        ],
        [
            "Hermione Butler",
            "Regional Director",
            "London",
            "1016",
            "2011\/03\/21",
            "$356,250"
        ],
        [
            "Lael Greer",
            "Systems Administrator",
            "London",
            "6733",
            "2009\/02\/27",
            "$103,500"
        ],
        [
            "Jonas Alexander",
            "Developer",
            "San Francisco",
            "8196",
            "2010\/07\/14",
            "$86,500"
        ],
        [
            "Shad Decker",
            "Regional Director",
            "Edinburgh",
            "6373",
            "2008\/11\/13",
            "$183,000"
        ],
        [
            "Michael Bruce",
            "Javascript Developer",
            "Singapore",
            "5384",
            "2011\/06\/27",
            "$183,000"
        ],
        [
            "Donna Snider",
            "Customer Support",
            "New York",
            "4226",
            "2011\/01\/25",
            "$112,000"
        ]
    ]
}
View Code

(3)index_02.html代碼

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>DataTables簡單用例</title>
        <!--樣式-->
        <link rel="stylesheet" type="text/css" href="DataTables-1.10.13/media/css/jquery.dataTables.css">
        <!-- jQuery -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.js"></script>
        <!-- DataTables -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.dataTables.js"></script>
        <script type="text/javascript" language="javascript" class="init">
            $(document).ready(function() {
                $('#example').DataTable({
                    "ajax": "data/arrays.txt"
                });
            });
        </script>
    </head>

    <body>
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </tfoot>
        </table>

    </body>

</html>
View Code

(4)效果圖:

 

4、例子4:(PS:使用ajax,數據源是對象objects)

通過使用 columns.dataDT 選項用于告訴Datatables找到每一列的數據源對象中對應的屬性。

 //data:name指的是引用data中name的屬性。columns中每一個data順序就是表格內容的真正順序

(1)項目結構(多了一個data的文件夾,文件夾里有一個objects.txt文件,)

(2)objects.txt文件內容

{
    "data": [
        {
            "name": "Tiger Nixon",
            "position": "System Architect",
            "salary": "$320,800",
            "start_date": "2011\/04\/25",
            "office": "Edinburgh",
            "extn": "5421"
        },
        {
            "name": "Garrett Winters",
            "position": "Accountant",
            "salary": "$170,750",
            "start_date": "2011\/07\/25",
            "office": "Tokyo",
            "extn": "8422"
        },
        {
            "name": "Ashton Cox",
            "position": "Junior Technical Author",
            "salary": "$86,000",
            "start_date": "2009\/01\/12",
            "office": "San Francisco",
            "extn": "1562"
        },
        {
            "name": "Cedric Kelly",
            "position": "Senior Javascript Developer",
            "salary": "$433,060",
            "start_date": "2012\/03\/29",
            "office": "Edinburgh",
            "extn": "6224"
        },
        {
            "name": "Airi Satou",
            "position": "Accountant",
            "salary": "$162,700",
            "start_date": "2008\/11\/28",
            "office": "Tokyo",
            "extn": "5407"
        },
        {
            "name": "Brielle Williamson",
            "position": "Integration Specialist",
            "salary": "$372,000",
            "start_date": "2012\/12\/02",
            "office": "New York",
            "extn": "4804"
        },
        {
            "name": "Herrod Chandler",
            "position": "Sales Assistant",
            "salary": "$137,500",
            "start_date": "2012\/08\/06",
            "office": "San Francisco",
            "extn": "9608"
        },
        {
            "name": "Rhona Davidson",
            "position": "Integration Specialist",
            "salary": "$327,900",
            "start_date": "2010\/10\/14",
            "office": "Tokyo",
            "extn": "6200"
        },
        {
            "name": "Colleen Hurst",
            "position": "Javascript Developer",
            "salary": "$205,500",
            "start_date": "2009\/09\/15",
            "office": "San Francisco",
            "extn": "2360"
        },
        {
            "name": "Sonya Frost",
            "position": "Software Engineer",
            "salary": "$103,600",
            "start_date": "2008\/12\/13",
            "office": "Edinburgh",
            "extn": "1667"
        },
        {
            "name": "Jena Gaines",
            "position": "Office Manager",
            "salary": "$90,560",
            "start_date": "2008\/12\/19",
            "office": "London",
            "extn": "3814"
        },
        {
            "name": "Quinn Flynn",
            "position": "Support Lead",
            "salary": "$342,000",
            "start_date": "2013\/03\/03",
            "office": "Edinburgh",
            "extn": "9497"
        },
        {
            "name": "Charde Marshall",
            "position": "Regional Director",
            "salary": "$470,600",
            "start_date": "2008\/10\/16",
            "office": "San Francisco",
            "extn": "6741"
        },
        {
            "name": "Haley Kennedy",
            "position": "Senior Marketing Designer",
            "salary": "$313,500",
            "start_date": "2012\/12\/18",
            "office": "London",
            "extn": "3597"
        },
        {
            "name": "Tatyana Fitzpatrick",
            "position": "Regional Director",
            "salary": "$385,750",
            "start_date": "2010\/03\/17",
            "office": "London",
            "extn": "1965"
        },
        {
            "name": "Michael Silva",
            "position": "Marketing Designer",
            "salary": "$198,500",
            "start_date": "2012\/11\/27",
            "office": "London",
            "extn": "1581"
        },
        {
            "name": "Paul Byrd",
            "position": "Chief Financial Officer (CFO)",
            "salary": "$725,000",
            "start_date": "2010\/06\/09",
            "office": "New York",
            "extn": "3059"
        },
        {
            "name": "Gloria Little",
            "position": "Systems Administrator",
            "salary": "$237,500",
            "start_date": "2009\/04\/10",
            "office": "New York",
            "extn": "1721"
        },
        {
            "name": "Bradley Greer",
            "position": "Software Engineer",
            "salary": "$132,000",
            "start_date": "2012\/10\/13",
            "office": "London",
            "extn": "2558"
        },
        {
            "name": "Dai Rios",
            "position": "Personnel Lead",
            "salary": "$217,500",
            "start_date": "2012\/09\/26",
            "office": "Edinburgh",
            "extn": "2290"
        },
        {
            "name": "Jenette Caldwell",
            "position": "Development Lead",
            "salary": "$345,000",
            "start_date": "2011\/09\/03",
            "office": "New York",
            "extn": "1937"
        },
        {
            "name": "Yuri Berry",
            "position": "Chief Marketing Officer (CMO)",
            "salary": "$675,000",
            "start_date": "2009\/06\/25",
            "office": "New York",
            "extn": "6154"
        },
        {
            "name": "Caesar Vance",
            "position": "Pre-Sales Support",
            "salary": "$106,450",
            "start_date": "2011\/12\/12",
            "office": "New York",
            "extn": "8330"
        },
        {
            "name": "Doris Wilder",
            "position": "Sales Assistant",
            "salary": "$85,600",
            "start_date": "2010\/09\/20",
            "office": "Sidney",
            "extn": "3023"
        },
        {
            "name": "Angelica Ramos",
            "position": "Chief Executive Officer (CEO)",
            "salary": "$1,200,000",
            "start_date": "2009\/10\/09",
            "office": "London",
            "extn": "5797"
        },
        {
            "name": "Gavin Joyce",
            "position": "Developer",
            "salary": "$92,575",
            "start_date": "2010\/12\/22",
            "office": "Edinburgh",
            "extn": "8822"
        },
        {
            "name": "Jennifer Chang",
            "position": "Regional Director",
            "salary": "$357,650",
            "start_date": "2010\/11\/14",
            "office": "Singapore",
            "extn": "9239"
        },
        {
            "name": "Brenden Wagner",
            "position": "Software Engineer",
            "salary": "$206,850",
            "start_date": "2011\/06\/07",
            "office": "San Francisco",
            "extn": "1314"
        },
        {
            "name": "Fiona Green",
            "position": "Chief Operating Officer (COO)",
            "salary": "$850,000",
            "start_date": "2010\/03\/11",
            "office": "San Francisco",
            "extn": "2947"
        },
        {
            "name": "Shou Itou",
            "position": "Regional Marketing",
            "salary": "$163,000",
            "start_date": "2011\/08\/14",
            "office": "Tokyo",
            "extn": "8899"
        },
        {
            "name": "Michelle House",
            "position": "Integration Specialist",
            "salary": "$95,400",
            "start_date": "2011\/06\/02",
            "office": "Sidney",
            "extn": "2769"
        },
        {
            "name": "Suki Burks",
            "position": "Developer",
            "salary": "$114,500",
            "start_date": "2009\/10\/22",
            "office": "London",
            "extn": "6832"
        },
        {
            "name": "Prescott Bartlett",
            "position": "Technical Author",
            "salary": "$145,000",
            "start_date": "2011\/05\/07",
            "office": "London",
            "extn": "3606"
        },
        {
            "name": "Gavin Cortez",
            "position": "Team Leader",
            "salary": "$235,500",
            "start_date": "2008\/10\/26",
            "office": "San Francisco",
            "extn": "2860"
        },
        {
            "name": "Martena Mccray",
            "position": "Post-Sales support",
            "salary": "$324,050",
            "start_date": "2011\/03\/09",
            "office": "Edinburgh",
            "extn": "8240"
        },
        {
            "name": "Unity Butler",
            "position": "Marketing Designer",
            "salary": "$85,675",
            "start_date": "2009\/12\/09",
            "office": "San Francisco",
            "extn": "5384"
        },
        {
            "name": "Howard Hatfield",
            "position": "Office Manager",
            "salary": "$164,500",
            "start_date": "2008\/12\/16",
            "office": "San Francisco",
            "extn": "7031"
        },
        {
            "name": "Hope Fuentes",
            "position": "Secretary",
            "salary": "$109,850",
            "start_date": "2010\/02\/12",
            "office": "San Francisco",
            "extn": "6318"
        },
        {
            "name": "Vivian Harrell",
            "position": "Financial Controller",
            "salary": "$452,500",
            "start_date": "2009\/02\/14",
            "office": "San Francisco",
            "extn": "9422"
        },
        {
            "name": "Timothy Mooney",
            "position": "Office Manager",
            "salary": "$136,200",
            "start_date": "2008\/12\/11",
            "office": "London",
            "extn": "7580"
        },
        {
            "name": "Jackson Bradshaw",
            "position": "Director",
            "salary": "$645,750",
            "start_date": "2008\/09\/26",
            "office": "New York",
            "extn": "1042"
        },
        {
            "name": "Olivia Liang",
            "position": "Support Engineer",
            "salary": "$234,500",
            "start_date": "2011\/02\/03",
            "office": "Singapore",
            "extn": "2120"
        },
        {
            "name": "Bruno Nash",
            "position": "Software Engineer",
            "salary": "$163,500",
            "start_date": "2011\/05\/03",
            "office": "London",
            "extn": "6222"
        },
        {
            "name": "Sakura Yamamoto",
            "position": "Support Engineer",
            "salary": "$139,575",
            "start_date": "2009\/08\/19",
            "office": "Tokyo",
            "extn": "9383"
        },
        {
            "name": "Thor Walton",
            "position": "Developer",
            "salary": "$98,540",
            "start_date": "2013\/08\/11",
            "office": "New York",
            "extn": "8327"
        },
        {
            "name": "Finn Camacho",
            "position": "Support Engineer",
            "salary": "$87,500",
            "start_date": "2009\/07\/07",
            "office": "San Francisco",
            "extn": "2927"
        },
        {
            "name": "Serge Baldwin",
            "position": "Data Coordinator",
            "salary": "$138,575",
            "start_date": "2012\/04\/09",
            "office": "Singapore",
            "extn": "8352"
        },
        {
            "name": "Zenaida Frank",
            "position": "Software Engineer",
            "salary": "$125,250",
            "start_date": "2010\/01\/04",
            "office": "New York",
            "extn": "7439"
        },
        {
            "name": "Zorita Serrano",
            "position": "Software Engineer",
            "salary": "$115,000",
            "start_date": "2012\/06\/01",
            "office": "San Francisco",
            "extn": "4389"
        },
        {
            "name": "Jennifer Acosta",
            "position": "Junior Javascript Developer",
            "salary": "$75,650",
            "start_date": "2013\/02\/01",
            "office": "Edinburgh",
            "extn": "3431"
        },
        {
            "name": "Cara Stevens",
            "position": "Sales Assistant",
            "salary": "$145,600",
            "start_date": "2011\/12\/06",
            "office": "New York",
            "extn": "3990"
        },
        {
            "name": "Hermione Butler",
            "position": "Regional Director",
            "salary": "$356,250",
            "start_date": "2011\/03\/21",
            "office": "London",
            "extn": "1016"
        },
        {
            "name": "Lael Greer",
            "position": "Systems Administrator",
            "salary": "$103,500",
            "start_date": "2009\/02\/27",
            "office": "London",
            "extn": "6733"
        },
        {
            "name": "Jonas Alexander",
            "position": "Developer",
            "salary": "$86,500",
            "start_date": "2010\/07\/14",
            "office": "San Francisco",
            "extn": "8196"
        },
        {
            "name": "Shad Decker",
            "position": "Regional Director",
            "salary": "$183,000",
            "start_date": "2008\/11\/13",
            "office": "Edinburgh",
            "extn": "6373"
        },
        {
            "name": "Michael Bruce",
            "position": "Javascript Developer",
            "salary": "$183,000",
            "start_date": "2011\/06\/27",
            "office": "Singapore",
            "extn": "5384"
        },
        {
            "name": "Donna Snider",
            "position": "Customer Support",
            "salary": "$112,000",
            "start_date": "2011\/01\/25",
            "office": "New York",
            "extn": "4226"
        }
    ]
}
View Code

對比數據源是數組(左邊)和對象(右邊)格式的不同

(3)index_02.html內容

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>DataTables簡單用例</title>
        <!--樣式-->
        <link rel="stylesheet" type="text/css" href="DataTables-1.10.13/media/css/jquery.dataTables.css">
        <!-- jQuery -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.js"></script>
        <!-- DataTables -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.dataTables.js"></script>
        <script type="text/javascript" language="javascript" class="init">
            $(document).ready(function() {
                $('#example').DataTable({
                    "ajax": "data/objects.txt", 
                    //data:name指的是引用data中name的屬性。columns中每一個data順序就是表格內容的真正順序
                    "columns": [{
                        "data": "name"
                    }, {
                        "data": "position"
                    }, {
                        "data": "office"
                    }, {
                        "data": "extn"
                    }, {
                        "data": "start_date"
                    }, {
                        "data": "salary"
                    }]
                });
            });
        </script>
    </head>

    <body>
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </tfoot>
        </table>

    </body>

</html>
View Code

(4)效果

 

5、例子5:(PS:使用ajax,不指定數據源屬性)

 當從 Ajax 源加載數據,在默認情況下,數據表將尋找要使用返回的對象的數據參數中的數據。

dataSrc有許多用法如下:

  • 作為一個字符串,如 dataSrc: 'myData'
  • 為空字符串,如 dataSrc:"",下面的例子展示的是這個用法
  • 作為一個方法,如 dataSrc: function(json) {} (您可以從 XML 轉換到一個 Javascript 對象)
$(document).ready(function() {
      $('#example').dataTable( {
          "ajax": {
              "url": "data/objects_root_array.txt",
              //默認為data,這里定義為空,則只需要傳不帶屬性的數據
              "dataSrc": ""
          },
          "columns": [
              { "data": "name" },
              { "data": "position" },
              { "data": "office" },
              { "data": "extn" },
              { "data": "start_date" },
              { "data": "salary" }
          ]
      } );
  } );

 效果:如上圖例子所示

 

 6、例子6:(PS:動態顯示和隱藏列)

這個例子演示了 column().visible()方法來隱藏顯示列,通過點擊列按鈕動態切換。

(1)項目結構(PS:新增加的index_03.css)

(2)index_03.css文件

/*表格字體*/
body {
    font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Arial, Helvetica, sans-serif;
    margin: 0;
    padding: 0;
    color: #333;
    background-color: #fff;
}

/*column的字體顏色和小手*/
a {
    cursor: pointer;
    color: #3174c7;
    text-decoration: none;
}

(3)objects.txt文件內容參考上的例子內容

(4)index_03.html文件

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>DataTables簡單用例</title>
        <!--自己樣式-->
        <link rel="stylesheet" type="text/css" href="css/index_03.css"/>
        <!--樣式-->
        <link rel="stylesheet" type="text/css" href="DataTables-1.10.13/media/css/jquery.dataTables.css">
        <!-- jQuery -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.js"></script>
        <!-- DataTables -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.dataTables.js"></script>
        <script type="text/javascript" language="javascript" class="init">
            $(document).ready(function() {
                var table = $("#example").DataTable({
                    "ajax": "data/objects.txt",
                    "columns": [{
                        "data": "name"
                    }, {
                        "data": "position"
                    }, {
                        "data": "office"
                    }, {
                        "data": "extn"
                    }, {
                        "data": "start_date"
                    }, {
                        "data": "salary"
                    }],
                    //展示高度
                    "scrollY": "300px",
                    //顯示多少行去掉
                    "paging": false
                });
                //顯示和隱藏
                $('a.toggle-vis').on('click', function(e) {
                    e.preventDefault();
                    // Get the column API object
                    var column = table.column($(this).attr('data-column'));
                    // Toggle the visibility
                    column.visible(!column.visible());
                });
            });
        </script>
    </head>

    <body>
        <!--索引-->
        <div class="demo-html">
            <div>
                Toggle column:
                <a class="toggle-vis" data-column="0">Name</a> -
                <a class="toggle-vis" data-column="1">Position</a> -
                <a class="toggle-vis" data-column="2">Office</a> -
                <a class="toggle-vis" data-column="3">Age</a> -
                <a class="toggle-vis" data-column="4">Start date</a> -
                <a class="toggle-vis" data-column="5">Salary</a>
            </div>
        </div>
        <!--正式table-->
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </tfoot>
        </table>

    </body>

</html>
View Code

(5)效果所示

  7、例子7:(PS:在回調方法中使用api)

 有時候希望給表格綁定的數據行綁定一些事件,這個時候你需要用到 initCompleteDT 、rowCallback 這些回調方法,下面的例子演示了,當表格加載完后給表格的td綁定點擊事件,取到值后進行搜索

(1)項目結構

(2)index_03.css文件參照上一個例子

(3)objects.txt文件參照上面的例子

(4)index_04.html文件

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>DataTables簡單用例</title>
        <!--自己樣式-->
        <link rel="stylesheet" type="text/css" href="css/index_03.css"/>
        <!--樣式-->
        <link rel="stylesheet" type="text/css" href="DataTables-1.10.13/media/css/jquery.dataTables.css">
        <!-- jQuery -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.js"></script>
        <!-- DataTables -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.dataTables.js"></script>
        <script type="text/javascript" language="javascript" class="init">
            $(document).ready(function() {
                var table = $("#example").DataTable({
                    "ajax": "data/objects.txt",
                    "columns": [{
                        "data": "name"
                    }, {
                        "data": "position"
                    }, {
                        "data": "office"
                    }, {
                        "data": "extn"
                    }, {
                        "data": "start_date"
                    }, {
                        "data": "salary"
                    }],
                    //展示高度
//                    "scrollY": "300px",
                    //顯示多少行去掉
//                    "paging": false
                    //右下角的分頁
                    "initComplete":function(){
                        var api=this.api();
                        api.$("td").click(function(){
                            api.search(this.innerHTML).draw();
                        })
                    }
                });
                //顯示和隱藏哪一列
                $('a.toggle-vis').on('click', function(e) {
                    e.preventDefault();
                    // Get the column API object
                    var column = table.column($(this).attr('data-column'));
                    // Toggle the visibility
                    column.visible(!column.visible());
                });
            });
        </script>
    </head>

    <body>
        <!--索引-->
        <div class="demo-html">
            <div>
                Toggle column:
                <a class="toggle-vis" data-column="0">Name</a> -
                <a class="toggle-vis" data-column="1">Position</a> -
                <a class="toggle-vis" data-column="2">Office</a> -
                <a class="toggle-vis" data-column="3">Age</a> -
                <a class="toggle-vis" data-column="4">Start date</a> -
                <a class="toggle-vis" data-column="5">Salary</a>
            </div>
        </div>
        <!--正式table-->
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Extn.</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </tfoot>
        </table>

    </body>

</html>
View Code

(5)效果如圖

 三、稍微復雜的例子

8、例子8:(PS:顯示行的附加信息)

使用Ajax調用服務器來獲得行的附加信息。

 (1)項目結構

(2)index_03.css

/*表格字體*/

body {
    font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Arial, Helvetica, sans-serif;
    margin: 0;
    padding: 0;
    color: #333;
    background-color: #fff;
}


/*column的字體顏色和小手*/

a {
    cursor: pointer;
    color: #3174c7;
    text-decoration: none;
}


/*顯示顯示和影藏按鈕*/

td.details-control {
    background: url(../img/details_open.png) no-repeat center center;
    cursor: pointer;
}

tr.shown td.details-control {
    background: url(../img/details_close.png) no-repeat center center;
}

(3)objects.js文件和objects.txt文件內容一樣,你使用js和txt是一樣的,只要里面存的數據是對的就行

(4)添加img兩個圖片

(5)index_05.htm

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>DataTables簡單用例</title>
        <!--自己樣式-->
        <link rel="stylesheet" type="text/css" href="css/index_03.css" />
        <!--樣式-->
        <link rel="stylesheet" type="text/css" href="DataTables-1.10.13/media/css/jquery.dataTables.css">
        <!-- jQuery -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.js"></script>
        <!-- DataTables -->
        <script type="text/javascript" charset="utf8" src="DataTables-1.10.13/media/js/jquery.dataTables.js"></script>
        <script type="text/javascript" language="javascript" class="init">
            //格式函數
            function format(d) {
                // `d` is the original data object for the row
                return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
                    '<tr>' +
                    '<td>Full name:</td>' +
                    '<td>' + d.name + '</td>' +
                    '</tr>' +
                    '<tr>' +
                    '<td>Extension number:</td>' +
                    '<td>' + d.extn + '</td>' +
                    '</tr>' +
                    '<tr>' +
                    '<td>Extra info:</td>' +
                    '<td>And any further details here (images etc)...</td>' +
                    '</tr>' +
                    '</table>';
            }
            //ready
            $(document).ready(function() {
                var table = $("#example").DataTable({
                    "ajax": "/DataTables_simple/data/objects.js",
                    "columns": [{
                        "className": 'details-control',
                        "orderable": false,
                        "data": null,
                        "defaultContent": ''
                    }, {
                        "data": "name"
                    }, {
                        "data": "position"
                    }, {
                        "data": "office"
                    }, {
                        "data": "salary"
                    }],
                    //默認顯示是升序
                    "order": [
                        [1, 'asc']
                    ],

                    //展示高度
                    //    "scrollY": "300px",
                    //顯示多少行去掉
                    //    "paging": false

                    //右下角的分頁
                        "initComplete": function() {
                        var api = this.api();
                        api.$("td").click(function() {
                            api.search(this.innerHTML).draw();
                        })
                    }
                });
                //顯示和隱藏哪一列
                $('a.toggle-vis').on('click', function(e) {
                    e.preventDefault();
                    // Get the column API object
                    var column = table.column($(this).attr('data-column'));
                    // Toggle the visibility
                    column.visible(!column.visible());
                });

                //按鈕顯示和隱藏  Add event listener for opening and closing details
                $('#example tbody').on('click', 'td.details-control', function() {
                    var tr = $(this).closest('tr');
                    var row = table.row(tr);
                    if(row.child.isShown()) {
                        // This row is already open - close it
                        row.child.hide();
                        tr.removeClass('shown');
                    } else {
                        // Open this row
                        row.child(format(row.data())).show();
                        tr.addClass('shown');
                    }
                });
            });
        </script>
    </head>

    <body>
        <!--索引-->
        <div class="demo-html">
            <div>
                Toggle column:
                <a class="toggle-vis" data-column="1">Name</a> -
                <a class="toggle-vis" data-column="2">Position</a> -
                <a class="toggle-vis" data-column="3">Office</a> -
                <a class="toggle-vis" data-column="4">Salary</a>
            </div>
        </div>
        <!--正式table-->
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th></th>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th></th>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Salary</th>
                </tr>
            </tfoot>
        </table>

    </body>

</html>
View Code

(6)效果如圖

 

三、報錯問題的解決

 

會出現這種錯誤,是對數據進行解析出錯,需要你檢查地方如下所示

1、數據的路徑容易錯,很容易錯,

2、數據解析的方式容易錯,與txt或者js里存的數據格式要對應上

 


文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()