蓝色天空.知识 · 实践 · 创造
我的收藏
jQuery Mobile 教程目录 32章
jQuery Mobile / LEARN BY DOING

jQuery Mobile 表格

系统拆解 jQuery Mobile 响应式表格的两种模式——回流模式(reflow)与列切换模式(columntoggle)、thead/tbody 必需结构与禁用 rowspan/colspan 约束、data-priority 1–6 优先级隐显规则、data-column-btn-text 以及 ui-shadow 与斑马纹样式定制。

学完这一章,你能够

  • 系统拆解 jQuery Mobile 响应式表格的两种模式——回流模式(reflow)与列切换模式(columntoggle)、thead/tbody 必需结构与禁用 rowspan/colspan 约束、data-priority 1–6 优先级隐显规则、data-column-btn-text 以及 ui-shadow 与斑马纹样式定制。

响应式表格与回流模式(reflow)

宽表格在手机窄屏上极易引发横向溢出。jQuery Mobile 提供“回流(reflow)”与“列切换(columntoggle)”两种响应式表格模型。默认采用回流模式:在 `<table>` 上添加 `data-role="table"` 与 `class="ui-responsive"` 后,大屏保持常规横向表格,窄屏则自动将每条记录折排为“表头标签 + 单元格值”的垂直键值对列表。使用响应式表格有两个硬性结构前提:必须显式划分 `<thead>` 与 `<tbody>`,且不得使用 `rowspan` 或 `colspan` 合并单元格。

列切换模式(columntoggle)与优先级规则

当需要保持表格横向结构、仅在空间不足时收起次要列,可将 `data-mode` 设为 `columntoggle` 并指定 `id`。框架会在表格上方生成“Columns...”勾选弹窗按钮,并允许通过表头 `<th>` 的 `data-priority`(`1` 为最高优先级、`6` 为最低优先级)控制窄屏下的隐藏次序:数字越大(优先级越低)越先被隐藏,而未设置 `data-priority` 的列(如示例中的 `CustomerName`)属于持久关键列,不会被自动隐藏。此外,可通过 `data-column-btn-text` 自定义切换按钮文案(例如改为中文提示)。

表格阴影、边框与斑马纹样式

为 `<table>` 追加 `ui-shadow` 类即可添加外阴影;进一步精细排版可直接叠加标准 CSS,例如用 `tr { border-bottom: 1px solid #d6d6d6; }` 为数据行添加分隔底线,或用 `th { border-bottom: 1px solid #d6d6d6; }` 搭配 `tr:nth-child(even) { background: #e9e9e9; }` 实现表头下边框与偶数行斑马纹底色(原页面小标题中将“底边框”笔误写为“按钮”,实际源码为 `th { border-bottom: ... }`)。在现代项目中(jQuery Mobile 已于 2021 年停止维护),通常借助横向滚动容器、CSS Container Queries 或响应式数据表组件实现移动端表格适配。

响应式表格 HTML 结构约束

主题必须遵守的结构规则
thead/tbody 与禁用合并单元格响应式表格必须包含 <thead> 与 <tbody>,且不支持 rowspan 与 colspan,否则回流标签复制或列显隐索引将错位。

列切换优先级省略规则

主题持久列行为
未设 data-priority 的列省略 data-priority 属性的表头列在任何断点下都保持常驻显示,不会被自动隐藏,适合放置主键名称等核心标识字段。

运行回流表格约束、列切换 1–6 优先级排序与表格 CSS 规则离线核验

含完整源码 · 142 行
展开查看收起
python
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 13: tables."""
import json
import re
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline jQuery Mobile DOM & data-role verifier"


def run_cases():
    cases = []

    # Case 1: Reflow table structural prerequisites (thead + tbody, no rowspan/colspan)
    doc1 = html.fragment_fromstring(
        """
        <table data-role="table" class="ui-responsive" id="reflowTable">
          <thead>
            <tr><th>CustomerID</th><th>CustomerName</th><th>City</th></tr>
          </thead>
          <tbody>
            <tr><td>1</td><td>Alfreds Futterkiste</td><td>Berlin</td></tr>
          </tbody>
        </table>
        """
    )
    actual_1 = {
        "role": doc1.get("data-role"),
        "default_mode": doc1.get("data-mode", "reflow"),
        "has_responsive_class": "ui-responsive" in doc1.get("class", "").split(),
        "has_thead_and_tbody": bool(doc1.xpath("./thead")) and bool(doc1.xpath("./tbody")),
        "has_span_attrs": bool(doc1.xpath(".//*[@rowspan or @colspan]")),
    }
    expected_1 = {
        "role": "table",
        "default_mode": "reflow",
        "has_responsive_class": True,
        "has_thead_and_tbody": True,
        "has_span_attrs": False,
    }
    cases.append({
        "name": "reflow_table_structure",
        "summary": "Verify reflow table data-role='table', ui-responsive class, thead/tbody requirement, and absence of rowspan/colspan.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Columntoggle table with data-priority (1..6 and persistent column without priority)
    doc2 = html.fragment_fromstring(
        """
        <table data-role="table" data-mode="columntoggle" class="ui-responsive"
               data-column-btn-text="点我显示或隐藏列!" id="myTable">
          <thead>
            <tr>
              <th data-priority="6">CustomerID</th>
              <th>CustomerName</th>
              <th data-priority="1">ContactName</th>
              <th data-priority="2">Address</th>
              <th data-priority="3">City</th>
              <th data-priority="4">PostalCode</th>
              <th data-priority="5">Country</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td><td>Alfreds Futterkiste</td><td>Maria Anders</td>
              <td>Obere Str. 57</td><td>Berlin</td><td>12209</td><td>Germany</td>
            </tr>
          </tbody>
        </table>
        """
    )
    ths = doc2.xpath("./thead/tr/th")
    persistent = [th.text.strip() for th in ths if th.get("data-priority") is None]
    priorities = {th.text.strip(): int(th.get("data-priority")) for th in ths if th.get("data-priority") is not None}
    hide_order = [name for name, _ in sorted(priorities.items(), key=lambda kv: -kv[1])]
    actual_2 = {
        "mode": doc2.get("data-mode"),
        "column_btn_text": doc2.get("data-column-btn-text"),
        "persistent_columns": persistent,
        "hide_order_highest_number_first": hide_order,
    }
    expected_2 = {
        "mode": "columntoggle",
        "column_btn_text": "点我显示或隐藏列!",
        "persistent_columns": ["CustomerName"],
        "hide_order_highest_number_first": ["CustomerID", "Country", "PostalCode", "City", "Address", "ContactName"],
    }
    cases.append({
        "name": "columntoggle_priority_and_button_text",
        "summary": "Verify data-mode='columntoggle', persistent column without data-priority, priority 6-to-1 hide order, and data-column-btn-text.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Table visual styles (ui-shadow, row border-bottom, th border + zebra striping)
    doc3 = html.fragment_fromstring(
        """
        <div>
          <table data-role="table" data-mode="columntoggle" class="ui-responsive ui-shadow" id="styledTable">
            <thead><tr><th>Col1</th><th>Col2</th></tr></thead>
            <tbody><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></tbody>
          </table>
          <style>
            tr { border-bottom: 1px solid #d6d6d6; }
            th { border-bottom: 1px solid #d6d6d6; }
            tr:nth-child(even) { background: #e9e9e9; }
          </style>
        </div>
        """
    )
    css_text = doc3.xpath("string(./style)")
    actual_3 = {
        "has_ui_shadow": "ui-shadow" in doc3.xpath("./table/@class")[0].split(),
        "has_tr_border_bottom": bool(re.search(r"tr\s*\{[^}]*border-bottom\s*:\s*1px solid #d6d6d6", css_text)),
        "has_th_border_bottom": bool(re.search(r"th\s*\{[^}]*border-bottom\s*:\s*1px solid #d6d6d6", css_text)),
        "has_even_zebra_bg": bool(re.search(r"tr:nth-child\(even\)\s*\{[^}]*background\s*:\s*#e9e9e9", css_text)),
    }
    expected_3 = {
        "has_ui_shadow": True,
        "has_tr_border_bottom": True,
        "has_th_border_bottom": True,
        "has_even_zebra_bg": True,
    }
    cases.append({
        "name": "table_shadow_and_css_styling",
        "summary": "Verify ui-shadow class and custom CSS rules for row borders, header borders, and even-row striping.",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    return cases


def main():
    print(json.dumps({"version": VERSION, "cases": run_cases()}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
确认 reflow 表格含 thead/tbody 且无 rowspan/colspan、columntoggle 常驻列与 6 到 1 隐藏顺序及自定义按钮文案,以及 ui-shadow、底边框与偶数行斑马纹 CSS 核验。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a2050.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:jQuery Mobile

课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip

下载课程关联工程包:jQuery Mobile

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2078.zip

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

data-priority="6" 的列优先级最低,在窄屏下最先被隐藏(依次为 6→5→4→3→2→1);未设置 data-priority 的列则始终保持显示,不会被自动隐藏。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习