/** * Generate table rows as JSON * * @param mr_html_table $table Table instance * @param mr_html_paging $paging Paging instance * @return void * @todo Make this not a hack? */ public function mr_html_table_json(mr_html_table $table, mr_html_paging $paging) { if (optional_param('tjson', 0, PARAM_BOOL)) { $json = new stdClass(); $json->recordsReturned = (int) count($table->get_rows()); $json->totalRecords = (int) $paging->get_total(); $json->startIndex = (int) $paging->get_page(); $json->sort = $table->get_sort(); $json->pageSize = (int) $paging->get_perpage(); $json->records = array(); $json->emptyMessage = $table->get_emptymessage(); if ($table->get_order() == SORT_ASC) { $json->dir = 'asc'; } else { $json->dir = 'desc'; } // If we are returning 0 records, we probably don't have any at all if ($json->recordsReturned == 0) { $json->totalRecords = 0; } $columns = $table->get_columns(true); $htmlrows = $this->convert_to_htmlrows($table); foreach ($htmlrows as $htmlrow) { $position = 0; foreach ($columns as $column) { if (array_key_exists($position, $htmlrow->cells)) { $value = $htmlrow->cells[$position]->text; } else { $value = ''; } $record[$column->get_name()] = $value; $position++; } $json->records[] = (object) $record; } echo json_encode($json); die; } }