HarmonyOS 鸿蒙NEXT中ArkUI开发,后续是否会推出表格组件?

HarmonyOS 鸿蒙NEXT中ArkUI开发,后续是否会推出表格组件?

5 回复

您好,您的问题可参考这个帖子: https://developer.huawei.com/consumer/cn/forum/topic/0202185716837966007

具体解决方案如下:

使用Web组件模拟表格: 可以通过使用Web组件来引入一个本地的HTML文件,在HTML中绘制表格,不仅可以展示数据,还可以通过HTML和JavaScript实现对表格得操作。

ArkTS代码:

import web_webview from '@ohos.web.webview';

@Entry
@Component
struct TableHtml {
    controller: web_webview.WebviewController = new web_webview.WebviewController();

    build() {
        Column() {
            Web({ src: $rawfile('excelTest.html'), controller: this.controller})
              .domStorageAccess(true)
              .javaScriptAccess(true)
              .width('100%')
              .height('100%')
            // 其他配置
        }
    }
}

HTML部分代码:

<!DOCTYPE html>
<html>
<head>
    <title>简易在线表格</title>
    <style>
        .excel-table {
            border-collapse: collapse;
            margin: 20px;
        }

        .excel-table th,
        .excel-table td {
            border: 1px solid #ddd;
            padding: 8px;
            min-width: 80px;
            height: 30px;
        }

        .excel-table th {
            background-color: #f2f2f2;
            position: sticky;
            top: 0;
        }

        .excel-table tr:hover td {
            background-color: #f5f5f5;
        }

        .column-header {
            background-color: #e0e0e0;
            user-select: none;
        }

        .row-header {
            background-color: #f0f0f0;
            text-align: center;
            user-select: none;
        }

        .selected {
            background-color: #b3d4fc !important;
        }

        .toolbar {
            margin: 10px;
            padding: 10px;
            background: #f8f8f8;
            border: 1px solid #ddd;
        }
    </style>
</head>
<body>
<div class="toolbar">
    <button onclick="addRow()">添加行</button>
    <button onclick="addColumn()">添加列</button>
    <button onclick="deleteSelected()">删除选中</button>
    <button onclick="clearAll()">清空表格</button>
</div>

<table id="excelTable" class="excel-table">
    <thead>
    <tr id="headerRow">
        <th class="row-header"></th>
        <th class="column-header" contenteditable="true">A</th>
        <th class="column-header" contenteditable="true">B</th>
        <th class="column-header" contenteditable="true">C</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <th class="row-header">1</th>
        <td contenteditable="true"></td>
        <td contenteditable="true"></td>
        <td contenteditable="true"></td>
    </tr>
    <tr>
        <th class="row-header">2</th>
        <td contenteditable="true"></td>
        <td contenteditable="true"></td>
        <td contenteditable="true"></td>
    </tr>
    <tr>
        <th class="row-header">3</th>
        <td contenteditable="true"></td>
        <td contenteditable="true"></td>
        <td contenteditable="true"></td>
    </tr>
    </tbody>
</table>

<script>
    let selectedCell = null;
    let isMouseDown = false;

    // 初始化表格
    document.querySelectorAll('#excelTable td').forEach(cell => {
        cell.addEventListener('mousedown', startSelection);
        cell.addEventListener('mouseover', selectCells);
        cell.addEventListener('mouseup', endSelection);
        cell.addEventListener('click', selectCell);
    });

    // 单元格选择逻辑
    function startSelection(e) {
        isMouseDown = true;
        selectCell(e);
    }

    function selectCells(e) {
        if (isMouseDown && selectedCell) {
            const currentCell = e.target;
            const start = selectedCell;
            const end = currentCell;
            const startRow = Math.min(start.parentNode.rowIndex, end.parentNode.rowIndex);
            const endRow = Math.max(start.parentNode.rowIndex, end.parentNode.rowIndex);
            const startCol = Math.min(start.cellIndex, end.cellIndex);
            const endCol = Math.max(start.cellIndex, end.cellIndex);

            document.querySelectorAll('#excelTable td').forEach(cell => {
                const row = cell.parentNode.rowIndex;
                const col = cell.cellIndex;
                if (row >= startRow && row <= endRow && col >= startCol && col <= endCol) {
                    cell.classList.add('selected');
                } else {
                    cell.classList.remove('selected');
                }
            });
        }
    }

    function endSelection() {
        isMouseDown = false;
    }

    function selectCell(e) {
        if (e.target.tagName === 'TD') {
            document.querySelectorAll('#excelTable td').forEach(cell => cell.classList.remove('selected'));
            e.target.classList.add('selected');
            selectedCell = e.target;
        }
    }

    // 添加行
    function addRow() {
        const newRow = document.createElement('tr');
        const rowIndex = document.getElementById('excelTable').rows.length - 1;
        const rowHeader = document.createElement('th');
        rowHeader.className = 'row-header';
        rowHeader.textContent = rowIndex;

        newRow.appendChild(rowHeader);

        document.querySelectorAll('#excelTable tr:first-child th').forEach((th, i) => {
            if (i > 0) {
                const newCell = document.createElement('td');
                newCell.contentEditable = 'true';
                newRow.appendChild(newCell);
            }
        });

        document.getElementById('excelTable').tBodies[0].appendChild(newRow);
        updateRowHeaders();
    }

    // 添加列
    function addColumn() {
        const newHeader = document.createElement('th');
        newHeader.className = 'column-header';
        newHeader.contentEditable = 'true';
        newHeader.textContent = String.fromCharCode(65 + document.querySelectorAll('#excelTable th').length - 1);

        document.getElementById('headerRow').appendChild(newHeader);

        document.querySelectorAll('#excelTable tr').forEach(tr => {
            const newCell = document.createElement('td');
            newCell.contentEditable = 'true';
            tr.appendChild(newCell);
        });
    }

    // 删除选中
    function deleteSelected() {
        document.querySelectorAll('.selected').forEach(cell => cell.remove());
        updateRowHeaders();
    }

    // 清空表格
    function clearAll() {
        document.querySelectorAll('#excelTable td').forEach(cell => cell.textContent = '');
    }

    // 更新行号
    function updateRowHeaders() {
        document.querySelectorAll('#excelTable tr').forEach((tr, index) => {
            if (index > 0) {
                tr.querySelector('.row-header').textContent = index;
            }
        });
    }
</script>
</body>
</html>

更多关于HarmonyOS 鸿蒙NEXT中ArkUI开发,后续是否会推出表格组件?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


可以用ForEach 和 Row、Column、Text、Divider等组件模拟。

表格

treeview pc那种

制作工具类APP非常需要 现在的UI组件不是很切合PC端

目前华为官方未正式公布ArkUI在鸿蒙NEXT中是否会推出表格组件。根据鸿蒙开发者文档,ArkUI当前主要提供基础组件和容器组件,如List、Grid等。表格功能可通过组合现有组件实现,但原生表格组件尚未出现在已公开的路线图中。建议关注HDC开发者大会及官方更新日志获取最新组件动态。

根据目前HarmonyOS的发展路线和ArkUI的组件迭代趋势,表格组件确实在开发计划中。华为开发者团队已经注意到企业应用开发中对数据表格展示的强烈需求,预计会在后续的HarmonyOS NEXT版本中推出功能完善的表格组件,包括排序、分页、单元格合并等企业级功能。建议关注HDC开发者大会和官方文档更新,以获取最新组件发布信息。

回到顶部