PHP code example of cscfa / datagrid-bundle

1. Go to this page and download the library: Download cscfa/datagrid-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cscfa / datagrid-bundle example snippets


// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            [...]
            new Cscfa\Bundle\DataGridBundle\CscfaDataGridBundle(),
        );
        
        [...]
    }
}

// in php file
use Cscfa\Bundle\DataGridBundle\Objects\DataGridContainer;

	// Asume this code is into a controller
	
	$datas = array(array("element 1.1", "element 1.2"), array("element 2.1", "element 2.2"));
	
	$dataGrid = new DataGridContainer($datas, array(0, 1), array("head1", "head2"), DataGridContainer::TYPE_ARRAY);
	
	$this->render("AcmeBundle:Default:index.html.twig", array("data"=>$dataGrid));

	// Asume this code is into a controller
	$dataGrid = new DataGridContainer();
	
	$this->render("AcmeBundle:Default:index.html.twig", array("data"=>$dataGrid));

	// Asume this code is into a controller
	/*
	 * Note we use here the result of a doctrine request
	 * And we specify the the type is object.
	 */
    $manager = $this->getDoctrine()->getManager();
    $repository = $manager->getRepository("Acme\Bundle\AcmeBundle\Entity\Miscellaneous");
    $miscs = $repository->findAll();
            
	$dataGrid = new DataGridContainer();
	
	$dataGrid->setContainer($miscs);
	$dataGrid->setAccessMethods("getName", "getId");
	$dataGrid->setHeader("name", "identity");
	$dataGrid->setType(DataGridContainer::TYPE_OBJECT);
	
	$this->render("AcmeBundle:Default:index.html.twig", array("data"=>$dataGrid));

    //This access to $miscs->getBag()->getName()
    $dataGrid->setContainer($miscs);
    $dataGrid->setAccessMethods("getBag.getName");

// in php file
use Cscfa\Bundle\DataGridBundle\Objects\DataGridStepper;

	// in php file
	$dataGrid = new DataGridContainer();
    $dataGrid->setStepper(new DataGridStepper());

	// in php file
	$dataGrid = new DataGridContainer();
    $dataGrid->setStepper(new DataGridStepper());
    
    // This one display the header before each values
    $dataGrid->getStepper()->addCallback("onElementPrepend", function($type, $process, $row, $data){
        return $data['header']." : ";
    });
    
    // This one display a title before the datagrid
    $dataGrid->getStepper()->addCallback("onGridStart", function($type, $process, $row, $data){
        return "<h3>See our awesome datagrid : </h3>";
    }, true);
    
    // This one set the style of the header at 'color: red'
    $dataGrid->getStepper()->addCallback("onHead", function($type, $process, $row, $data){
        return "style='color: ".$data["color"].";'";
    }, false, array("color"=>"red"));

	// In a php context
	$datas = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

	/* 
     * Instanciate with arguments
     * 
     * In this example, we instanciate the paginator
     * with a 10 index array, on page 2, with 4 data
     * per page. 
     */
    $paginator = new DataGridPaginator($datas, 2, 4);

	//In a php context
    
    /*
     * Note that this example render the
     * same result as the previous example.
     */
    $paginator = new DataGridPaginator();
    $paginator->setPage(2)
    	->setLimit(4)
        ->setData(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

	//In a php context
	$datas = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    $paginator = new DataGridPaginator($datas, 2, 4);
    
    $data = new DataGridContainer($paginator->getPageData());

//in your controller
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Cscfa\Bundle\DataGridBundle\Objects\PaginatorLimitForm;

class AcmeController extends Controller
{
	public function limitAction(Request $request)
	{
		$paginatorLimitForm = new PaginatorLimitForm();
		$paginatorLimitForm->setAllowedLimits(array(5, 10, 25, 50, 100));
		
        $limitForm = $this->createForm("paginatorLimit", $paginatorLimitForm);
        
        if ($request->getMethod() === "POST") {
            $limitForm->handleRequest($request);
            
            $choice = $paginatorLimitForm->getLimit();
            $value = $paginatorLimitForm->getAllowedLimits()[$choice];
            
            $lastLimit = $paginatorLimitForm->getLastLimit();
            $page = $paginatorLimitForm->getPage();
            
        	// render the template
            
        } else {
        	// render the template
        }
	}
}
twig
	{# in your twig template #}
	
	{{ renderPaginator(pager, null, 5) }}