Jstree-Behavior - Unable To Create Nodes

I installed jstree-behavior using the database schema provided.

At first it looked like it was working. But, I added and organized many new nodes, and then hit "refresh" on the page, and was surprised to see that they all disappeared.

For some reason it is not saving new nodes. I noticed when I first installed it that the “Create” button didn’t work either, so I removed it (along with the “Refresh” button).

  • New nodes aren’t being saved

  • When I right-click on a new node, “Properties” and “Update” don’t do anything, but those options work fine on existing nodes.

  • Rearranging the organization of existing nodes works fine.

I uploaded a short video so that you can see what I am experiencing:

http://www.youtube.com/watch?v=wrnxVDh98u4

Help would be appreciated. Thanks.

Hi Tony,

While it is a great extension, unfortunately the extension has a number of bugs in it. I started using it over the weekend and had to work through all the code to get it to work properly. It can be done.

Part of the problem is that the errors are usually JavaScript related and if the display of the JS errors in you browser is suppressed you will not see them. I uses Chrome’s developer tools to monitor the JS and it works well.

I made a lot of changes in a number of files so it’s difficult at this stage to say exactly what you need to change. I do however remember that the creation of root nodes, and the pop-up of the JS pop-up box was a problem and that I fixed it in the _form view file. I’ve pasted my whole _form.php below. Hope it helps.


<!--

 /**

  * Form for JsTreeBehavior model.

  *

  * Date: 1/29/13

  * Time: 12:00 PM

  *

  * @author: Spiros Kabasakalis <kabasakalis@gmail.com>

  * @link http://iws.kabasakalis.gr/

  * @link http://www.reverbnation.com/spiroskabasakalis

  * @copyright Copyright &copy; Spiros Kabasakalis 2013

  * @license http://opensource.org/licenses/MIT  The MIT License (MIT)

  * @version 1.0.0

  */

  -->


<?php if ($model->isNewRecord) : ?>

<h3><?php echo Yii::t('global', 'Create') ?> <?php echo Yii::t('global', $modelClassName) ?></h3>

<?php elseif (!$model->isNewRecord): ?>

<h3><?php echo Yii::t('global', 'Update') ?> <?php echo Yii::t('global', $modelClassName) ?></h3>

<?php endif; ?>


<p> <h2><?php //echo $model->name;?></h2></p>


<?php      $val_error_msg = Yii::t('global', "Error.$modelClassName  was not saved.");

                   $val_success_message = ($model->isNewRecord) ?

                   Yii::t('global', "$modelClassName has been created successfully.") :

                    Yii::t('global', "$modelClassName  has been updated successfully.");

?>


<div id="success-note" class="alert alert-success"

     style="display:none;">

    <?php   echo $val_success_message;  ?>

</div>


<div id="error-note" class="alert alert-error"

     style="display:none;">

    <?php   echo $val_error_msg;  ?>

</div>


<div id="ajax-form" class='form'>

    <?php

    $formId = "$modelClassName-form";


    if ($model->isNewRecord){

    	if (isset($_POST['create_root'])) { 

    		if ($_POST['create_root']!='true') {

    			$actionUrl = CController::createUrl($this->id.'/createnode');

    		} else {

    			$actionUrl = CController::createUrl($this->id.'/createRoot');

    		}

    	} else {

    		$actionUrl = CController::createUrl($this->id.'/createnode');

    	}

    } else {

    	$actionUrl = CController::createUrl($this->id.'/updatenode');

    }    

    

    $form = $this->beginWidget('CActiveForm', array(

                                                   'id' => $formId,

                                                   //  'htmlOptions' => array('enctype' => 'multipart/form-data'),

                                                   'action' => $actionUrl,

                                                   // 'enableAjaxValidation'=>true,

                                                   'enableClientValidation' => true,

                                                   'focus' => array($model, 'name'),

                                                   'errorMessageCssClass' => 'alert alert-error',

                                                   'clientOptions' => array(

                                                       'validateOnSubmit' => true,

                                                       'validateOnType' => false,

                                                       'inputContainer' => '.control-group',

                                                       'errorCssClass' => 'error',

                                                       'successCssClass' => 'success',

                                                       'afterValidate' => 'js:function(form,data,hasError){$.js_afterValidate(form,data,hasError);  }',

                                                   ),

                                              ));

    ?>


    <?php

         echo $form->errorSummary($model,

                                                                     '<div style="font-weight:bold">Please correct these errors:</div>',

                                                                      NULL,

                                                                      array('class' => 'alert alert-error')

);

    ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <fieldset>




        <!--

        Modify the input fields according to your model.

        -->

        <div class="control-group">

            <?php echo $form->labelEx($model, 'name', array('class' => 'control-label')); ?>

            <div class="controls">

            	<?php if (isset($_POST['name'])) { 

                	echo $form->textField($model, 'name', array('value' => $_POST['name'], 'class' => 'span4', 'size' => 60, 'maxlength' => 128)); 

            	} else {

					echo $form->textField($model, 'name', array('class' => 'span4', 'size' => 60, 'maxlength' => 128)); 

				} ?>

				

                <?php //if ($_POST['create_root']='true')

                	//echo $form->textField($model, 'name', array('value'=>'','class' => 'span4', 'size' => 60, 'maxlength' => 128)); ?>

                	

                <p class="help-block"><?php echo $form->error($model, 'name'); ?></p>

            </div>

        </div>


        <div class="control-group">

            <?php echo $form->labelEx($model, 'description', array('class' => 'control-label')); ?>

            <div class="controls">

                <?php echo $form->textArea($model, 'description', array('class' => 'span4', 'rows' => 5, 'cols' => 50)); ?>

                <p class="help-block"><?php echo $form->error($model, 'description'); ?></p>

            </div>

        </div>


        <input type="hidden" name="YII_CSRF_TOKEN"

               value="<?php echo Yii::app()->request->csrfToken; ?>"/>

        

        <?php  if (isset($_POST['parent_id'])): ?>

	        <input type="hidden" name= "parent_id" 

	        value="<?php echo $_POST['parent_id']; ?>"/> 

        <?php endif; ?>

	                

        <?php  if (!$model->isNewRecord): ?>

        <input type="hidden" name="update_id"

               value=" <?php echo $model->id; ?>"/>

        <?php endif; ?>

        <div class="control-group">

            <?php   echo CHtml::submitButton($model->isNewRecord ? Yii::t('global', 'Submit')

                                                     : Yii::t('global', 'Save'),

                                             array('class' => 'btn btn-large pull-right')); ?>

        </div>

</fieldset>

        <?php  $this->endWidget(); ?>

</div>

<!-- form -->

Thanks for the reply. I tried out your code.

First, here is what happened before (with firebug console):

http://www.youtube.com/watch?v=wrnxVDh98u4

Then, here is what happens with your code:

http://www.youtube.com/watch?v=6VMLgrDlr2I

As you can see, “Create Root” works now, but creating a new node via right-click still doesn’t work. But, at least I can workaround by creating new nodes as roots, and then moving them where I want them.

Hi,

First of all, I want to thank Spiros for his excellent work. I’ve tried a number of tree extensions for Yii and this is by far the best one out there. Unfortunately it contained a number of bugs, both in the core files as well as in the example that was included. I worked through the code for a number of days and because of the excellent comments in the code, I could eventually get a very good understanding of the extension. I also learned a huge amount about Yii in the process. Thanks again Spiros.

I saw on the forums that a number of people were struggling to use JsTreeBehavior. Since I had already spent a lot of time sorting out the issues I thought I’d share my efforts.

First a disclaimer: I do not claim any credit for this great extension. All credit belongs to Spiros. I do not support this code. I do not claim that it is bug free or that it will work out of the box. It works in my environment and I know of at least one more bug (cutting a node with its children and pasting it for example), but it should get you a long way closer in getting it working.

I’ve also extended it for my purposes. I needed to be able to represent a single root node in the tree while I have multiple root nodes in my dataset. To accomplish this I added two new properties to JSTreeWidget. Use of this can be seen in the tree.php file where I’ve commented out the lines but left them on the Widget call to demonstrate their use.

  1. node_ID - this is the id of the top level node that you want to show in the tree. Ex: ‘node_ID’ => ‘111’,

  2. allow_drag_to_root - set to true to allow the dragging of nodes to a root position. false to prevent nodes to be dragged to a root position. Ex: ‘allow_drag_to_root’ => false

I do not have the time to fork the code on Github but if anybody else want to do so, feel free. I am however willing to discuss ideas to better the extension if it coincides with my own needs.

I attached the code with my changes to this reply. I kept the structure in the ZIP file exactly the same as that of the ZIP file on the Yii extension page so the original installation instructions still applies. I hope I didn’t miss any files.

4685

JsTree-Behaviour.zip

Gerhard

Gerhard: Thanks! Your version seems to be working perfectly.

[color=#222222][font=arial, sans-serif][size=2]OK so I took the time to see what’s wrong.[/size][/font][/color][color=#222222][font=arial, sans-serif][size=2]Open the network tab in your Chrome console ,see that the returnForm ajax request fails,click on it,switch to preview tab and see what the PHP error is.Simple as that.[/size][/font][/color][color=#222222][font=arial, sans-serif][size=2]With the most strict (E_ALL | E_STRICT) error reporting settings used in development seems like POST variables need to be defined before they participate in boolean (and I guess any other) operations. $_POST[‘create_root’]!=‘true’ will throw an error.So we must check with isset() before we use that variable.[/size][/font][/color][color=#222222][font=arial, sans-serif][size=2]Turns out I was not developing with E_ALL | E_STRICT,I admit it , my bad.If I remember well,I coded this extension online with (PHPStorm can do that),and the free hosting company obviously does not use E_ALL | E_STRICT .My “buggy” code works perfect on my live demo,for this reason it’s not fair to call my code buggy,if you upload it to a server it will work.In production less strict error reporting is used.This is just a coding standard error.[/size][/font][/color][color=#222222][font=arial, sans-serif][size=2] Suggestion,take some time to review your debugging tools,it will save you from hours of hair pulling and forum posting.[/size][/font][/color][color=#222222][font=arial, sans-serif][size=2]I changed the code on GitHub it’s my version, I did’nt use Gerhardvr’s code,as he is more verbose using complex conditionals,while I think ternary operator is more compact.[/size][/font][/color][color=#222222][font=arial, sans-serif][size=2]Cheers everybody.[/size][/font][/color][color=#222222][font=arial, sans-serif][size=2]

[/size][/font][/color][color=#222222][font=arial, sans-serif][size=2]https://github.com/drumaddict/yii-jstree-behavior/blob/master/example/views/category/_form.php

[/size][/font][/color]

Hi Spiros,

Just wanted to drop you a note to thank you for an excellent extension. My apologies if I stepped on toes, it was not the intention. I retract my statement calling your code “buggy”. Clearly it is my inexperience in using Yii that led me to this conclusion. :blink:

As I’ve mentioned before, I learned a great deal from working through your code. The beauty of using Yii is of course that there is always someone else that already did the hard work saving the rest of us a lot of time. Though I customized your code to suite my needs, it was the excellent comments and easy to follow coding structure that allowed me to do this.

Gerhard

Gerhardvr,thanks for appreciating my extension.You do not need to apologize for anything.You debugged the coding standard error,and shared your solution,plus customized the extension for yourself.Ideal Yii developer attitude.I just cant understand people who send tons of emails in my inbox ,before they try the most basic debugging workflow in a browser’s javascript console.No hard feelings for anyone,Cheers!

I did all according to the instructions but it looks like I missed something.

I always get error:

POST http://localhost/Yii-jstree-behavior/category/fetchTree

404 Not Found

I think something is wrong with behaviors of CategoryController!?

Can anyone help me somehow?