HTML 表的 PHP 类:奇怪的属性分配
P粉384679266
P粉384679266 2023-09-06 00:38:07
[PHP讨论组]

在构建 HTML 表格的类中,我有这个呈现表格的方法。除了某些条件下的 HTML 属性分配之外,一切都工作正常(缩进、结束标签、数据表示等)。当我设置单元格数据时,我调用 setData() 来接收三个参数并像这样使用它。注意我如何设置第三个参数(单元格属性):

方法定义:

public function setData(
        array $data,
        array $row_attributes = [],
        array $cell_attributes = []
    ): bool {
    // the code
}

通话:

$table->setData(
    $pagination->resultset,
    [], // row attributes
    array( // cell attributes
        array(), // row 1 (index 0)
        array( // row2 (index 1)
            ["id"=>"R2C1id"], // row 2, cell 1
            ["id"=>"R2C2id", "onclick"=>"R2C2_onclick();"], // row 2, cell 2
        ),
        array( // row 3
            [],
            [],
            ["id"=>"R3C3id", "selected"=>"selected"], // row 3, cell 3
            [],
            [],
            [],
            []
        )
    )
);

在此示例中,表格有七列。

在这里您将看到 HTML 输出。注意第二行和第三行的单元格属性:

<div class='table-body'>
    <div class='table-row'>
        <div class='table-row-cell'>1</div>
        <div class='table-row-cell'>Consumidor Final</div>
        <div class='table-row-cell'>Consumidor Final</div>
        <div class='table-row-cell'></div>
        <div class='table-row-cell'>1</div>
        <div class='table-row-cell'></div>
        <div class='table-row-cell'></div>
    </div>
    <div class='table-row'>
        <div class='table-row-cell' id='R2C1id'>2</div>
        <div class='table-row-cell' id='R2C2id' onclick='R2C2_onclick();'>Prueba SRL</div>
        <div class='table-row-cell' 0='Array' 1='Array'>Tu Prueba</div>
        <div class='table-row-cell' 0='Array' 1='Array'>12345678901</div>
        <div class='table-row-cell' 0='Array' 1='Array'>1</div>
        <div class='table-row-cell' 0='Array' 1='Array'></div>
        <div class='table-row-cell' 0='Array' 1='Array'></div>
    </div>
    <div class='table-row'>
        <div class='table-row-cell'>3</div>
        <div class='table-row-cell'>Otra Prueba SA</div>
        <div class='table-row-cell' id='R3C3id' selected='selected'>Prueba 2</div>
        <div class='table-row-cell'>12345678902</div>
        <div class='table-row-cell'>1</div>
        <div class='table-row-cell'></div>
        <div class='table-row-cell'></div>
    </div>
</div>

这是我用来完成这一切的代码。我将向您展示单元格渲染的代码段以及处理属性的方法。

单元格渲染:

// process cells
        $row_counter = 0;
        foreach ($this->data as $data) { // row
            $row_build = "";
            $cell_counter = 0;
            foreach ($data as $cell_data) { // cell
                if ($cell_counter < $col_count) {
                    $row_build .= $this->getHtmlDiv(
                        $html_cell_class,
                        $cell_data ?? "",
                        $this->getHtmlAttributes("cell", $row_counter, $cell_counter),
                        3
                    );
                }
                $cell_counter++;
            }

            // $cell_counter++;

            // complete empty cells to preserve row:hover on full row
            while ($cell_counter < $col_count) {
                $row_build .= $this->getHtmlDiv(
                    $html_cell_class,
                    "",
                    $this->getHtmlAttributes("cell", $row_counter, $cell_counter),
                    3
                );
                $cell_counter++;
            }

            $body_build .= $this->getHtmlDiv(
                $html_row_class,
                $row_build,
                $this->getHtmlAttributes("row", $row_counter, 0),
                2,
                true
            );
            $row_counter++;
        }

属性的方法:

private function getHtmlAttributes(string $section, int $row, int $column): array
{

    if (count($this->html_attributes[$section]) > 0) {
        if (array_key_exists($row, $this->html_attributes[$section])) {
            if ($section === "cell") {
                if (array_key_exists($column, $this->html_attributes[$section][$row])) {
                    return $this->html_attributes[$section][$row][$column];
                }
            }
            return $this->html_attributes[$section][$row];
        }
    }
    return [];
}

怎么了?谢谢。

P粉384679266
P粉384679266

全部回复(1)
P粉349222772

好吧,当我发布问题并选择代码时,答案出现了。在方法 getHtmlAttributes() 中,缺少 else 条件。

private function getHtmlAttributes(string $section, int $row, int $column): array
{

    if (count($this->html_attributes[$section]) > 0) {
        if (array_key_exists($row, $this->html_attributes[$section])) {
            if ($section === "cell") {
                if (array_key_exists($column, $this->html_attributes[$section][$row])) {
                    return $this->html_attributes[$section][$row][$column];
                } else { // <-- this 
                    return [];
                }
            }
            return $this->html_attributes[$section][$row];
        }
    }
    return [];
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号