Basic Usage: PHP arrays

This is the most basic case where you have a PHP array containing data and want to easily create an HTML table, with various handy features such as sorting, captions, custom value formats, etc.

$SimpleTable = new ImpTable( 
  array( 
    array("foo", "bar"), 
    array("baaz", "quux") 
  )
); 
$SimpleTable->Caption = 'Metasyntactic'; 
$SimpleTable->generate();

Styling and Scripting

ImpTable exposes its structure through CSS and you can assign arbitrary attributes using the ImpTable->Attributes array:

$QueryTable->Attributes['id'] = "ReportTable5";
$QueryTable->Attributes['class'] = "ReportTable";

For a full example of how this can be used, you can see the optional database profiling report in ImpDB->printQueryLog(): ImpDB.php

The same approach can be used to add custom formatting callbacks, sort functions, etc. in your javascript before calling ImpTable->generate(). This works exactly as it does in normal DataTables - if it doesn't, please let me know!

Remote Data

Frequently you want to load data which is generated externally and doesn't slow your initial page rendering with all of that data. The assumption in ImpTable is that it's most useful to help keep the simple stuff simple. The examples below illustrate various was to load external data for simple needs. The last example shows how you can create an external DataSource and use that when your requirements become more complicated.

XML using xmlHttpRequest

$xhrTable = new ImpTable();
$xhrOptions = array(
  'resultNode' => 'row',
  'fields'     => array('col1','col2')
);
$xhrTable->useData('XML', 'data.xml', $xhrOptions);
// In a real project this would specify the initial search parameters:
$xhrTable->yuiDataTableOptions['initialRequest'] = "dummy=…"; 
$xhrTable->Caption = 'XML loaded using XHR';
$xhrTable->generate();       
      

JSON using xmlHttpRequest

$jsonTable = new ImpTable();
$jsonOptions = array(
  'resultsList' => "metavars", 
  'fields'      => array("col1","col2") 
);
$jsonTable->useData('JSON', 'data.json', $jsonOptions);
$jsonTable->Caption = 'JSON loaded using XHR';
$jsonTable->generate();

Text using xmlHttpRequest

$TextTable = new ImpTable();
$TextTable->Attributes['id'] = "TextTable";
$txtSchema = array(
  'recordDelim' => "\n", 
  'fieldDelim'  => "\t", 
  'fields'      => array('Column 1', 'Column 2')
);
$TextTable->useData('Text', 'data.txt', $txtSchema);
$TextTable->Caption = 'Text loaded using XHR';
$TextTable->generate();  

Using an existing YUI DataSource

<script type="text/javascript" charset="utf-8">
  my_preloaded_datasource                = new YAHOO.util.DataSource("data.xml?");
  my_preloaded_datasource.responseType   = YAHOO.util.DataSource.TYPE_XML;
  my_preloaded_datasource.responseSchema = {
    resultNode:'row',
    fields: ["col1", "col2"]
  };
</script>

<?php
$externalDSTable = new ImpTable();
$externalDSTable->ColumnHeaders = array(
  array('key'=>"col1", "label" => "Column 1", "sortable" => false), 
  array("key" => "col2", "label" => "Column 2", "sortable" => true)
);
$externalDSTable->useData('DataSource', 'my_preloaded_datasource');
$externalDSTable->Caption = 'XML loaded from an existing YUI DataSource';
$externalDSTable->generate();  
?>