Add attributes to a table header

Add attributes to a table header

wót Álvaro Gallart -
Anzahl Antworten: 2

  • Moodle v3.6.3
  • Table to display in a plugin.

My current code:

$table = new html_table();
        $table->id = 'table-jxx';
        $table->attributes['data-toggle'] = 'table';
        $table->attributes['data-url'] = $url;

        $table_head = array(get_string("course", "document"), 
                                            get_string("teacher", "document"), 
                                            get_string("modulename", "assign"), 
str_replace(" ", "<br/>",   get_string("documents_number", "document")),
                                            get_string("minimum", "document"), 
                                            get_string("maximum", "document"), 
                                            get_string("average", "document"));
       
         $table->head  = $table_head;

        foreach ($rows as $row){
            $table>data[] = array ($row["course"],$row["teacher"], $row["assign"], $row["analyzed_documents_count"], $row["minimum_rate"], $row["maximum_rate"], $row["average_rate"]);
        }
        echo html_writer::table($table);


      How can I put attributes to the "th" elements of the table?

something like this ....

table->head->attribute=array('data-sortable'=>'true');


Als Antwort auf Álvaro Gallart

Re: Add attributes to a table header

wót Renaat Debleu -
Nutzerbild von Core developers Nutzerbild von Particularly helpful Moodlers Nutzerbild von Plugin developers

Code like $table->colclasses = ['leftalign', 'rightalign']; let you align the tables if you provide a .leftalign and .rightalign css code.  But if you want to add sorting, you will have to use a table_sql instead of a html_table. For a good sample have a look at the core_tag_manage_table that implements sorting and collapsing.

 

Als Antwort auf Álvaro Gallart

Re: Add attributes to a table header

wót Álvaro Gallart -
I have finally used javascript, although I would like to use only the moodle render, any help is welcome

document.querySelectorAll('#table-js thead tr th').forEach((el, index) => {
switch (index) {
case 0:
el.setAttribute('data-field','course');
el.setAttribute('data-sortable','true');
el.setAttribute('data-sorter','urlSorter');
break;
}
});