PHP-Klasse für HTML-Tabelle: seltsame Attributzuweisung
P粉384679266
P粉384679266 2023-09-06 00:38:07
0
1
383

In der Klasse, die die HTML-Tabelle erstellt, habe ich diese Methode, die die Tabelle rendert. Bis auf die Zuweisung von HTML-Attributen unter bestimmten Bedingungen (Einrückung, schließende Tags, Datendarstellung usw.) funktioniert alles einwandfrei. Wenn ich die Zelldaten festlege, rufe ich setData() auf, um die drei Parameter zu empfangen und sie so zu verwenden. Beachten Sie, wie ich den dritten Parameter (Zelleneigenschaft) festlege:

Methodendefinition:

public function setData( Array $data, Array $row_attributes = [], Array $cell_attributes = [] ): bool { // der Code }

Aufruf:

$table->setData( $pagination->resultset, [], // Zeilenattribute array( // Zellattribute array(), // Zeile 1 (Index 0) array( // Zeile2 (Index 1) ["id"=>"R2C1id"], // Zeile 2, Zelle 1 ["id"=>"R2C2id", "onclick"=>"R2C2_onclick();"], // Zeile 2, Zelle 2 ), array( // Zeile 3 [], [], ["id"=>"R3C3id", "selected"=>"selected"], // Zeile 3, Zelle 3 [], [], [], [] ) ) );

In diesem Beispiel hat die Tabelle sieben Spalten.

Hier sehen Sie die HTML-Ausgabe. Beachten Sie die Zelleigenschaften der zweiten und dritten Zeile:

1
Consumidor Final
Consumidor Final
1
2
Prueba SRL
Tu Prueba
12345678901
1
3
Otra Prueba SA
Prueba 2
12345678902
1

Das ist der Code, den ich für all das verwende. Ich zeige Ihnen den Codeausschnitt für das Rendern von Zellen und wie Sie mit Eigenschaften umgehen.

Zellenrendering:

// Zellen verarbeiten $row_counter = 0; foreach ($this->data as $data) { // Zeile $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++; // Leere Zellen vervollständigen, um Zeile beizubehalten: Bewegen Sie den Mauszeiger über die gesamte Zeile 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, WAHR ); $row_counter++; }

Attributmethoden:

private Funktion 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]; } } zurückkehren []; }

Was ist los? Danke.

P粉384679266
P粉384679266

Antworte allen (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 []; }
    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage
    Über uns Haftungsausschluss Sitemap
    Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!