深度解析Ant Design中Table組件的使用

一、Antd表格兼容

Antd是一個基於React的UI框架,Table組件是其重要的組成部分之一。該組件可在各種瀏覽器和設備上進行良好的兼容。同時,它還提供了多個版本的Antd框架,包括React和Vue版本,以滿足不同的開發需求。

以下是Antd表格的基本用法:

{
  dataSource = [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park',
    },
    {
      key: '2',
      name: 'Jim Green',
      age: 42,
      address: 'London No. 1 Lake Park',
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park',
    },
  ];

  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
    },
  ];

  <Table dataSource={dataSource} columns={columns} />;
}

二、Antd是個UI框架嗎

Antd是基於React的UI框架,它提供了一組易於使用、高質量的UI組件,以便加速Web應用程序的開發。除了Table組件,Antd還包括布局、表單、導航、圖表等組件。

以下是Antd中選擇器組件的示例:

{
  import { Select } from 'antd';

  const { Option } = Select;

  function handleChange(value) {
    console.log('selected', value);
  }

  <Select defaultValue="lucy" style={{ width: 120 }} onChange={handleChange}>
    <Option value="jack">Jack</Option>
    <Option value="lucy">Lucy</Option>
    <Option value="disabled" disabled>
      Disabled
    </Option>
    <Option value="Yiminghe">yiminghe</Option>
  </Select>
}

三、Antd表格分頁

Antd提供了一個內置的Pagination組件,可用於表格分頁。默認情況下,表格不分頁。當數據量很大時,我們可以啟用分頁以提高用戶體驗。

以下是Antd表格分頁組件的基本用法:

{
  import { Table, Pagination } from 'antd';

  function onShowSizeChange(current, pageSize) {
    console.log(current, pageSize);
  }

  function onChange(pageNumber) {
    console.log('Page: ', pageNumber);
  }

  const dataSource = [...];

  const columns = [...];

  <div>
    <Table dataSource={dataSource} columns={columns} pagination={{ pageSize: 5 }} />
    <Pagination showSizeChanger onShowSizeChange={onShowSizeChange} defaultCurrent={3} total={50} onChange={onChange} />
  </div>;
}

四、Antd表格寬度設置不生效

Antd表格中寬度的設置可以使用固定值或百分比值,但是有時候會出現寬度設置不生效的問題。

這時可以考慮使用fixed屬性來設置列的寬度。對於需要固定寬度的列,將其fixed屬性設置為”left”或”right”即可。

以下是Antd表格列寬度設置的示例:

{
  const dataSource = [...];

  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      width: 150,
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      width: 100,
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
    },
    {
      title: 'Action',
      key: 'action',
      fixed: 'right',
      width: 100,
      render: () => <a>Delete</a>,
    },
  ];

  <Table dataSource={dataSource} columns={columns} scroll={{ x: 500 }} />;
}

五、Antd表格排序

Antd提供了一種方便的方式對表格進行排序。您可以在表格列定義中設置sorter屬性,然後通過編程方式激活排序。

以下是Antd表格排序的示例:

{
  const dataSource = [...];

  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      sorter: (a, b) => a.name.localeCompare(b.name),
      sortDirections: ['descend', 'ascend'],
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      sorter: (a, b) => a.age - b.age,
      sortDirections: ['descend', 'ascend'],
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
    },
  ];

  <Table dataSource={dataSource} columns={columns} />;
}

六、Antd組件

除了Table組件之外,Antd還提供了許多其他組件,可以用來加速Web應用程序的開發。

以下是Antd中常用組件的示例:

{
  import { Button, Input, Badge } from 'antd';

  function handleClick(e) {
    console.log('click button');
  }

  <div>
    <Button type="primary" onClick={handleClick} style={{ marginBottom: 16 }}>
      Primary Button
    </Button>
    <Input placeholder="Basic usage" defaultValue="Antd Input Component" style={{ marginBottom: 16 }} />
    <Badge count={5} style={{ backgroundColor: '#4CAF50' }}>
      <a href="/" className="head-example" />
    </Badge>
  </div>;
}

七、Antd表格拖拽

Antd表格支持拖拽調整列寬和列排序。只需將Table組件的dragable屬性設置為true即可啟用拖拽功能。

以下是Antd表格拖拽功能的示例:

{
  const dataSource = [...];

  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      width: 150,
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      width: 100,
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
    },
  ];

  <Table dataSource={dataSource} columns={columns} dragable />;
}

八、Antd表格刪除後多選還在

Antd表格在刪除操作時會自動取消多選狀態,但如果您手動管理選擇狀態,則需要手動取消選中狀態。

以下是Antd表格刪除操作的示例:

{
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);

  function handleDelete() {
    // 判斷是否選中某一行,如果是,則移除該行
    if (selectedRowKeys.length > 0) {
      setSelectedRowKeys([]); // 取消選擇
    }
  }

  const dataSource = [...];

  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      width: 150,
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      width: 100,
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
    },
  ];

  <Table
    dataSource={dataSource}
    columns={columns}
    rowSelection={{
      selectedRowKeys,
      onChange: (selectedKeys) => setSelectedRowKeys(selectedKeys),
    }}
  />
  <Button onClick={handleDelete}>Delete</Button>;
}

九、Antd表格排序 取消排序

Antd表格支持取消排序。只需要將sortOrder屬性從”descend”或”ascend”設置為null即可取消排序。

以下是Antd表格取消排序的示例:

{
  const dataSource = [...];

  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      sorter: (a, b) => a.name.localeCompare(b.name),
      sortDirections: ['descend', 'ascend'],
      sortOrder: null,
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      sorter: (a, b) => a.age - b.age,
      sortDirections: ['descend', 'ascend'],
      sortOrder: null,
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
    },
  ];

  <Table dataSource={dataSource} columns={columns} />;
}

十、Antd表格內容過多隱藏

Antd表格在內容過多時,可以通過ellipsis屬性來隱藏內容。只需要在列定義中設置ellipsis為true即可。

以下是Antd表格內容隱藏的示例:

{
  const dataSource = [...];

  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      ellipsis: true,
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
      ellipsis: true,
    },
  ];

  <Table dataSource={dataSource} columns={columns} />;
}

以上就是Antd表格的詳細介紹,Antd提供了豐富的組件和功能,使得Web應用程序的開發更加簡單和快捷。在實際開發中,您可以根據具體需求選擇合適的組件和功能。

原創文章,作者:DGQJQ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/372792.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
DGQJQ的頭像DGQJQ
上一篇 2025-04-25 15:26
下一篇 2025-04-25 15:26

相關推薦

  • 如何修改ant組件的動效為中心

    當我們使用Ant Design時,其默認的組件動效可能不一定符合我們的需求,這時我們需要修改Ant Design組件動效,使其更加符合我們的UI設計。本文將從多個方面詳細闡述如何修…

    編程 2025-04-29
  • 深度查詢宴會的文化起源

    深度查詢宴會,是指通過對一種文化或主題的深度挖掘和探究,為參與者提供一次全方位的、深度體驗式的文化品嘗和交流活動。本文將從多個方面探討深度查詢宴會的文化起源。 一、宴會文化的起源 …

    編程 2025-04-29
  • Ant Design組件的動效

    Ant Design是一個基於React技術棧的UI組件庫,其中動效是該組件庫中的一個重要特性之一。動效的使用可以讓用戶更清晰、更直觀地了解到UI交互的狀態變化,從而提高用戶的滿意…

    編程 2025-04-29
  • Python下載深度解析

    Python作為一種強大的編程語言,在各種應用場景中都得到了廣泛的應用。Python的安裝和下載是使用Python的第一步,對這個過程的深入了解和掌握能夠為使用Python提供更加…

    編程 2025-04-28
  • Python遞歸深度用法介紹

    Python中的遞歸函數是一個函數調用自身的過程。在進行遞歸調用時,程序需要為每個函數調用開闢一定的內存空間,這就是遞歸深度的概念。本文將從多個方面對Python遞歸深度進行詳細闡…

    編程 2025-04-27
  • Spring Boot本地類和Jar包類載入順序深度剖析

    本文將從多個方面對Spring Boot本地類和Jar包類載入順序做詳細的闡述,並給出相應的代碼示例。 一、類載入機制概述 在介紹Spring Boot本地類和Jar包類載入順序之…

    編程 2025-04-27
  • 用mdjs打造高效可復用的Web組件

    本文介紹了一個全能的編程開發工程師如何使用mdjs來打造高效可復用的Web組件。我們將會從多個方面對mdjs做詳細的闡述,讓您輕鬆學習並掌握mdjs的使用。 一、mdjs簡介 md…

    編程 2025-04-27
  • 深度解析Unity InjectFix

    Unity InjectFix是一個非常強大的工具,可以用於在Unity中修復各種類型的程序中的問題。 一、安裝和使用Unity InjectFix 您可以通過Unity Asse…

    編程 2025-04-27
  • Spring MVC主要組件

    Spring MVC是一個基於Java語言的Web框架,是Spring Framework的一部分。它提供了用於構建Web應用程序的基本架構,通過與其他Spring框架組件集成,使…

    編程 2025-04-27
  • Mescroll.js——移動端下拉刷新和上拉載入更多組件

    一、概述 Mescroll.js是一款移動端的下拉刷新和上拉載入更多組件,因其簡單易用和功能強大而深受開發者的喜愛。Mescroll.js可以應用於各種移動端網站和APP,能夠支持…

    編程 2025-04-25

發表回復

登錄後才能評論