mgallegos / laravel-jqgrid

@Laravel jqGrid package allows you to easily integrate the popular @jQuery Grid Plugin (jqGrid) into your Laravel application.
mariogallegos.com
Other
69 stars 44 forks source link

Example of TreeGrid #43

Closed KrasilnikovKB closed 8 years ago

KrasilnikovKB commented 8 years ago

I would like to see a small example of TreeGrid

mgallegos commented 8 years ago

Please take a look at issue https://github.com/mgallegos/laravel-jqgrid/issues/31

KrasilnikovKB commented 8 years ago

page not found :( https://github.com/mgallegos/decima-erp/blob/master/workbench/mgallegos/decima-accounting/src/views/account-management.blade.php

mgallegos commented 8 years ago

Sorry, try:

https://github.com/mgallegos/decima-accounting/blob/master/src/views/account-management.blade.php

KrasilnikovKB commented 8 years ago

I tried for a long time, but not get the correct result (

if i creating my own implementation of repository class, something like this :

namespace App\Grids;

use Mgallegos\LaravelJqgrid\Repositories\RepositoryInterface;

class ExampleRepository implements RepositoryInterface {

  public function getTotalNumberOfRows(array $filters = array())
  {
      return 5;
  }

 public function getRows($limit, $offset, $orderBy = null, $sord = null, array $filters = array(), $nodeId = null, $nodeLevel = null, $exporting = null )
    {

        return array(
            array("id" => 1, "parent_id" => 0, "describe_tree" => "1",   "payloads" => "some helpful information 1"),
            array("id" => 2, "parent_id" => 0, "describe_tree" => "2",   "payloads" => "some helpful information 2"),
            array("id" => 3, "parent_id" => 1, "describe_tree" => "1-1", "payloads" => "some helpful information 3"),
            array("id" => 4, "parent_id" => 2, "describe_tree" => "2-1", "payloads" => "some helpful information 4"),
            array("id" => 5, "parent_id" => 2, "describe_tree" => "2-2", "payloads" => "some helpful information 5"),
        );

    }

 /* 
    !!!!!!!!!!!!!!!!!!!!
 it is necessary to describe here ?
        treeGrid = true;
        parentColumn = 'id';
        eafColumn = 'parent_id';

 how ?
    ????????????????????
*/
}

and creating view some like this:


/* is required to include some external Javascript ? */

GridRender::setGridId("test_tree")

->setGridOption('treeGrid', true)
->setGridOption('ExpandColumn', 'describe_tree')

->setGridOption('treeReader', array('parent_id_field' => 'id', 'leaf_field' => 'parent_id'))

->addColumn(array('index'=>'id',        'name' => 'id',        'hidden' => true ))
->addColumn(array('index'=>'parent_id', 'name' => 'parent_id', 'hidden' => true ) )

->addColumn(array('label'=>'describe_tree','index'=>'describe_tree', 'width' => 150 ) )
->addColumn(array('label'=>'payloads',     'index'=>'payloads',      'width' => 150 ) )

I expect to see something like this:

1       some helpful information 1
 +1-1   some helpful information 3
2       some helpful information 2
 +2-1   some helpful information 4
 +2-2   some helpful information 5

but I get a flat table (((

1       some helpful information 1
2       some helpful information 2
1-1     some helpful information 3
2-1     some helpful information 4
2-2     some helpful information 5

where is error ?

mgallegos commented 8 years ago

ok, first the constructor of your repository class:

public function __construct()
    {
        $this->treeGrid = true;
        $this->parentColumn = 'parent_id';
        $this->leafColumn = 'is_leaf';
    }

And in the getRows function add the leaf column (this is how the grid knows when you can extend the row, 0 to extend, 1 not to extend):

 public function getRows($limit, $offset, $orderBy = null, $sord = null, array $filters = array(), $nodeId = null, $nodeLevel = null, $exporting = null )
    {

        return array(
            array("id" => 1, "parent_id" => 0, "describe_tree" => "1",   "payloads" => "some helpful information 1", "is_leaf" => 0),
            array("id" => 2, "parent_id" => 0, "describe_tree" => "2",   "payloads" => "some helpful information 2", "is_leaf" => 0),
            array("id" => 3, "parent_id" => 1, "describe_tree" => "1-1", "payloads" => "some helpful information 3", "is_leaf" => 1),
            array("id" => 4, "parent_id" => 2, "describe_tree" => "2-1", "payloads" => "some helpful information 4", "is_leaf" => 1),
            array("id" => 5, "parent_id" => 2, "describe_tree" => "2-2", "payloads" => "some helpful information 5", "is_leaf" => 1),
        );

    }

Finally in the view, set the tree options correctly:

->setGridOption('treeGrid', true)
->setGridOption('ExpandColumn', 'describe_tree')
->setGridOption('treeReader', array('parent_id_field' => 'parent_id', 'leaf_field' => 'is_leaf'))

Let me know if it works!

mgallegos commented 8 years ago

And one more thing, try creating and example table in your database, because in this case your repository class will always be returning the same five rows instead of the childs of an specific parent.

KrasilnikovKB commented 8 years ago

not workin ( The result :

image

  1. I think that it is generally unnecessary :
public function __construct()
    {
        $this->treeGrid = true;
        $this->parentColumn = 'parent_id';
        $this->leafColumn = 'is_leaf';
    }

because they generally are not declared in the RepositoryInterface. they are only used when json-answer preparing in EloquentRepositoryAbstract

  1. to display properly is really important only what jqGrid-library included on page, and what data is returned to the table from server.

can you show the correct json-response from server, in which is displayed correctly ?

mgallegos commented 8 years ago

The json looks good, are you sure you changed the treeReader property:

->setGridOption('treeReader', array('parent_id_field' => 'parent_id', 'leaf_field' => 'is_leaf'))
mgallegos commented 8 years ago

You are right, the field level is missing, this is automatically generated by the EloquentRepositoryAbstract class:

 public function getRows($limit, $offset, $orderBy = null, $sord = null, array $filters = array(), $nodeId = null, $nodeLevel = null, $exporting = null )
    {

        return array(
            array("id" => 1, "parent_id" => 0, "describe_tree" => "1",   "payloads" => "some helpful information 1", "is_leaf" => 0, "level" => 0 ),
            array("id" => 2, "parent_id" => 0, "describe_tree" => "2",   "payloads" => "some helpful information 2", "is_leaf" => 0, "level" => 0 ),
    }
KrasilnikovKB commented 8 years ago

I too found the missing "level"-field at this good example

my final correct data:

->setGridOption('treeGrid', true)
->setGridOption('ExpandColumn', 'describe_tree')
->setGridOption('treeReader', array('parent_id_field' => 'parent_id', 'leaf_field' => 'is_leaf'))

->addColumn(array('index'=>'id',        'name' => 'id',        'hidden' => true, 'key' => true ))
->addColumn(array('label'=>'describe_tree','index'=>'describe_tree', 'width' => 50 ) )
->addColumn(array('label'=>'payloads',     'index'=>'payloads',      'width' => 50 ) )
return array(
array("level" => 0,  "id" => 1, "parent_id" => "",  "describe_tree" => "1",   "payloads" => "some helpful information 1", "is_leaf" => false),
array("level" => 1,  "id" => 2, "parent_id" => "1", "describe_tree" => "1-1", "payloads" => "some helpful information 3", "is_leaf" => true),
array("level" => 0,  "id" => 3, "parent_id" => "",  "describe_tree" => "2",   "payloads" => "some helpful information 2", "is_leaf" => false),
array("level" => 1,  "id" => 4, "parent_id" => "3", "describe_tree" => "2-1", "payloads" => "some helpful information 4", "is_leaf" => true),
array("level" => 1,  "id" => 5, "parent_id" => "3", "describe_tree" => "2-2", "payloads" => "some helpful information 5", "is_leaf" => true),
        );

in my experience:

Thank for help! Now all works fine.

Please, look at my pool requests ;)

mgallegos commented 8 years ago

Nice, I'm glad it worked!

P.D: Yes, I will take a look at them as soon as I can.

mgallegos commented 8 years ago

And when you do use the EloquentRepositoryAbstract, this code is necessary:

public function __construct()
    {
        $this->treeGrid = true;
        $this->parentColumn = 'parent_id';
        $this->leafColumn = 'is_leaf';
    }