HTML 表的 PHP 类:奇怪的属性分配
P粉384679266
P粉384679266 2023-09-06 00:38:07
[PHP讨论组]
<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-&gt;setData( $pagination-&gt;resultset, [], // row attributes array( // cell attributes array(), // row 1 (index 0) array( // row2 (index 1) [&quot;id&quot;=&gt;&quot;R2C1id&quot;], // row 2, cell 1 [&quot;id&quot;=&gt;&quot;R2C2id&quot;, &quot;onclick&quot;=&gt;&quot;R2C2_onclick();&quot;], // row 2, cell 2 ), array( // row 3 [], [], [&quot;id&quot;=&gt;&quot;R3C3id&quot;, &quot;selected&quot;=&gt;&quot;selected&quot;], // row 3, cell 3 [], [], [], [] ) ) );</pre> <p>在此示例中,表格有七列。</p> <p>在这里您将看到 HTML 输出。注意第二行和第三行的单元格属性:</p> <pre class="brush:php;toolbar:false;">&lt;div class='table-body'&gt; &lt;div class='table-row'&gt; &lt;div class='table-row-cell'&gt;1&lt;/div&gt; &lt;div class='table-row-cell'&gt;Consumidor Final&lt;/div&gt; &lt;div class='table-row-cell'&gt;Consumidor Final&lt;/div&gt; &lt;div class='table-row-cell'&gt;&lt;/div&gt; &lt;div class='table-row-cell'&gt;1&lt;/div&gt; &lt;div class='table-row-cell'&gt;&lt;/div&gt; &lt;div class='table-row-cell'&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class='table-row'&gt; &lt;div class='table-row-cell' id='R2C1id'&gt;2&lt;/div&gt; &lt;div class='table-row-cell' id='R2C2id' onclick='R2C2_onclick();'&gt;Prueba SRL&lt;/div&gt; &lt;div class='table-row-cell' 0='Array' 1='Array'&gt;Tu Prueba&lt;/div&gt; &lt;div class='table-row-cell' 0='Array' 1='Array'&gt;12345678901&lt;/div&gt; &lt;div class='table-row-cell' 0='Array' 1='Array'&gt;1&lt;/div&gt; &lt;div class='table-row-cell' 0='Array' 1='Array'&gt;&lt;/div&gt; &lt;div class='table-row-cell' 0='Array' 1='Array'&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class='table-row'&gt; &lt;div class='table-row-cell'&gt;3&lt;/div&gt; &lt;div class='table-row-cell'&gt;Otra Prueba SA&lt;/div&gt; &lt;div class='table-row-cell' id='R3C3id' selected='selected'&gt;Prueba 2&lt;/div&gt; &lt;div class='table-row-cell'&gt;12345678902&lt;/div&gt; &lt;div class='table-row-cell'&gt;1&lt;/div&gt; &lt;div class='table-row-cell'&gt;&lt;/div&gt; &lt;div class='table-row-cell'&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre> <p>这是我用来完成这一切的代码。我将向您展示单元格渲染的代码段以及处理属性的方法。</p> <p><strong>单元格渲染:</strong></p> <pre class="brush:php;toolbar:false;">// process cells $row_counter = 0; foreach ($this-&gt;data as $data) { // row $row_build = &quot;&quot;; $cell_counter = 0; foreach ($data as $cell_data) { // cell if ($cell_counter &lt; $col_count) { $row_build .= $this-&gt;getHtmlDiv( $html_cell_class, $cell_data ?? &quot;&quot;, $this-&gt;getHtmlAttributes(&quot;cell&quot;, $row_counter, $cell_counter), 3 ); } $cell_counter++; } // $cell_counter++; // complete empty cells to preserve row:hover on full row while ($cell_counter &lt; $col_count) { $row_build .= $this-&gt;getHtmlDiv( $html_cell_class, &quot;&quot;, $this-&gt;getHtmlAttributes(&quot;cell&quot;, $row_counter, $cell_counter), 3 ); $cell_counter++; } $body_build .= $this-&gt;getHtmlDiv( $html_row_class, $row_build, $this-&gt;getHtmlAttributes(&quot;row&quot;, $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-&gt;html_attributes[$section]) &gt; 0) { if (array_key_exists($row, $this-&gt;html_attributes[$section])) { if ($section === &quot;cell&quot;) { if (array_key_exists($column, $this-&gt;html_attributes[$section][$row])) { return $this-&gt;html_attributes[$section][$row][$column]; } } return $this-&gt;html_attributes[$section][$row]; } } return []; }</pre> <p>怎么了?谢谢。</p>
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中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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