Discussion and Questions on EJuiAutoCompleteFkField

Hi Jeremy,

I checked the response from firebug, nothing is called there… is there because of the indexing? what I suppose to do in a situation where the _formDetail is called by a javascript function?

I can manage to make it work on the other (normal) case.

Cheers,

Daniel

If there’s no ajax GET, then the js function is not attached properly. you’re using renderPartial ( x, x, x, TRUE) which is correct. Have you ever used VisualEvent ? (http://www.sprymedia.co.uk/article/Visual+Event) Install, then check a page where the autoCompleter is working properly. You should see the js event attached to the correct widget. In my working application, each EJuiAutoCompleteFkField widget has 6 events subscribed. Hover your cursor over each to see the js function for the event. Now, go to the non-working page. Use Visual Event again - are there any attached events?

Hi, Jeremy,

Thanks for quick reply.

I installed Visual Event, and right on my working page, I saw 6 events. On my non working page, I can only see 1 event which is the onBlur…I guess this is why it is not working. however, any hints to fix this? something related to the attributes?

Moreover, since I can add the new row from javascript, no new jquery autocomplete is genereated when arow is added. Any suggestion to this problem?

I remember that we should be able to attach a jquery script to a class not just an id, but no idea how could this be working in my situation…

Cheers,

Daniel

the problem is with


'attribute' => '[$id]warehouseFk', 

the code is currently expecting the "attribute" to be a valid attribute of the model.

On the other hand, it’s also using “[xx]warehouseFk” to match the element “id” and “name” generated by Yii; otherwise I’d suggest passing just “warehouseFk”.

Please try modifying the extension as follows:

in init(), AFTER:




    	$this->_fieldName = get_class($this->model).'_'.$this->attribute;  // match what is generated by Yii

    	$this->_saveName = $this->attribute.'_save';

    	$this->_lookupName = $this->attribute.'_lookup';



add




// handle autoComplete rendered in child rows.  strip [xx] from the attribute to make it a valid attribute of $model

if (strpos($this->attribute, ']') {

   $parts = explode($this->attribute, ']');

   $this->attribute = $parts[1];

}



and see what happens. If it works I’ll patch the extension and release a new version.

  • Jeremy

Hi Jeremy,

I tried to add this part of code into your extension




if (strpos($this->attribute, ']')) {

   $parts = explode($this->attribute, ']');

   $this->attribute = $parts[1];

}



but still not working, it said no field "TransactionDetail." somehow it seems the $parts[1] is empty ;( I tried to print but could not see it anywhere, any idea to debug it?

Cheers,

Daniel

OK - here is the fix:

  1. when you create the widget, use double-quotes around the field-name

 'attribute' => "[$id]warehouseFk", 

otherwise it renders a literal "[$i]" instead of substituting the variable

  1. remove the code I previously suggested

  2. make this change in EJuiAutoCompleteFkField.init()




    	// JJD 8/3/11 make EJuiAutoCompleteFkField work for child rows where attribute like [$i]FieldName

    	//$this->_fieldName = get_class($this->model).'_'.$this->attribute;  // match what is generated by Yii

    	$this->_fieldName = CHtml::resolveName($this->model, $this->attribute); 



Let me know if it works for you. I tried in a view where I have child-rows, but the auto-completer was borrowed from another place in the app so I’m not 100% sure. But it did seem to work properly.

I’ll release a new version with this patch if it works ok. Thanks for your patience.

  • Jeremy

Hi Jeremy,

I follow your instruction, it did work, however, only for the first row, next rows generated by javascript, would not display the autocomplete.

Other problem is when submitting, it is error…it seemed not saving the fk …

I attached my code, if you want to play with…

use warem / warem to login.

Thanks a lot for your support. Nice extension!

Cheers,

Daniel

Hi Jeremy,

More info, your last fix will make the other page could not be submitted. It worked before, but becoming error on submit while I applied last fix.

Cheers,

Daniel

Daniel,

See attached code. I think this version is working properly for [$i]fieldName, as well as for fieldName. If working, I’ll publish the new version on the Extension homepage. Thanks for your patience. You’re helping to improve the extension.

  • Jeremy

Hi Jeremy,

Thanks a lot for your support. I tried on my code, and it works only for the first row. I wonder why it failed on the next row. Either, I generate from (+) button which is a javascript, or from the yii, prelaoded the create form with 3 details.

I don’t know whether this info is useful, if I compare the textfield next to it is product, the id would be TransactionDetail_x_productFk while on your extension it is only TransactionDetail_warehouseFk. Since all id would be TransactionDetail_warehouseFk, it would be difficult to distinguish which one is belong to which id.

I am really thank you for your patience and kindly support to answer my questions.

Cheers,

Daniel

Yes, the whole problem is getting the IDs to be correct, and unique.

Are you sure it doesn’t work for the existing rows? I have it working properly in my application for 20+ child rows, with valid autocompleters on each. But in my app, new rows cannot be created.

The problem with creating new rows is the autocompleter never gets attached to the field. This happens in anonymous js function that is executed after the page loads. Look at the very bottom of the HTML source and you’ll see it., like this:




jQuery('#FIELDNAME').autocomplete({...



When you add a row using js, does it create another jQuery command like the one above? Please try and post the result here.

Hi Jeremy,

The screenshot is attached.

1930

pic1.png

Here is my _formDDetail




<tr>    

    <td> 

        <?php

        $this->widget('EJuiAutoCompleteFkField', array(

            'model' => $model,

            'attribute' => "[$i]warehouseFk", //the FK field (from CJuiInputWidget)

            // controller method to return the autoComplete data (from CJuiAutoComplete)

            'sourceUrl' => Yii::app()->createUrl('/transaction/findWarehouse'),

            // defaults to false.  set 'true' to display the FK field with 'readonly' attribute.

//            'showFKField' => true,

            // display size of the FK field.  only matters if not hidden.  defaults to 10

//            'FKFieldSize' => 15,

            'relName' => 'warehouse', // the relation name defined above

            'displayAttr' => 'warehouseName', // attribute or pseudo-attribute to display

            // length of the AutoComplete/display field, defaults to 50

//            'autoCompleteLength' => 60,

            // any attributes of CJuiAutoComplete and jQuery JUI AutoComplete widget may 

            // also be defined.  read the code and docs for all options

            'options' => array(

                // number of characters that must be typed before 

                // autoCompleter returns a value, defaults to 2

//                'minLength' => 3,

            ),

        ));

        ?>

    </td>

    <td>

        <?php echo $form->textField($model, "[$id]productFk", array('style' => 'width: 250px;')); ?> 

    </td>

    <td> 

        <?php echo $form->textField($model, "[$id]batchNo", array('style' => 'width: 100px; text-align: center;')); ?>

    </td>

    <td> 

        <?php echo $form->textField($model, "[$id]quantity", array('style' => 'width: 100px; text-align: right;')); ?>

    </td>

    <td>

        <?php echo CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/remove_ico.png'), '', array('class' => 'delete', 'onClick' => 'deleteDetail($(this))')); ?>

    </td>

</tr>



and this is my controller that preloaded with 5 TransactionDetail




public function actionCreate() {

        $model = new Transaction;

        $model->date = date('Y-m-d');

        $model->status = Category::ACTIVE;

        $model->operatorFk = Yii::app()->user->id;


        $details = array();

        for ($i = 0; $i < 5; $i++) {

            $details[] = new TransactionDetail;

        }


        // Uncomment the following line if AJAX validation is needed

        // $this->performAjaxValidation($model);


        if (isset($_POST['Transaction'], $_POST['TransactionDetail'])) {

            $model->attributes = $_POST['Transaction'];

            $valid = $model->validate();


            $_POST['TransactionDetail'] = array_values($_POST['TransactionDetail']);


            $details = array();

            for ($i = 0; $i < count($_POST['TransactionDetail']); $i++) {

                $details[] = new TransactionDetail;

            }


            foreach ($details as $i => $detail) {

                if (isset($_POST['TransactionDetail'][$i]))

                    $detail->attributes = $_POST['TransactionDetail'][$i];

                $valid = $detail->validate() && $valid;

            }


            if ($valid) {

                $model->save();

                foreach ($details as $i => $detail) {

                    $detail->transactionFk = $model->id;

                    $detail->save();

                }

                $this->redirect(array('view', 'id' => $model->id));

            }

        }


        $this->render('create', array(

            'model' => $model,

            'details' => $details,

        ));

    }



Also, I was wondering why the button [x] is not displayed properly now?

Cheers,

Daniel

Daniel,

It seems the problem is due to using renderPartial to show the existing and the new rows. My view does not use renderPartial(); each detail line is rendered as part of the parent form. But as I wrote before, in my case new rows are not allowed. I understand why you are using renderPartial(), this is correct for your situation.

Unfortunately I do not have time to make the extension work in this situation, right now. I am not sure it is possible to attach the jQuery autocomplete to an ajax response; but I haven’t tried it yet. If you are able to improve it for your situation please post the code or send it to me; if it seems generally useful I’ll incorporate back into the extension and republish.

The one thing I find puzzling about your last post is the Firebug screenshot, which shows the fieldname is -not- numbered. Even though you are using renderPartial the fieldNames should still get numbered properly. Are you definitely using the code I posted in this forum a few days ago?

Hi Jeremy,

not sure yet how to do it. Someone mention about using .live, do you have any idea?

Cheers,

Daniel

Hi

I’m using this extension in a app and it’s working, but now i need to load two dependent dropdowns from a value selected in this field, using de fk id.

Anyone can tell me if its possible using this extension.

Hi Jeremy,

First I wanted to say thanks for pulling this extension together, it is really helpful.

I have a problem that is confusing to the end users. If there is an error on any other value on the form that is not the EJuiAutoCompleteFkField, this field will then display the fk id and not the item that was in the auto complete drop down.

For example. I am using the field to select a city. So the user starts typing city names and the auto complete drop down works as it should and the user selects their city. But if they make an error anywhere else on the form the city field will now show the id (i.e. 456) instead of the city name they selected originally. I tried force setting this in an after find function but could not get that to work either. This is confusing to the end user so they go and try to type over that id with the city name again and only every once in awhile with the auto complete work without refreshing the whole page.

Any help with this?

Hi Jeremy!

I have also problem as Márcio Coragem.

For example in first autocompleate field i choose city.

And in second autocompleate field i must to choose street in the city choosen in first field.

How i can to do it? (I mean second field with street).

Great plugin, this has helped me out a lot.

Does anyone know how to specify the id or name of the FKfield (The hidden field that the FK is inserted into)?

Any help would be greatly appreciated.

Any ideas why I would be getting the following error, when inserting a new row into my database:

Trying to get property of non-object

Here is call for the auto complete field:


$this->widget('EJuiAutoCompleteFkField', array(

				'model'=>$model_main,

				'attribute'=>'ordered_for_location_id', //the FK field (from CJuiInputWidget)

				// controller method to return the autoComplete data (from CJuiAutoComplete)

				'sourceUrl'=>Yii::app()->createUrl('ordersWls/FindLocations'),

				// defaults to false.  set 'true' to display the FK field with 'readonly' attribute.

				//'showFKField'=>true,

				// display size of the FK field.  only matters if not hidden.  defaults to 10

				'FKFieldSize'=>15,

				'relName'=>'ordersWlsDiscSusps', // the relation name defined above

				'displayAttr'=>'StreetCityAndState',  // attribute or pseudo-attribute to display

				// length of the AutoComplete/display field, defaults to 50

				'autoCompleteLength'=>80,

				// any attributes of CJuiAutoComplete and jQuery JUI AutoComplete widget may 

				// also be defined.  read the code and docs for all options

				'options'=>array(

					  // number of characters that must be typed before 

					  // autoCompleter returns a value, defaults to 2

					  'minLength'=>3,					  

				),

		));

My mistake, I called to an incorrect relation. Issue solved by using the correct relation.