HTML 表的 PHP 类:奇怪的属性分配
P粉384679266
P粉384679266 2023-09-06 00:38:07
0
1
436
<p>在构建 HTML 表格的类中,我有这个呈现表格的方法。除了某些条件下的 HTML 属性分配之外,一切都工作正常(缩进、结束标签、数据表示等)。当我设置单元格数据时,我调用 setData() 来接收三个参数并像这样使用它。注意我如何设置第三个参数(单元格属性):</p> <p><strong>方法定义:</strong></p> <pre class="brush:php;toolbar:false;">public function setData( array $data, array $row_attributes = [], array $cell_attributes = [] ): bool { // the code }</pre> <p><strong>通话:</strong></p> <pre class="brush:php;toolbar:false;">$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 [], [], [], [] ) ) );</pre> <p>在此示例中,表格有七列。</p> <p>在这里您将看到 HTML 输出。注意第二行和第三行的单元格属性:</p> <pre class="brush:php;toolbar:false;"><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>
1
;
;
;
;
3
; <div class='table-row-cell'>Otra Prueba SA</div>
Prueba 2
;
12345678902
1
;
;
;
;</pre> <p>这是我用来完成这一切的代码。我将向您展示单元格渲染的代码段以及处理属性的方法。</p> <p><strong>单元格渲染:</strong></p> <pre class="brush:php;toolbar:false;">// 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++; }</pre> <p><strong>属性的方法:</strong></p> <pre class="brush:php;toolbar:false;">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 []; }</pre> <p>怎么了?谢谢。</p>
1
0
0
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 [];
}
热门专题
更多>
热门文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!