Yii Framework Forum: Autocomplete, Convert Space(%) To + In Link - Yii Framework Forum

Jump to content

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

Autocomplete, Convert Space(%) To + In Link Rate Topic: -----

#1 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 26 March 2013 - 03:23 PM

Hiii

I have an autocomplete script which search for merchants in my system, and sends the user to a new page..

View:
<?php $this->widget('ext.myAutoComplete', array(
    	'name' => 'merchantName',
    	'source' => $this->createUrl('review/autocomplete'),
    	'options' => array(
        	'minLength'=>1,
        	'autoFill'=>false,
        	'focus'=> 'js:function( event, ui ) {
	            	$( "#autocomplete" ).val( ui.item.name );
    	        	return false;
        	}',
		'select' => 'js:function( event, ui ){
     			top.location = "write/" + ui.item.name;
		}'
     	),
    	'htmlOptions'=>array('class'=>'input-1', 'autocomplete'=>'off', 'size'=>80,),
    	'methodChain'=>'.data( "autocomplete" )._renderItem = function( ul, item ) {
	        return $( "<li></li>" )
	        .data( "item.autocomplete", item )
	        .append( "<a>" + item.name +  "</a>" )
	        .appendTo( ul );
    	};'
)); ?>


Controller:
public function actionAutocomplete(){
    	$res = array();
    	$term = Yii::app()->getRequest()->getParam('term', false);
    	if ($term)
    	{
     	 	$sql = 'SELECT id, name FROM merchants where LCASE(name) LIKE :name';
      		$cmd = Yii::app()->db->createCommand($sql);
      		$cmd->bindValue(":name","%".strtolower($term)."%", PDO::PARAM_STR);
      		$res = $cmd->queryAll();
    	}
    	echo CJSON::encode($res);
    	Yii::app()->end();
	}


This works all fine.. But what happens when i pick a merchant named, etc. "Merchant Casper"..
It sends me to: "Write/Merchant%Casper"

But I would like all the % to be switched out with + instead..

How can i do this??


// Casper
0

#2 User is offline   dawd 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 26-March 13

Posted 26 March 2013 - 05:42 PM

Parse string in js with :

replace('%', '+')
0

#3 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 27 March 2013 - 03:06 AM

Could you maybe give me an example on how to implement that in my script??

I've tried just to put it in the Select before top.location, with and without a ; at the end, and ' and " around %+, and all different tries just makes my link stop working... :/
0

#4 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 30 March 2013 - 06:02 AM

Isn't there anyone else with some javascript skills who can help me with this one..? :)
0

#5 User is offline   Sergey Zlomovsky 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-December 12

Posted 04 April 2013 - 08:53 AM

You can change substring with this JavaScript piece:
ui.item.name = ui.item.name.replace('%','+');

As you see, replace function is called as a method of the String (ui.item.name) object.
0

#6 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 05 April 2013 - 02:55 AM

Hmm, I got an error 500 (Division by zero) error when i implemented it in my code.. :/

i put it in here:
'select' => 'js:function( event, ui ){
	ui.item.name = ui.item.name.replace('%','+');
  	top.location = "write/" + ui.item.name;
}'

0

#7 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 12 April 2013 - 03:53 AM

Really..?

Can someone please help me with this problem...? :/
0

#8 User is offline   Sergey Zlomovsky 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-December 12

Posted 21 April 2013 - 04:11 AM

View Postcasperskou, on 05 April 2013 - 02:55 AM, said:

Hmm, I got an error 500 (Division by zero) error when i implemented it in my code.. :/

i put it in here:
'select' => 'js:function( event, ui ){
	ui.item.name = ui.item.name.replace('%','+');
  	top.location = "write/" + ui.item.name;
}'


ahaha. Just be more carefully with quotation marks. Change «ui.item.name = ui.item.name.replace('%','+');» to «ui.item.name = ui.item.name.replace("%","+");».

As a result your code will be:
'select' => 'js:function( event, ui ){
	ui.item.name = ui.item.name.replace("%","+");
  	top.location = "write/" + ui.item.name;
}'


Always keep in mind, you can freely use «"» quotation mark inside «'» quote, and you can use «'» quotation mark inside «"» quote. But if you will use either «'» quotation mark inside «'» quote or «"» inside «"» (without escaping — \" or \'), you will catch errors.
0

#9 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 891
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 21 April 2013 - 08:01 AM

okay guys be serious what would happen if he has a item with more than 2 words he is gonna have problem again here is one for all


ui.item.name = ui.item.name.replace(/\s/g, '+');

0

#10 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 21 April 2013 - 11:25 AM

thanks a lot for the help guys...

I ended up using alirz23 solution due it sounded like it's a bit more secure..!

But thanks to you too Sergey
0

#11 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 891
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 21 April 2013 - 11:51 AM

casperskou don't you think I deserve something for that how about a vote
0

#12 User is offline   Sergey Zlomovsky 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-December 12

Posted 21 April 2013 - 01:10 PM

I'm just humble syntax helper. But the global replacing, rather than replacing first occurrence, is better idea, yes. :)
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