Permalink
Browse files

reworked demo so that data in DemoDataTable is loaded from different

services to demonstrate how to load data from a database and to
demonstrate how any data source can be injected into a DataTable.
  • Loading branch information...
1 parent 6acff84 commit 463dc8ced2294ff33471c956fc34d25ae80a6dac @lampjunkie committed Jul 24, 2011
Showing with 91 additions and 140 deletions.
  1. +50 −138 demo/DemoDataTable.php
  2. +32 −1 demo/ajax.php
  3. 0 demo/{ → data}/browsers.csv
  4. +9 −1 demo/index.php
View
@@ -9,16 +9,13 @@
* file that was distributed with this source code.
*/
-// include the Browser entity class
-include('Browser.php');
-
/**
* Demonstration of implementing DataTable_DataTable
*
* This class shows how to extend and implement DataTable_DataTable
*
* As a simple example this table is set up as an AJAX-enabled
- * table and pulls it's data from the local 'browsers.csv' file.
+ * table and pulls it's data from a given IBrowserService implementation
*
*/
class DemoDataTable extends DataTable_DataTable
@@ -128,6 +125,18 @@ public function __construct(DataTable_Config $config = null)
}
/**
+ * Set the IBrowserService implementation
+ *
+ * This is the service object where we will pull our data from
+ *
+ * @param IBrowserService $browserService
+ */
+ public function setBrowserService(IBrowserService $browserService)
+ {
+ $this->browserService = $browserService;
+ }
+
+ /**
* Load the data for a request
*
* This demo emulates loading data from a database and performing
@@ -138,28 +147,47 @@ public function __construct(DataTable_Config $config = null)
*/
public function loadData(DataTable_Request $request)
{
- // get fake data set
- $results = $this->loadFakeData('browsers.csv');
-
- // get total length of all results (emulate count(*) query)
- $totalLength = count($results);
+ // get the name of the sort property that was passed in
+ $sortProperty = $this->config->getColumns()->get($request->getSortColumnIndex())->getSortKey();
- // search against object array if a search term was passed in
- if(!is_null($request->getSearch())){
- $results = $this->search($results, $request->getSearch(), $this->getSearchableColumnNames());
- }
-
- // get the count of the filtered results
- $filteredTotalLength = count($results);
+ // check if a search term was passed in
+ if($request->hasSearch()){
+
+ // call the searchAll() service method
+ $results = $this->browserService->searchAll($request->getSearch(),
+ $this->getSearchableColumnNames(),
+ $request->getDisplayStart(),
+ $request->getDisplayLength(),
+ $sortProperty,
+ $request->getSortDirection());
+
+ // get the total number of results (for pagination)
+ $totalLength = $this->browserService->searchAll($request->getSearch(),
+ $this->getSearchableColumnNames(),
+ $request->getDisplayStart(),
+ $request->getDisplayLength(),
+ $sortProperty,
+ $request->getSortDirection(),
+ true);
- // sort results by sort column passed in
- $this->sortObjectArray($this->config->getColumns()->get($request->getSortColumnIndex())->getSortKey(), $results, $request->getSortDirection());
+ } else {
+
+ // call the getAll() service method
+ $results = $this->browserService->getAll($request->getDisplayStart(),
+ $request->getDisplayLength(),
+ $sortProperty,
+ $request->getSortDirection());
+
+ // get the total number of results (for pagination)
+ $totalLength = $this->browserService->getAll($request->getDisplayStart(),
+ $request->getDisplayLength(),
+ $sortProperty,
+ $request->getSortDirection(),
+ true);
+ }
- // limit the results based on parameters passed in
- $this->limit($results, $request->getDisplayStart(), $request->getDisplayLength());
-
// return the final result set
- return new DataTable_DataResult($results, $totalLength, $filteredTotalLength);
+ return new DataTable_DataResult($results, $totalLength, $totalLength);
}
/**
@@ -213,120 +241,4 @@ function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
}
";
}
-
- /**
- * ==========================================================================
- * Utility Methods for Demo
- * ==========================================================================
- */
-
- /**
- * Get an array of Browser objects
- *
- * @return array
- */
- private function loadFakeData($file)
- {
- $browsers = array();
-
- if (($handle = fopen($file, "r")) !== FALSE) {
- while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
- $browsers[] = new Browser($data[0], $data[1], $data[2], $data[3], $data[4]);
- }
- fclose($handle);
- }
-
- return $browsers;
- }
-
- /**
- * Sort an array of objects by an object property
- *
- * @param string $field
- * @param array $arr
- * @param string $sorting
- * @param boolean $case_insensitive
- */
- private function sortObjectArray($field, &$arr, $sorting='asc', $case_insensitive=true)
- {
- $field = 'get' . ucfirst($field);
- if(is_array($arr) && (count($arr)>0) && ( ( is_array($arr[0]) && isset($arr[0][$field]) ) || ( is_object($arr[0]) && method_exists($arr[0], $field) ) ) ){
- if($case_insensitive==true) $strcmp_fn = "strnatcasecmp";
- else $strcmp_fn = "strnatcmp";
-
- if($sorting=='asc'){
- $fn = create_function('$a,$b', '
- if(is_object($a) && is_object($b)){
- return '.$strcmp_fn.'($a->'.$field.'(), $b->'.$field.'());
- }else if(is_array($a) && is_array($b)){
- return '.$strcmp_fn.'($a["'.$field.'()"], $b["'.$field.'"]);
- }else return 0;
- ');
- } else {
- $fn = create_function('$a,$b', '
- if(is_object($a) && is_object($b)){
- return '.$strcmp_fn.'($b->'.$field.'(), $a->'.$field.'());
- }else if(is_array($a) && is_array($b)){
- return '.$strcmp_fn.'($b["'.$field.'()"], $a["'.$field.'()"]);
- }else return 0;
- ');
- }
- usort($arr, $fn);
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Get a subset of an input array
- *
- * @param array $arr
- * @param integer $start
- * @param integer $length
- */
- private function limit(&$arr, $start, $length)
- {
- $arr = array_slice($arr, $start, $length);
- }
-
- /**
- * Search for a given search term within the given properties
- * of an array of objects
- *
- * @param array $objects
- * @param string $search
- * @param array $properties
- */
- private function search($objects, $search, $properties)
- {
- $results = array();
-
- $search = strtolower($search);
-
- $regex = "/^{$search}/";
-
- if(is_null($objects) || count($objects) == 0){
- return $results;
- }
-
- foreach($objects as $object){
-
- foreach($properties as $property){
-
- $getter = 'get' . ucfirst($property);
-
- $value = strtolower($object->$getter());
-
- preg_match($regex, $value, $matches);
-
- if(count($matches) > 0){
- $results[] = $object;
- break;
- }
- }
- }
-
- return $results;
- }
}
View
@@ -9,16 +9,47 @@
* file that was distributed with this source code.
*/
+/**
+ * This file shows how the AJAX data for a DataTable is rendered.
+ *
+ * By default this file is configured to load the data from a CSV file using the CsvBrowserService.
+ * This can be switched to use the DbBrowserService which will load the data from a MySQL database.
+ *
+ * This demonstrates how your DataTable doesn't need to be tied to any specific database implementation, etc.
+ * Thus, you can use your application's existing ORM, services, etc. to deal with the data
+ * and your DataTable doesn't need to care about where the data came from as long as it's
+ * provided an array of entity objects.
+ */
+
// register the DataTable autoloader
include('../src/DataTable/Autoloader.php');
spl_autoload_register(array('DataTable_Autoloader', 'autoload'));
// include the Demo DataTable class
-include('DemoDataTable.php');
+include_once('DemoDataTable.php');
+
+// define the Browser Service to use for this demo
+// either 'db' or 'csv'
+define('SERVICE_TO_USE', 'db');
+
+// build a Browser Service object based on the type that was selected
+if(SERVICE_TO_USE == 'db'){
+
+ include_once('DbBrowserService.php');
+ $browserService = new DbBrowserService('localhost', 'browsers', 'root', '');
+
+} elseif(SERVICE_TO_USE == 'csv'){
+
+ include_once('CsvBrowserService.php');
+ $browserService = new CsvBrowserService('data/browsers.csv');
+}
// instatiate new DataTable
$table = new DemoDataTable();
+// set selected Browser Service to the demo DataTable
+$table->setBrowserService($browserService);
+
// convert DataTable AJAX parameters in request to a DataTable_Request
$request = new DataTable_Request();
$request->fromPhpRequest($_REQUEST);
File renamed without changes.
View
@@ -15,6 +15,14 @@
*
* Be sure to download the DataTables jQuery plugin from http://http://datatables.net/download/
* and extract it within this folder
+ *
+ * Look at ajax.php to see how the data is pulled for the table. By default it is configured
+ * to load the data from a CSV file using the CsvBrowserService. This can be switched to use
+ * the DbBrowserService which will load the data from a MySQL database. This demonstrates
+ * how your DataTable doesn't need to be tied to any specific database implementation, etc.
+ * Thus, you can use your application's existing ORM, services, etc. to deal with the data
+ * and your DataTable doesn't need to care about where the data came from as long as it's
+ * provided an array of entity objects.
*/
// register the DataTable autoloader
@@ -55,4 +63,4 @@
<?php echo $table->render(); ?>
</body>
-</html>
+</html>

0 comments on commit 463dc8c

Please sign in to comment.