CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Autocomplete trouble
Pages: [1] 2

Author Topic: Autocomplete trouble  (Read 2153 times)

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Autocomplete trouble
February 03, 2015, 06:28:49 am
I am struggling with an autocomplete using CRM.api3 (which works like a charm). Everything seems hunkydory when I use the .done for the CRM.api3 call, but not when I try to parse it into the autocomplete widget  ??? ??? ???
Can you spot the (must be blatantly obvious) mistake?

My code is:
Code: [Select]
{literal}
  <script type="text/javascript">
    cj(function($) {
      var testErik = CRM.api3('Contact', 'Get', {
        return:"sort_name",
        is_deleted:0,
        is_deceased:0,
        json:1});

      $('#ehtestje').autocomplete(testErik, {
        width:200,
        dataType:"json",
        parse: function(data){
          var parsed = new Array();
          $.each(data.values, function(id){
            parsed.push({data:data.values[id].contact_id, value:data.values[id].sort_name, result:data.values[id].sort_name });
          });
          return parsed;
        },
        selectFirst:false,
        matchContains:true,
        minChars:2
      });
    });
  </script>
{/literal}
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Autocomplete trouble
February 03, 2015, 06:53:59 am
Which version of Civi are you using? In 4.4 and below $.autocomplete was bound to a legacy plugin which has since been removed. In 4.5 and above $.autocompete is bound to jQuery UI's autocomplete widget. But in 4.5 we tend to use crmEntityRef a lot more than we use autocompletes because they're more robust.
Try asking your question on the new CiviCRM help site.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Autocomplete trouble
February 03, 2015, 07:01:52 am
In this project we are using 4.4  :(
And migrating is not an option (yet) so I am going to have to struggle with the autocomplete
« Last Edit: February 03, 2015, 07:05:03 am by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Autocomplete trouble
February 03, 2015, 08:25:18 am
I can't remember exactly what the signature of the legacy autocomplete plugin was... BUT you could improve forward-compatibility with your code by not using it.
4.4 does have the jQuery UI autocomplete widget built-in, it's just a little hard to get at because the other plugin is hogging its namespace. But you can access it using a trick like this.
Try asking your question on the new CiviCRM help site.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Autocomplete trouble
February 03, 2015, 10:05:23 am
Thanks Coleman, but I will need a little more explaining......I understand the principle that I can override the function, but I have no clue what I would do next from your example? Altough I can happily PHP my way around in CiviCRM, I am a total newbie in jQuery. So what happens in the trick and do you know of a good spot where the autocomplete widget is explained, or should I just Google?
« Last Edit: February 03, 2015, 10:11:59 am by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Autocomplete trouble
February 03, 2015, 10:14:15 am
Is this the autocomplete widget you are referring to?
http://jqueryui.com/autocomplete/
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Autocomplete trouble
February 03, 2015, 10:41:41 am
Yes, that's the one. Unlike the legacy autocomplete plugin, the jQuery UI one has documentation!

Here is what I was referring to - since both autocomplete widgets are trying to occupy the $.autocomplete namespace, you need to access the new one differently - basically make up a new name for it, like so:

Code: [Select]
cj.widget("civicrm.newAutocomplete", cj.ui.autocomplete, {});

cj(function($) {
  $('#ehtestje').autocomplete(...);
});
Try asking your question on the new CiviCRM help site.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Autocomplete trouble
February 04, 2015, 02:58:47 am
Thanks, I will have a go with that!  :)
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Autocomplete trouble
February 10, 2015, 07:47:54 am
Coleman,
still experiencing trouble, can you have one more look?

I was expecting this to work (and I have tested with a simple array although I want to use API results in the end):
Code: [Select]
<script type="text/javascript">
  {literal}
    cj.widget("civicrm.newAutocomplete", cj.ui.autocomplete, {});
    cj(function($) {
      var testData = ["Erik Hommel","Jaap Jansma","Betty Dolfing"];
      $('#ehtestje').autocomplete({source:testData});
    });
  {/literal}
</script>

but it does nothing. If I do this however, I get an autocomplete working. So to me it looks like I am still accessing the old widget??????
Code: [Select]
<script type="text/javascript">
  {literal}
    cj.widget("civicrm.newAutocomplete", cj.ui.autocomplete, {});
    cj(function($) {
      var testData = ["Erik Hommel","Jaap Jansma","Betty Dolfing"];
      $('#ehtestje').autocomplete(testData);
    });
  {/literal}
</script>
which will then cause me all sorts of parsing hassle. I can get around this by creating a function that puts the api result into text, but I want to be stubborn and investigate the way it is supposed to work neatly :-)
« Last Edit: February 10, 2015, 07:50:24 am by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Autocomplete trouble
February 10, 2015, 10:51:04 am
You almost got it, you just need to use the name of your newAutocomplete :)
So your code should be
Code: [Select]
<script type="text/javascript">
  {literal}
    cj.widget("civicrm.newAutocomplete", cj.ui.autocomplete, {});
    cj(function($) {
      var testData = ["Erik Hommel","Jaap Jansma","Betty Dolfing"];
      $('#ehtestje').newAutocomplete({source:testData});
    });
  {/literal}
</script>
Try asking your question on the new CiviCRM help site.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Autocomplete trouble
February 10, 2015, 11:19:19 am
I did actually try that, but then got an error:
Code: [Select]
TypeError: this.menu is undefined


...element.zIndex()+1).hide().data("menu"),this._on(this.menu.element,{mousedown:fu...

so I assumed I was using the wrong name. So I was on the right path but still have an error, but now probably in the widget? So should I drop back to the previous widget and do some function to parse the results of the CRM.api3 to the autocomplete?
« Last Edit: February 10, 2015, 11:30:43 am by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Autocomplete trouble
February 10, 2015, 12:19:29 pm
Yikes, I just tried it myself and that code works in 4.5 but not in 4.4... there must be something about that widget that breaks when it is renamed. Sorry for leading you astray...

I guess your 2 choices are:
- Upgrade to 4.5 and get all the great features of entityRef widgets.
- Use the old autocomplete. I suppose it doesn't really matter which one you use because either way you are not using entityRef so either way you are writing legacy code.
Try asking your question on the new CiviCRM help site.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
SOLVED Re: Autocomplete trouble
February 11, 2015, 04:21:38 am
I love to be led astray  ;D

Seriously, I understand a lot more about the issue now so I am happy :-) Solved it roughly like this:
Code: [Select]
<script type="text/javascript">
  {literal}
    cj(function($) {
      var tekstArray = [];
      CRM.api('Contact', 'get', {
        contact_type: "Individual",
        return: "sort_name",
        is_deleted: 0}, {
        success: function(data) {
          $.each(data.values, function(key) {
            tekstArray.push(data.values[key].contact_id,data.values[key].sort_name);
          });
          console.log(tekstArray);
          $('#ehtestje').autocomplete(tekstArray, {minChars:1});
        }
      });
    });
  {/literal}
</script>
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Autocomplete trouble
February 11, 2015, 01:48:56 pm
Hmm, that looks like sort of the opposite of an ajax autocomplete - it loads the entire(?) database into the client's memory on page load and then serves choices from that.
I guess that would work with a small database...
Try asking your question on the new CiviCRM help site.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Autocomplete trouble
February 11, 2015, 02:50:33 pm
Agree! The example was meant as a basic structure, not the end solution!!!! This will now be worked with this structure, will update the post with the actual end solution once it is done :-)
« Last Edit: February 11, 2015, 11:25:28 pm by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Autocomplete trouble

This forum was archived on 2017-11-26.