2017 © Pedro PelĆ”ez
 

library jqgrider

Generating jqGrid from PHP

image

djuki/jqgrider

Generating jqGrid from PHP

  • Sunday, September 16, 2012
  • by Djuki
  • Repository
  • 4 Watchers
  • 1 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

jqGrider

Introduction

jqGrider is PHP library for generating jqGrid without typing JavaScript code. I created jqGrider for admin part of my applications. I wanted to without much effort present my database tables and datas into ajax grid., (*1)

jqGrid is at this moment most complete full-stack javascript grid. At this moment jqGrider supotring only basic and most needed functionality of jqGrid., (*2)

With jqGrider you can:, (*3)

  • Present data from database table
  • Present array data in grid
  • Search grid data with any operator
  • Refresh data grid
  • Paginate results
  • Add custon anonymous function for columns

Code examples

basic usage of jgGrider

Your main action presenting grid, (*4)

public function action_grid()
{
    $view = $this->app->forge('View', 'grid');


    $grid = new \JqGrider\Grid(\JqGrider\Grid::DATA_TYPE_JSON);
    $grid
    ->addColumn('ID', 'id', 200)
    ->addColumn('Full Name', 'name', 450)
    ->setUrl('http://localhost/fuelphp2/public/index.php/welcome/grid_data');

    $view->set('grid_js', $grid->getJavaScriptCode(), false);

    return $view;
}

Your view with grid, (*5)

<?=$grid_js?>

Ajax action which will return grid data with ajax call, (*6)

public function action_grid_data()
{

    $grid = new \JqGrider\Grid(\JqGrider\Grid::DATA_TYPE_JSON);
    $grid
    ->setRepository(new \App\Model\User())
    ->addColumn('ID', 'id', 200)
    ->addColumn('Full Name', 'name', 450);

    $grid->printRespositoryData();
}

User model need to implement IGridRepository and define two methods getData and countDataRows., (*7)

getData can return object with data, and it must have properties like column names. Also it can returna array and key names also must be named as column names., (*8)

countDataRows will count data rows considering conditions., (*9)

Object Conditions representing search query, and it wil help you to generate database query to calculate rows in search conditions., (*10)

namespace App\Model;

use JqGrider\Data\IGridRepository;
use JqGrider\Data\Conditions;

class User implements IGridRepository
{
protected $pdo;

public function __construct()
{   
    // Connect on database with pdo object for example
}

public function getData(Conditions $dataConditions)
{
    $offset = $dataConditions->page == 1 ? 0 : $dataConditions->rowsLimit * ($dataConditions->page - 1);
    $sql = 'SELECT id, name FROM user ';

    if ($dataConditions->search === 'true')
    {
        $sql .= ' WHERE '.$dataConditions->searchCondition;
    }

    $sql .=' ORDER BY '.$dataConditions->sortBy.' '.$dataConditions->sort;

    $sql .= ' LIMIT '.$dataConditions->rowsLimit.' OFFSET '.$offset;


    $statement = $this->pdo->prepare($sql, array(\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY));
    @$statement->bindParam(':grid_search_string',$dataConditions->searchString);

    if ($statement->execute())
    {
        return $statement->fetchAll(\PDO::FETCH_CLASS);
    }
}

public function countDataRows(Conditions $dataConditions)
{
    $sql = 'SELECT count(id) FROM user';
    if ($dataConditions->search === 'true')
    {
        $sql .= ' WHERE '.$dataConditions->searchCondition;
    }       
    if ($result = $this->pdo->query($sql))
    {
        return $result->fetchColumn();
    }       
}
}

Using anonymous functions

Anonymous function can be added into Column object to change column content., (*11)

        $nameFunction = function ($content)
        {
            return 'Member name is: '.$content;
        };

        $grid = new Grid(Grid::DATA_TYPE_JSON);
        $grid->addColumn('Full Name', 'name', 450, $nameFunction);

Using local data for grid

public function action_grid()
{
    $view = $this->app->forge('View', 'grid');


    $grid = new Grid(Grid::DATA_TYPE_JSON);
    $grid
    ->addColumn('ID', 'id', 200)
    ->addColumn('Full Name', 'name', 450);

    $grid->setLocalData(array(
        array('id' => 4, 'name' => 'William Harrison'),
        array('id' => 5, 'name' => 'Erick Hawkins'),
        array('id' => 6, 'name' => 'Gene Autry'),
    ));

    $view->set('grid_js', $grid->getJavaScriptCode(), false);
    return $view;
}

The Versions

16/09 2012

dev-master

9999999-dev https://github.com/Djuki/jqgrider

Generating jqGrid from PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

grid jqgrid

14/09 2012

1.0.0

1.0.0.0 https://github.com/Djuki/jqgrider

Generating jqGrid from PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

grid jqgrid