New Autocomplete with QCubed 2.0.1
Thu, 07/22/2010 - 19:46
Apparently, there is a new Autocomplete. The old one had a "SetSelectCallback", to set a callback, when an item is selected. Also, I suspect it is no longer possible to set the "|" (pipe) to separate the data to be listed and the data to be passed back via the "SetSelectCallback".
Is there any information on how to use this new Autocomplete?
Thanks,
LaCeja

LaCeja,
There is a small example for the new QAutocomplete in
assets/_core/php/examples/other_controls/jq_example.php.
I don't remember the details for the old plugin, but with the new control the source is set like this:
<?php$this->autocomplete = new QAutocomplete($this);
$this->autocomplete->Source = array("c++", "java", "php", "coldfusion", "javascript", "asp", "ruby");
?>
As for SetSelectCallback, if you use the current trunk (or apply the patch from #604 to 2.0.1, or wait for the upcoming 2.0.2), then you can do either
<?php$this->autocomplete->OnSelect = new QJavaScriptAction("alert('hi')");
?>
or more a la QCubed
<?php$this->autocomplete->AddAction(new QAutocomplete_SelectEvent(), new QJavaScriptAction("alert('hi')"));
?>
If this does not cover all that the other plugin was doing, take a look at the internals of QAutocompeteBase add see if there is anything else than will. If not, we can always add more to QAutocomplete to extend its functionality.
-Vartan
Vartan:
Thanks for the information. I was just reading up on the "onItemSelect" option and trying to figure out how to implement it. I'll install the patch and do some testing with it. I did download and install the new plugin into a fresh install of 2.0.1 and I can't get the example to work for the Ajax version. The client side version works great, but the server side doesn't return a list at all. Just the wait icon flashes for a moment and that's it.
For the QJavaScriptAction of the QAutocomplete_SelectEvent, am I passing just a php method name or do I need to write a js function to return the selected value. I have almost no js experience, so I don't really understand it.
Also, in the old version, each member of the array, passed to autocomplete, included a final entry, with a pipe delimiter ("|"). Is it the same for this new version. I think read somewhere that the pipe was not allowed.
Off subject, but are you planning to implement a new version of QSortable or will the one I provided still work in 2.0.1 and 2.0.2? I use it a lot and am working on several other programs that use it, so if it's going to change, I'll delay working any more on them. The one I wrote is pretty sloppy, but it works. So, I stopped fixing it.
Thanks again,
LaCeja
LaCeja,
The new controls are part of core, so when you say "plugin" I'm assuming you're really talking about the new controls. Can you give a small example of what's not working "for the Ajax version"? Do you mean if the Source is set with an ajax action?
QJavaScriptAction constructor takes a javascript code, so if you want to use QJavaScriptAction you would have to write some javascript. However, you can also use QAjaxControlAction (and any of its decedents) with the same event.
I don't know about the pipe, if that's part of the original JQuery control features, then it would work the same way. But if it was a special implementation by the old QAutocompleteTextBox plugin, then clearly it won't work here.
QSortable is also auto-generated already in 2.0.2, so you should see it when you apply the patch. It has an example in the example page as well.
I think I know what you mean now: The old QAutoCompleteTextBox (the one written for QCodo) uses the property "UseAjax" and the QAutoCompleteTextBoxEvent to associate an ajax callback for populating the autocomplete list. The new QAutocomplete does not use the UseAjax flag. It's possible to make it use an ajax callback for the list, but it's currently not easy. I will make some changes to the generator to simplify this use case.
-Vartan
Sounds great! But, just to be clear, what I'm saying is, the current example (for the 2.0.1 version, which installs with the plugin), using Ajax fails. So, whatever change you need to make to the generator (which by the way is incredibly fantastic!), the example will probably need to be updated as well.
LaCeja,
If I'm not mistaken your problem is with the QAjaxAutoCompleteTextBox plug-in and with its example. The new QAutocomplete control (which is in core) and the generator have nothing to do with that plug-in. I'm not the author of that plug-in, and in fact I have never used that with QCubed (I had used the old version with QCodo), so I'm not the right person to fix whatever problems that plug-in has. I suggest opening a ticket for the issue you're seeing, so that the author (or someone else) can fix it.
I will however try to make the new QAutocomplete control provide the equivalent functionality.
-Vartan
Thanks, Vartan. Will there be an example to go with it? It doesn't make much sense to use a separate plugin, if you're providing the same functionality as the old QCodo version. Actually, the previous version of the plugin was a little different from the QCodo version, as the QCodo version didn't include the SetSelectCallback. I believe that's something added by Agsel a few months ago.
Anyway, I'll wait for your enhancement and make use of that instead.
Thanks again,
LaCeja
LaCeja,
Just in case you wanna try and test the new AJAX functionality in QAutocomplete before it's released (hopefully as part of 2.0.2), you can get a patch from http://trac.qcu.be/projects/qcubed/ticket/620
The examples page is also updated with a small example for the ajax autocomplete. The method to set the data binder is called SetDataBinder() (similar to QDataGrid). As for the pipe functionality, I'm not sure if it's equivalent, but you can set the DataSource to a list of QAutocompleteListItem objects; this allows to show one string in the drop-down list, but put a different string in the text box once selected (and of course, the one in the text box is the one that will be posted back to the server on submit).
Let us know your feedback.
-Vartan
Vartan:
What I was looking for was the ability to include a "hidden" value, which would, for example, be the Id of the selected row. For example, I might have an autocomplete for the Customer, which might include the Customer Name and first line of address. In a "hidden" portion would be the Id of the Customer row in the database. The old autocomplete allowed for a pipe ("|"), which separated the portion displayed in the list from the part, which followed the pipe. You could set a callback, which would callback to a specified method to receive the selected line, where you could "explode" it, based on the pipe as the delimiter and then do whatever you needed to do with it.
The callback is an absolute necessity for me. For example, a user might select an order in an autocomplete, and in the callback, I find the row of the order header and all the details to display. If I had the callback, I could allow the control to update with the Id and then do whatever I needed with it, including populate the entire form. I use that callback on select in almost every instance of autocmplete.
So, is it possible to include a callback, when the user makes a selection?
Hope this make sense.
Thanks,
LaCeja
LaCeja,
The callback on select is already supported by QAutocompete (even in 2.0.1). Here is how to set it:
<?php$this->autocomplete->AddAction(new QAutocomplete_SelectEvent(), new QAjaxAction("autocomplete_selected"));
?>
Note that you could also use QJavaScriptAction the same way.
The hidden value feature however is not there. I'll think about this some more.
-Vartan
Vartan:
Thanks... that's the part I missed. I'll do some testing in the morning. Actually, I think this will suffice, because I can set the value to be set in the text box to the Id. On QAutocomplete_SelectEvent, I can use that value to do whatever I want, including replacing it with the actual value I want to appear in that text box. There will be a flicker, when the value changes, but I think my users can live with that.
This is some fantastic work! We all appreciate it very much!
Thanks again,
LaCeja
Hello
I saw your interesting conversation and I could make something very nice.
For cons I encounter a problem in order to implement Autocomplete multivalued, as on page jquery ui:
http://jqueryui.com/demos/autocomplete/#multiple
javascript code that I put in place does not recognize the following parameters: event, ui, leaving that uses the Select function without parameters: function (). How to fix this? Thank you. best regards
I'm not sure I fully understand your question, but if you use the current trunk version (the upcoming 2.0.2), you can pass functions with arguments: in that version the QJsClosure constructor takes a $strParamsArray, which is then rendered as the arguments of the javascript function.
-Vartan
yes thank you I'll do the tests.
By cons I have another issue with autocomplete:
in index.php
<?php
$strArray = array ("abc", "bcd", "cde", "dfg");
$source = $ StrArray;
$this->txtField = new QAutocomplete($this,"txtField");
$this->txtField->MinLength = 0;
$this->txtField->Width = 150;
$this->txtField->Name = QApplication::Translate("Field");
$this->txtField->Required = FALSE;
$this->txtField->CausesValidation = FALSE;
$this->txtField->Search ();
$this->txtField->Source = $source;
?>
javascript code generated is the following
jQuery("#txtField").autocomplete({
minLength: 0,
source: ["abc", "bcd", "cde", "dfg"],});
(you see comma, which is after the "]")
IE8, IE7 - shows an error generated on the line: "Expected identify, string or number";
But Firefox, Google Chrome, Safari, Opera works well and does not detect the error.
Do you have any suggestions to this problem?
Thank you
ourri,
This problem is also fixed as part of the same ticket (#620), which I just committed to trunk. If it's possible, try to get an update and test again.
Thanks.
-Vartan
It work.
thank you vartan.
has any progress been made on this yet ? ie. is it usable as a droplist replacement (as it should be) now, with support for ID's that correspond to label matches? ala http://grover.open2space.com/content/using-jquery-autocomplete-ids
"as it should be" is a pretty strong statement, especially considering that jQuery UI has been around for a couple of years now without that feature. I have already clarified my own opinion about this "feature" in the lengthy discussion in another thread: http://qcu.be/content/qautocomplete-20 . I still would appreciate if interested parties would provide code for the ideas discussed in that thread. However, to my knowledge, no code came out of that. So I would say, short of implementing it yourself, your best bet is to use the old QAutoCompleteTextBox plugin. This of course, if after reading all the arguments in that thread you're still sure you need the feature.
-Vartan
Cache, the old autocomplete plugin for QCubed uses the plugin written and enhanced by Dylan Verheul, Dan Switzer, and Jorn Zaefferer. It works quite well and I use it extensively in my application, mainly because I'm still using QCubed 2.0.1. I have used it both with Ajax calls, as well as with the drop list and it seems to work and perform quite well. When I get around to upgrading to 2.0.2 or QCubed, I'll look into adding the drop list to it, but for now the old plugin is working very well.
Merrill