jQuery Mobile - 列切換



描述

列切換將列隱藏,並允許使用者使用data-mode = "columntoggle"屬性根據自己的選擇選擇列。

示例

以下示例演示了在 jQuery Mobile 框架中使用列切換

<!DOCTYPE html>
<html>
   <head>
      <title>Table Column Toggle</title>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1">
      <link rel = "stylesheet" href = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
      <script src = "https://code.jquery.com/jquery-1.11.3.min.js"></script>
      <script src = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
   </head>

   <body>
      <div data-role = "header">
         <h2>Header</h2>
      </div>
      
      <table data-role = "table" id = "table-column-toggle" data-mode = "columntoggle" 
         class = "ui-responsive table-stripe" data-column-btn-theme = "b" 
         data-column-btn-text = "Click here to display columns...">
         
         <thead>
            <tr>
               <th data-priority = "1">Position</th>
               <th>Team</th>
               <th data-priority = "2">Rating</th>
               <th data-priority = "3">Points</th>
            </tr>
         </thead>
         
         <tbody>
            <tr>
               <th>1</th>
               <td>SOUTH AFRICA</td>
               <td>114</td>
               <td>3308</td>
            </tr>
            
            <tr>
               <th>2</th>
               <td>INDIA</td>
               <td>110</td>
               <td>3535</td>
            </tr>
            
            <tr>
               <th>3</th>
               <td>AUSTRALIA</td>
               <td>109</td>
               <td>4376</td>
            </tr>
            
            <tr>
               <th>4</th>
               <td>PAKISTAN</td>
               <td>106</td>
               <td>2977</td>
            </tr>
            
            <tr>
               <th>5</th>
               <td>NEW ZEALAND</td>
               <td>99</td>
               <td>3578</td>
            </tr>
            
            <tr>
               <th>6</th>
               <td>ENGLAND</td>
               <td>99</td>
               <td>3940</td>
            </tr>
            
            <tr>
               <th>7</th>
               <td>SRI LANKA</td>
               <td>89</td>
               <td>3123</td>
            </tr>
            
            <tr>
               <th>8</th>
               <td>WEST INDIES</td>
               <td>76</td>
               <td>2504</td>
            </tr>
            
            <tr>
               <th>9</th>
               <td>BANGLADESH</td>
               <td>47</td>
               <td>1026</td>
            </tr>
            
            <tr>
               <th>10</th>
               <td>ZIMBABWE</td>
               <td>5</td>
               <td>53</td>
            </tr>
         </tbody>
      </table>
      
      <div data-role = "footer">
         <h2>Footer</h2>
      </div>
      
   </body>
</html>

輸出

讓我們執行以下步驟來檢視上述程式碼是如何工作的:

  • 將上述 html 程式碼儲存為jqm_table_column_toggle.html檔案到伺服器根目錄。

  • 開啟此 HTML 檔案,地址為 https:///jqm_table_column_toggle.html,將顯示以下輸出。

列選擇器模式和設定列優先順序

您可以使用data-role = "table"data-mode = "columntoggle"屬性顯示錶格元素,如下面的標籤所示。

<table data-role = "table" id = "table-column-toggle" data-mode = "columntoggle">

您可以使用data-priority屬性指定哪些列應隱藏或顯示,併為優先順序值分配 1 到 6 的值。將具有 data-priority 屬性的表頭將出現在列選單中。

主題和自定義

預設情況下,按鈕上的文字是“Columns...”但是您可以使用data-column-btn-text屬性更改表格的文字。您還可以使用data-column-btn-theme屬性設定按鈕或列選擇器選單的主題。透過向表格元素新增table-stripe類,可以以條紋格式顯示行。

使表格響應式

您可以根據螢幕大小和方向使表格響應式。媒體查詢使用優先順序指定列的響應行為,並使用min-width屬性編寫媒體查詢。使用 em 單位設定的寬度將響應字型大小的變化,您可以透過將目標寬度除以 16 畫素來計算 em 單位中的畫素值。

應用預設斷點

您可以為每個優先順序級別應用自定義斷點,方法是向表格元素新增class = "ui-responsive" ,如下所示:

<table data-role = "table" class = "ui-responsive" id = "table-column-toggle" data-mod 
   e ="columntoggle">

您可以為每個優先順序級別使用以下預設樣式:

  • data-priority = "1" - 用於在 320px(20em)顯示列。

  • data-priority = "2" - 用於在 480px(30em)顯示列。

  • data-priority = "3" - 用於在 640px(40em)顯示列。

  • data-priority = "4" - 用於在 800px(50em)顯示列。

  • data-priority = "5" - 用於在 960px(60em)顯示列。

  • data-priority = "6" - 用於在 1120px(70em)顯示列。

分組列標題

列資料可以顯示在每個分組標題下,用於財務資料。您可以使用 priority 屬性解析指定的行,並將標題組顯示為選項。任何具有優先順序的表頭都將出現在列選擇器選單中。

jquery_mobile_widgets.htm
廣告
© . All rights reserved.