Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There you have it #2

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Easy tabular data for CakePHP (cakephp.org)
-- by Robert Ross ([email protected])
-- available at http://github.com/rross0227
-- requires CakePHP 1.3.x
-- requires CakePHP 2.x

Copyright (c) 2011 The Daily Save, LLC. All rights reserved.

Expand Down Expand Up @@ -47,7 +47,7 @@ In your view file you can now create grids easily by doing something like this:

$this->Grid->addAction('Edit', array('controller' => 'orders', 'action' => 'edit'), array('/Order/id'));

echo $this->Grid->generate($results);
echo $this->Grid->generate($orders);

This will create a 4 column grid (including actions) for all of your orders or whatever you like!
CakeGrid uses the Set::extract format found here: http://book.cakephp.org/view/1501/extract
Expand Down Expand Up @@ -133,6 +133,13 @@ This will output in the cell the users first and last name together. Concat uses
'/User/register_ip'
));

## Paginate
By setting paginate as true you instruct CakeGrid to use the Paginator::sort method to create the headers of your table

$this->Grid->addColumn('User', '/User/name', array('paginate'=>true));

CakeGrid won't add pagination links at the end of the table, you should add them yourself if you need them.

## Elements

CakeGrid allows the usage of your own elements to be used in cells. This is useful if you're wanting to use a hasMany relationship into a dropdown or something similar.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions View/Elements/table/grid_headers.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<tr class="<?php echo $options['class_header'] ?>">
<?php foreach($headers as $header) { ?>
<th>
<?php if ($header['options']['paginate']) {
echo $this->Paginator->sort(strtolower($header['title']));
} else {
echo $header['title'];
}?>
</th>
<?php } ?>
</tr>
File renamed without changes.
75 changes: 43 additions & 32 deletions views/helpers/grid.php → View/Helper/GridHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class GridHelper extends AppHelper {
public $name = 'Grid';
public $plugin_name = 'cake_grid';
public $plugin_name = 'CakeGrid';

/**
* Load html helper for links and such
Expand Down Expand Up @@ -53,7 +53,8 @@ class GridHelper extends AppHelper {
*
* @author Robert Ross
*/
function __construct(){
function __construct(View $View, $settings = array()){
parent::__construct( $View,$settings);
$this->options(array());
}

Expand Down Expand Up @@ -108,7 +109,8 @@ function addColumn($title, $valuePath, array $options = array()){
'type' => 'string',
'element' => false,
'linkable' => false,
'total' => false
'total' => false,
'paginate' => false
);

$options = array_merge($defaults, $options);
Expand Down Expand Up @@ -159,8 +161,8 @@ function addAction($name, array $url, array $trailingParams = array(), array $op
* @author Robert Ross
*/
function generate($results){
$View = $this->__view();
$View = $this->_View;

$directory = $this->__settings['type'];

if($this->__settings['type'] == 'csv' && !empty($this->__totals)){
Expand All @@ -178,17 +180,21 @@ function generate($results){
'plugin' => $this->plugin_name,
'headers' => $this->__columns,
'options' => $this->__settings
));
$results = $this->results($results);

),
array( 'plugin' => $this->plugin_name)
);

$results = $this->results($results);
$generated = $View->element($this->elemDir . DS . 'grid_full', array(
'plugin' => $this->plugin_name,
// 'plugin' => $this->plugin_name,
'headers' => $headers,
'results' => $results,
'options' => $this->__settings
));

return $generated;
),
array( 'plugin' => $this->plugin_name)
);

return $generated;
}

/**
Expand All @@ -200,7 +206,7 @@ function generate($results){
*/
function results($results = array()){
$rows = array();
$View = $this->__view();
$View = $this->_View;

foreach($results as $key => $result){
//-- Loop through columns
Expand All @@ -215,7 +221,9 @@ function results($results = array()){
'zebra' => $key % 2 == 0 ? 'odd' : 'even',
'rowColumns' => $rowColumns,
'options' => $this->__settings
));
),
array( 'plugin' => $this->plugin_name)
);
}

if(!empty($this->__totals)){
Expand All @@ -238,7 +246,7 @@ function results($results = array()){
}

if($this->__settings['type'] == 'csv'){
$total = intval(str_replace(array('$', ','), '', $total));
$total = floatval(str_replace(array('$', ','), '', $total));
$totalColumns[] = $total;
continue;
}
Expand All @@ -255,7 +263,9 @@ function results($results = array()){
'rowColumns' => $totalColumns,
'options' => $this->__settings,
'zebra' => 'totals'
));
),
array( 'plugin' => $this->plugin_name)
);
}

//-- Upon review, this if statement is hilarious
Expand All @@ -264,9 +274,10 @@ function results($results = array()){
'plugin' => $this->plugin_name,
'colspan' => sizeof($this->__columns) + (sizeof($this->__actions) ? 1 : 0),
'options' => $this->__settings
));
),
array( 'plugin' => $this->plugin_name));
}
return implode("\n", $rows);
}

Expand Down Expand Up @@ -322,9 +333,9 @@ private function __generateColumn($result, $column){
}

if(isset($column['options']['element']) && $column['options']['element'] != false){
$View = $this->__view();
$View = $this->_View;

return $View->element($this->elemDir . DS . $column['options']['element'], array('result' => $value));
return $View->element($this->elemDir . DS . $column['options']['element'], array('result' => $value));
} else {
if(isset($column['options']['type']) && $column['options']['type'] == 'date'){
$value = date('m/d/Y', strtotime($value));
Expand All @@ -333,7 +344,7 @@ private function __generateColumn($result, $column){
} else if(isset($column['options']['type']) && $column['options']['type'] == 'money' && $this->__settings['type'] != 'csv'){
$value = money_format('%n', $value);
} else if(isset($column['options']['type']) && $column['options']['type'] == 'actions'){
$View = $this->__view();
$View = $this->_View;
$actions = array();

//-- Need to retrieve the results of the trailing params
Expand All @@ -352,12 +363,12 @@ private function __generateColumn($result, $column){
}

$actions[$name] = array(
'url' => Router::url($action['url'] + $trailingParams),
'url' => $action['url'] + $trailingParams,
'options' => $action['options']
);
}

return $View->element($this->elemDir . DS . 'column_actions', array('plugin' => $this->plugin_name, 'actions' => $actions), array('Html'));
return $View->element($this->elemDir . DS . 'column_actions', array( 'actions' => $actions), array('plugin' => $this->plugin_name));
}
}

Expand Down Expand Up @@ -423,13 +434,13 @@ function csvData($data){
* @return void
* @author Robert Ross
*/
private function __view() {
if (!empty($this->globalParams['viewInstance'])) {
$View = $this->globalParams['viewInstance'];
} else {
$View = ClassRegistry::getObject('view');
}

return $View;
}
// private function __view() {
// if (!empty($this->globalParams['viewInstance'])) {
// $View = $this->globalParams['viewInstance'];
// } else {
// $View = ClassRegistry::getObject('view');
// }
//
// return $View;
// }
}
5 changes: 0 additions & 5 deletions views/elements/table/grid_headers.ctp

This file was deleted.

Loading