CakeDataGridder
A CakePHP plugin to easy create DataGrids in CakePHP with functionalities as: pagination, sorting, searching etc.
Install / Use
/learn @BradCrumb/CakeDataGridderREADME
CakeDataGridder
A CakePHP plugin to easy create DataGrids in CakePHP with functionalities as: pagination, sorting, searching etc.
Requirements
The master branch has the following requirements:
- CakePHP 2.2.0 or greater.
- PHP 5.3.0 or greater.
- jQuery 1.9 or greater
Installation
- Clone/Copy the files in this directory into
app/Plugin/DataGridder - Ensure the plugin is loaded in
app/Config/bootstrap.phpby callingCakePlugin::load('DataGridder'); - Include the helper in your
AppController.php:public $helpers = array('DataGridder.DataGrid');
Documentation
In your view you can create a simple DataGrid by doing something like this:
$this->DataGrid->addColumn('Id', 'User.id');
$this->DataGrid->addColumn('Username', 'User.username', array('sort' => true));
//Actions column to add actions to the row
$this->DataGrid->addColumn('Actions', null, array('type' => 'actions'));
//Add delete action to the actions column
$this->DataGrid->addAction('Delete', array('action' => 'delete'), array('User.id'));
echo $this->DataGrid->generate($users);
This code will generate a DataGrid with 3 columns for all the users we pass. The DataGrid Helper uses the Set::extract format to query the array data. So instead of User.id you can also use /User/id.
When you are using multiple DataGrids on one page you can reset the DataGrid settings:
$this->DataGrid->reset();
Tree
It is also possible to give a threaded result from $this->Model->find('threaded') to the DataGrid. The Helper recognizes the array structure and gives you extra functionalities, like expanding and collapsing.
Columns
Adding a column can be done with several options, to add extra functionality to the column:
$this->DataGrid->addColumn($label, $valuePath, $options);
String column
The String column is the default type of column. It retrieves the value of the array and places it inside the column. Optionally any URLs or e-mail addresses can be autolinked by enabling the autoLink column option.
$options = array(
'type' => 'string',
'autoLink' => false
);
Image column
The Image column creates an Image tag of the retrieved value. When you have the BradCrumb/CakeImageCropResize plugin inside your project. You have extra functionality to resize and crop the image. For this type of column.
$this->DataGrid->addColumn('Image', 'User.image', array(
'type' => 'image',
'resize' => array(
'width' => 80,
'height' => 80,
'autocrop' => true,
'crop' => true
)
));
Switcher column
A switcher column switches a field between 2 states: enabled and disabled.
$this->DataGrid->addColumn('Active', 'User.active', array(
'type' => 'switcher',
'options' => array(
'url' => array( //The url where the switch is triggered
'action' => 'active'
),
'trailingParams' => array('User.id'), //Parameters to add to the url
'icon' => 'active' //Add the default Icon class and the active class
)
));
Datetime column
A datetime column formats a datetime or a timestamp to a specified format and optionally a locale. The format can be defined according to http://www.php.net/manual/en/function.date.php
$this->DataGrid->addColumn(
'Modified',
'User.modified',
array(
'sort' => true,
'type' => 'datetime',
'format' => '%A %e %B %Y',
'locale' => 'nl_NL.UTF8', // Optional
)
);
Column with a user defined callback function
One or more examples to show the possibilities
$this->DataGrid->addColumn(
'Example A',
array('User.first_name'),
array(
'type' => 'user_defined',
'callback' => function ($value) {
return strtoupper($value);
}
)
);
$this->DataGrid->addColumn(
'Example B',
'User.last_name',
array(
'type' => 'user_defined',
'callback' => function ($value) {
return strtoupper($value);
}
)
);
$this->DataGrid->addColumn(
'Example C',
null, // Do not supply any columns, so the complete record will be available as a parameter in the callback function
array(
'type' => 'user_defined',
'callback' => function ($data) {
return implode(' ', array_filter(array($data['User']['first_name'], $data['User']['last_name'])));
}
)
);
$this->DataGrid->addColumn(
'Example D',
array('User.first_name','User.last_name'),
array(
'type' => 'user_defined',
'callback' => function ($data) {
return implode(' ', array_filter(array($data['User.first_name'], $data['User.last_name'])));
}
)
);
Actions column
The actions column is the container for the actions you can add with addAction.
$this->DataGrid->addColumn('Actions', null, array(
'type' => 'actions'
));
/**
* @param $label
* @param $url
* @param $trailingParams
* @param $options
*/
$this->DataGrid->addAction('Delete', array(
'action' => 'delete'
), array(
'User.id'
), $options);
By setting the type option for an action to 'image' and the image option to the URL to an image file, it is possible to create image links for actions:
$this->DataGrid->addAction('Delete', array(
'action' => 'delete'
), array(
'User.id'
), array(`type` => `image`, `image` => $this->Html->url('/img/image.jpg')));
By default a link will be generated (type = 'link').
Conditional column
With the Conditional column it is possible to show a value according to 1 or more conditions.
$this->DataGrid->addColumn('Active', 'User.active', array(
'type' => 'conditional',
'conditions' => array('User.active' => '1'), //Check if User.active == 1
'true' => 'Active', //If true then print "Active"
'false' => 'Inactive' //If false then print "Active"
));
A more advanced example:
$this->DataGrid->addColumn('Active', 'User.active', array(
'type' => 'conditional',
'conditions' => array('User.usergroup' => '1'), //Check if User.usergroup == 1
'true' => array( //If true
'type' => 'string', //We want a string value
'url' => '...', //Whit a link
'rawData' => 'Set active state', //And instead of the value we want the text "Set active state"
),
'false' => '-' //If false then print "-"
));
You can see dat you can create conditions with as result a new column. So it is also possible to nest conditions. Simply use type conditional again, with true or false.
Formatted column
The Formatted column type displays one or more values in a specific string format using sprintf() formatting.
The simplest form just uses one value:
$this->DataGrid->addColumn('Name', 'User.last_name', array(
'type' => 'formatted',
'formatString' => 'My name is %s.'
));
We can also display an alternative value. The following example will display the user's account balance, while still using the last name for other functionality such as sorting:
$this->DataGrid->addColumn('Account Balance', 'User.last_name', array(
'type' => 'formatted',
'formatString' => 'Account balance: $ %.2f',
'valuePath' => 'User.account_balance'
));
Multiple values can be displayed by using multiple substitution patterns and supplying the value paths as an array:
$this->DataGrid->addColumn('Name', 'User.last_name', array(
'type' => 'formatted',
'formatString' => 'My full name is %s %s, but we sort on last name.',
'valuePath' => array('User.first_name', 'User.last_name')
));
It is possible to wrap the different formatted elements in HTML span tags for styling purposes by enabling the span option:
$this->DataGrid->addColumn('Last Name', 'User.last_name', array(
'type' => 'formatted',
'formatString' => 'My last name is %s.',
'span' => true
));
If we are called John Doe, this will result in the following output:
My last name is <span class="User_last_name">Doe</span>.
The class names can be customized by explicitly providing them in the span option as a string (1 value) or as an array (multiple values):
$this->DataGrid->addColumn('Name', 'User.last_name', array(
'type' => 'formatted',
'formatString' => 'My full name is %s %s, but we sort on last name.',
'valuePath' => array('User.first_name', 'User.last_name'),
'span' => array('first_name', 'last_name')
));
If we are called John Doe, this will result in the following output:
My full name is <span class="first_name">John</span> <span class="last_name">Doe</span>, but we sort on last name.
The span option is set to false by default.
Add multiple columns in one call
It is also possible to add multiple columns in one method call. The addColumns method can be used.
$this->DataGrid->addColumns(
array(
//Column 1
array(
'label' => 'Id',
'valuePath' => 'User.id',
'options' => array('sort' => true)
),
//Column 2
array(
'label' => 'Username',
'valuePath' => 'User.username',
'options' => array('sort' => true)
)
)
);
Default DataGrid settings
It is possible to set default settings for the DataGrid.
$this->DataGrid->defaults(array(
'ajax' => true, //Do we use AJAX for pagination, sorting and switching
'update' => '#content', //Conainer to update when we do an AJAX request
'column' => array( //Default settings for columns
'sort' => false, //Sorting on or off
'type' => 'string', //Type of the column
'htmlAttributes' => false, //Other HTML attributes
'header' => false, //Header settings
'iconClass' => 'icon', //Icon class
'indentOnThread' => false, //Indent on threaded data
'indentSize' => 2, //Indent size for nested grids
'rawData' => false //Place this data one on one inside the field instead of searching for data
'escape' => false //HTML escape retrieved data
'autoLink' => false //Automatically create hyperlinks for URLs and e-mail addresses
),
'grid' => array( //Default grid settings
'class' => 'data_grid' //Class for datagrid
),
'p
Related Skills
node-connect
351.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
