Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tomshaw/zf2-simplegrid

SimpleGrid is a Zend Framework 2 controller plugin that makes it extremely easy and flexible to setup a custom grid in your application.
https://github.com/tomshaw/zf2-simplegrid

Last synced: 11 days ago
JSON representation

SimpleGrid is a Zend Framework 2 controller plugin that makes it extremely easy and flexible to setup a custom grid in your application.

Awesome Lists containing this project

README

        

# SimpleGrid - Zend Framework 2 Grid Controller Plugin

SimpleGrid is a Zend Framework 2 controller plugin that makes it extremely easy and flexible to setup a custom grid in your application. SimpleGrid doesn't create a grid for you it just makes it much easier to develop one. SimpleGrid uses sessions and unique namespaces to remember sorting and search criteria and to uniquely distinguish grids developed on your system. Any input or pull requests would be greatly appreciated.

## Installation

Simply enable the module inside your application.config.php

return array(
'modules' => array(
'Application',
'SimpleGrid'
),

## Implementation Notes

Implementation of the module is quite simple.

Create a route that your grid uses.

'cart-user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin/user[/order/:order/sort/:sort/page/:page]',
'constraints' => array(
'page' => '[0-9]+',
'sort' => '[a-zA-Z][a-zA-Z0-9_-]*',
'order' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Admin\Controller\User',
'action' => 'index',
'order' => 'id',
'sort' => 'asc',
'page' => 1
)
)
)

Implement the helper in your action controller. Please note the first and only grid parameter should be unique and not clash with other grids on your system. Below I'm using an Admin module, User controller and Index action. Namespaces do not have to follow this convention but should be unique none the less.

class UserController extends AbstractActionController
{
protected $userTable;

public function getUserTable()
{
if (!$this->userTable) {
$sm = $this->getServiceLocator();
$this->userTable = $sm->get('Application\Model\UserTable');
}
return $this->userTable;
}

public function indexAction()
{
$grid = $this->grid('Admin\User\Index');

$select = $this->getUserTable()->fetchAll($grid);

$adapter = new DbSelect($select, $this->getUserTable()->getAdapter());

$paginator = new Paginator($adapter);

$paginator->setCurrentPageNumber($grid['page']);

$paginator->setItemCountPerPage(20);

return new ViewModel(array_merge($grid, array('paginator'=>$paginator)));
}
}

Create a method in your model that utilizes the grid array.

public function fetchAll($data = array())
{
$sql = new Sql($this->tableGateway->getAdapter());

$select = $sql->select()
->from(array('user' => 'user'))
->columns(array('*'));

if (isset($data['name']) && !empty($data['name'])) {
$select->where->like('user.name', '%'.strtolower($data['name']).'%');
}

if (isset($data['newsletter']) && !empty($data['newsletter']) && $data['newsletter'] != '-1') {
$select->where->equalTo('user.newsletter', intval($data['newsletter']-1));
}

$select->order(array($data['order'] . ' ' . strtoupper($data['sort'])));

return $select;
}

Code your template like you normally would.


paginationControl($this->paginator, 'Jumping', 'pagination', array('route' => 'cart-user')); ?>


Filter Results



sort == 'desc') ? 'asc' : 'desc' ?>




ID
Customer Name
Email
Address
Phone
Newsletter











''), array('0'=>'Not Subscribed','1'=>'Subscribed')) as $key => $value): ?>
newsletter == $key) ? 'selected="selected"': '' ?>
>




paginator)): ?>
paginator as $row): ?>

escapeHtml($row['id']) ?>
escapeHtml($row['name']); ?>
escapeHtml($row['email']) ?>
escapeHtml($row['address1']) ?> escapeHtml($row['address2']) ?> escapeHtml($row['city']) ?> escapeHtml($row['state']) ?> escapeHtml($row['zipcode']) ?>
escapeHtml($row['phone']) ?>
SubscribedNot Subscribed







$('#grid-reset').click(function(e) {
$(this).closest('form').find('input[type=text], select').val('').submit();
});
$("input#created").datepicker({dateFormat: 'yy-mm-dd'});
$("input#updated").datepicker({dateFormat: 'yy-mm-dd'});

## Styling Tips

To get your inputs to play nicely inside the table cells, you need to tweak the margins a little. It's also a good idea to prevent those cells from wrapping.

table tbody tr td input {
margin: 2px 2px;
width: 90%;
}

table input[type="text"] {
margin-bottom: 1px;
}

table thead tr a {
margin-left: 2px;
}

tr.grid-inputs td {
white-space: nowrap;
text-align: left;
vertical-align: top;
}

## License

#### (The MIT License)

Copyright © 2013 [[email protected]](mailto:[email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.