EN | CS | Přihlásit | Registrovat

CBTree

Nadstavba TreeView pre vykreslenie CheckBoxTree kompatibilneho s http://www.redcarrot.co.uk/…uery-plugin/

Download http://github.com/Lopo/treeview
Forum thread http://forum.nette.org/…xlist-cbtree
Demo http://nette.losys.sk/…ew/c-b-tree/
Autor Lopo (Pavol Hluchy)

poziadavka bola moznost urcovania viditelnosti poloziet v stromovej strukture

K pouzitiu je potrebny jQuery plugin Collapsible Checkbox Tree jQuery Plugin

TreeView sluzi ako datova struktura

pouzitie: treba zaregistrovat rozsirenie formu (v bootstrape a pod.):

Form::extensionMethod('Form::addCBTree', array('CBTree', 'addCBTree'));

a v tovarnicke potom uz len

$tree=new TreeView;
$tree->addLink(null, 'name', 'id', true, $this->presenter);
$tree->dataSource=$this->model->findAllCTree();
$form->addCBTree('ctree', 'filter', $tree);

pripadne doplnit nazov stlpca podla ktoreho sa riadia checkboxy (defaultne visible):

$form['ctree']->checkColumn='visible';

v DB potom treba samozrejme v tabulke doplnit bool stlpec visible a v sablone linknut prislusny jQuery plugin

v odoslanom formulari sa potom cela struktura objavi ako jednorozmerne pole, v ktorom hodnoty su id checknutych boxov

vysledny form prvok vyzera zhruba takto:

samotna nadstavba:

<?php
/**
* @author Pavol Hluchy
* @version 0.1.0
*/

class CBTree
extends FormControl
{
/** @var Nette\Web\Html container element template */
protected $container;
/** @var TreeView data */
protected $tree;
/** @var string */
public $checkColumn='visible';

/**
* @param string label
* @param TreeView options from which to choose
*/

public function __construct($label, TreeView $tree)
{
parent::__construct($label);
$this->control->type='checkbox';
$this->container=Html::el();
if ($tree!==NULL) $this->setItems($tree);
}

/**
* Returns selected checkbox value.
* @param bool
* @return mixed
*/

public function getValue($raw=FALSE)
{
return is_array($this->value)? $this->value : NULL;
}

/**
* Form container extension method. Do not call directly.
* @param Form
* @param string name
* @param string label
* @param array items
* @return CBTree
*/

public static function addCBTree(Form $form, $name, $label, $tree)
{
return $form[$name]=new self($label, $tree);
}

/**
* Sets options from which to choose.
* @param TreeView
* @return CBTree provides a fluent interface
*/

public function setItems(TreeView $tree)
{
$this->tree=$tree;
return $this;
}
/**
* Returns container HTML element template.
* @return Nette\Web\Html
*/

final public function getContainerPrototype()
{
return $this->container;
}

/**
* Generates control's HTML element.
* @param mixed
* @return Nette\Web\Html
*/

public function getControl($key=NULL)
{
if ($key===NULL)
$container=clone $this->container;
elseif (!isset($this->tree[$key]))
return NULL;
$control=parent::getControl();
$control->name.='[]';
$id=$control->id;
$values=$this->value===NULL? NULL : (array)$this->getValue();
$label=Html::el('label');
$x=Html::el('ul', array('id'=>'example'));
foreach ($this->tree->getNodes() as $node) {
$x->add($this->renderNode($node, $control, $label));
}
$container->add($x);
return $container;
}

/**
* Generates label's HTML element.
* @return void
*/

public function getLabel($caption=NULL)
{
$label=parent::getLabel($caption);
$label->for=NULL;
return $label;
}

public function renderNode(TreeViewNode $node, $control, $label)
{
$pcontrol=clone $control;
$li=Html::el('li');
$nid=$node->getDataRow()->id;
$control->id=$label->for=$control->id.'-'.$nid;
$ck=$this->checkColumn;
if ($node->getDataRow()->$ck)
$control->checked='checked';
else $control->checked=null;
$control->value=$nid;
$label->setText($node->getDataRow()->name);
$li->add((string)$control.(string)$label);
$nodes=$node->getNodes();
if (count($nodes)) {
$u=Html::el('ul');
$li->add($u);
foreach ($nodes as $n)
$u->add($this->renderNode($n, $pcontrol, $label));
}
return $li;
}
}
historia verzii:
0.1.0 prvy verejny rls, uverejnene http://forum.nette.org/…xlist-cbtree
0.2.0 doplnene automaticke generovanie prepajacieho JS a id kontajneroveho UL
0.2.1 upravene nazvy premennych, zmena renderNode() na private, vyhodeny zbytocny riadok v getControl()
0.2.2 doplnena moznost prenastavit defaultne hodnoty parametrov jQuery pluginu checkParents,chec­kChildren,unchec­kChildren,ini­tialState

Login to submit a comment