Yii Framework Forum: Cjuiautocomplete - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cjuiautocomplete Rate Topic: -----

#1 User is offline   ceejay 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 17-March 14

Posted 27 March 2014 - 08:34 PM

I would like the list returned by CJuiAutoComplete to be filtered to just those that match at the beginnning exactly what is typed. So I want "a" to return "apple" but not "orange."

JQuery's autocomplete has a javascript function to accomplish this:

'select'=>$( "#autocomplete" ).autocomplete({
  source: function( request, response ) {
          var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
          response( $.grep( tags, function( item ){
              return matcher.test( item );
          }) );
      }
});


Where 'tags' is the list of returned items.

How can I do this in CJuiAutoComplete?
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 28 March 2014 - 04:16 AM

You should be able to do the same. I think it would look like this:

$this->widget('zii.widgets.jui.CJuiAutoComplete',array(
    // other options...
    'source'=>new CJavaScriptExpression('
        function( request, response ) {
            var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
            response( $.grep( tags, function( item ){
                return matcher.test( item );
            }) );
        }
    '),
));


Check out the source property.
0

#3 User is offline   ceejay 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 17-March 14

Posted 28 March 2014 - 08:29 AM

OK. But now my problem is that I was using the source parameter originally to get the list of tags. So how can I combine both the getting of the tags and the regex in that same source parameter?

Is it possible to pass the user's keystroke input to the Yii PHP createLeadnameList() method in my model that builds the list of tags? If not, how can I build the list of tags and then process my regex expression against it as the user types in the field?

My existing CJuiAutoComplete code is

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
	    	'name'=>'leadname',
		'source'=>$model->createLeadnameList(),
		'model'=>$model,
		'attribute'=>'leadname',
		'options'=>array('minLength'=>'1',),
		'htmlOptions'=>array('style'=>'height:20px;'),)
                );

0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 28 March 2014 - 08:39 AM

Can you do something like this?

$this->widget('zii.widgets.jui.CJuiAutoComplete',array(
    // other options...
    'source'=>new CJavaScriptExpression('
        function( request, response ) {
            var tags = ' . CJavaScript::encode($model->createLeadnameList()) . ';
            var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
            response( $.grep( tags, function( item ){
                return matcher.test( item );
            }) );
        }
    '),
));

0

#5 User is offline   ceejay 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 17-March 14

Posted 28 March 2014 - 10:24 AM

Yes. Thank you. That works :)

Can you explain what the dots do before and after the CJavaScript::encode fragment in the var tags line? I know they are used for appending in PHP, but I don't understand what is being appended in the tags variable.

Here is my working CJuiAutoComplete statement:

        $this->widget('zii.widgets.jui.CJuiAutoComplete',array(
            'name'=>'leadname',
            'model'=>$model,
            'attribute'=>'leadname',
            'options'=>array(
                'minLength'=>'1',
                ),
            'htmlOptions'=>array(
                    'style'=>'height:20px;'
                ),
            'source'=>new CJavaScriptExpression('
                    function( request, response ) {
                    var tags = ' . CJavaScript::encode($model->createLeadnameList()) . ';
                    var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
                    response( $.grep( tags, function( item ){
                        return matcher.test( item );
                    }) );
                    }
                '),
            )
        );

0

#6 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 28 March 2014 - 10:38 AM

The call to CJavaScript::encode() is just creating a string of javascript, so the dot operators are being used for concatenation as normal.
0

#7 User is offline   ceejay 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 17-March 14

Posted 28 March 2014 - 11:28 AM

String. Got it. Thanks again.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users