Skip to main content Link Menu Expand (external link) Document Search Copy Copied
This site uses Just the Docs, a documentation theme for Jekyll.

Using RecordGrid

Xataface RecordGrid Class and Template

Also see .

Introduction

As we learned in Actions I: The Basics , we can retrieve custom data from the DB in action via queries. This site deals with showing data in tabular form, like it could be received from even such a query. It is done by using Dataface_RecordGrid.

First Example

class actions_testTableAction {
 // Will be called from Xataface, if this action is called
 function handle(&$params){
 $this->app =& Dataface_Application::getInstance(); // reference to Dataface_Application object
 // Custom query
 $result = mysql_query("select * from testTable", $this->app->db());
 $body = "<br /><br />";
 
 if(!$result)
 {
 // Error handling
 $body .= "MySQL Error ...";
 }else
 {
 while($row = mysql_fetch_assoc($result)) // Fetch all rows
 {
 // Maybe do something with the single rows
 $data[] = $row; // Add singe row to the data
 }
 mysql_free_result($result); // Frees the result after finnished using it
 $grid = new Dataface_RecordGrid($data); // Create new RecordGrid with the data
 
 $body .= $grid->toHTML(); // Get the HTML of the RecordGrid
 }
 // Shows the content (RecordGrid or error message) in the Main Template
 df_display(array('body' => $body), 'Dataface_Main_Template.html');
 }
}

You get your data from the query, fetch it into an associative array, create with it an RecordGrid and use the toHTML function. This is sure better than manually create the html tags around the data.

Screenshot: Blank RecordGrid

!

Screenshot: ResultList

!

Colored Example

As you see above, the resultlist has colored rows, our First Example not. But you can easily change this, when you override the Dataface_RecordGrid.html template:

<table id="{$id}" class="listing {$class}">
 <thead>
 <tr>
 {foreach from=$labels item=label}
 <th>{$label}</th>
 {/foreach}
 </tr>
 </thead>
 <tbody>
 {section name=row loop=$data}
 <tr class="listing {cycle values="odd,even"}">
 {foreach from=$columns item=col}
 <td>{$data[row][$col]}</td>
 {/foreach}
 </tr>
 {/section}
 </tbody>
</table>

The magic happens in <tr class="listing {cycle values="odd,even"}"> The {cycle values="odd,even"} value cycles these two values and creates so the alternating row classes, like they are in the RecordList. By the way, {cycle ...} is a Smarty Function, see Smarty Doc.

(Find fruther information to template overriding in the tutorial and its links: Changing the Look and Feel)

Screenshot: colored RecordGrid

!

Example completly imitating ResultList

The Colored Example doesn’t look like the ResulList? Its rows aren’t as high as theres? That’s a styling matter or rather in this case a cause of the checkboxes. Here an example with checkboxes and added (empty) links, so it looks completly like the ResultList:

Change the part in action.php

 while($row = mysql_fetch_assoc($result)) // Fetch all rows
 {
 // Maybe do something with the single rows
 $row['<input type="checkbox">'] = '<input type="checkbox">';
 $data[] = $row; // Add singe row to the data
 }
 mysql_free_result($result); // Frees the result after finnished using it
 $grid = new Dataface_RecordGrid($data, // Create new RecordGrid with the data
 array('<input type="checkbox">', 'testID', 'name', 'description', 'number'), 
 //Order and selection of the colums
 null); // No other labels defined -> it uses keys of the associative array
 
 $body .= $grid->toHTML(); // Get the HTML of the RecordGrid

Dataface_RecordGrid.html template

<table id="{$id}" class="listing {$class}">
 <thead>
 <tr>
 {foreach from=$labels item=label}
 <th><a class="unmarked_link">{$label}</a></th>
 {/foreach}
 </tr>
 </thead>
 <tbody>
 {section name=row loop=$data}
 <tr class="listing {cycle values="odd,even"}">
 {foreach from=$columns item=col}
 <td><a class="unmarked_link">{$data[row][$col]}</a></td>
 {/foreach}
 </tr>
 {/section}
 </tbody>
</table>
Screenshot: RecordGrid completly imitating ResultList

!

Screenshot: ResultList

!

Try it out

Use the following code with static test data and any template in this article (or even without overriding it) to try it out.

class actions_testTableAction {
 // Will be called from Xataface, if this action is called
 function handle(&$params){
 $this->app =& Dataface_Application::getInstance(); // reference to Dataface_Application object
 $result_dummy = array(
 array('testID' => '1', 'name' => 'testname', 
 'description' => 'a short description', 'number' => '258'),
 array('testID' => '2', 'name' => 'another name', 
 'description' => 'a bit longer description to this data set', 'number' => '946'),
 array('testID' => '3', 'name' => 'dummy name', 
 'description' => 'yea, a dummy data set!', 'number' => '1342'),
 array('testID' => '4', 'name' => 'not empty', 
 'description' => 'this data set isn\'t empty ...', 'number' => '282'),
 array('testID' => '5', 'name' => 'your entry', 
 'description' => 'this entry is only for you', 'number' => '79'),
 array('testID' => '6', 'name' => 'no idea', 
 'description' => 'running out of ideas ...', 'number' => '203'),
 array('testID' => '7', 'name' => 'the last one', 
 'description' => 'the end', 'number' => '26841')
 );
 $body = "<br /><br />";
 
 foreach($result_dummy as $row) // Fetch all rows
 {
 // Maybe do something with the singe rows
 $row['<input type="checkbox">'] = '<input type="checkbox">';
 $data[] = $row; // Add singe row to the data
 }
 $grid = new Dataface_RecordGrid($data, // Create new RecordGrid with the data
 array('<input type="checkbox">', 'testID', 'name', 'description', 'number'), 
 //Order and selection of the colums
 null); // No other labels defined -> it uses keys of the associative array
 $body .= $grid->toHTML(); // Get the HTML of the RecordGrid
 // Shows the content (RecordGrid or error message) in the Main Template
 df_display(array('body' => $body), 'Dataface_Main_Template.html');
 }
}

AltStyle によって変換されたページ (->オリジナル) /