亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

HTML 表的 PHP 類(lèi)別:奇怪的屬性分配
P粉384679266
P粉384679266 2023-09-06 00:38:07
0
1
600
<p>在建立 HTML 表格的類(lèi)別中,我有這個(gè)呈現(xiàn)表格的方法。除了某些條件下的 HTML 屬性分配之外,一切都正常運(yùn)作(縮排、結(jié)束標(biāo)籤、資料表示等)。當(dāng)我設(shè)定單元格資料時(shí),我會(huì)呼叫 setData() 來(lái)接收三個(gè)參數(shù)並像這樣使用它。注意我如何設(shè)定第三個(gè)參數(shù)(單元格屬性):</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>通話(huà):</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>在此範(fàn)例中,表格有七個(gè)欄位。 </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> <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></pre> <p>這是我用來(lái)完成這一切的程式碼。我將向您展示單元格渲染的程式碼片段以及處理屬性的方法。 </p> <p><strong>儲(chǔ)存格渲染:</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>
P粉384679266
P粉384679266

全部回覆(1)
P粉349222772

好吧,當(dāng)我發(fā)布問(wèn)題並選擇程式碼時(shí),答案出現(xiàn)了。在方法 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 [];
}
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板