Yii Framework Forum: My Patched-Up JQrelcopy - Yii Framework Forum

Jump to content

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

My Patched-Up JQrelcopy Adds a unique ID to name attribute Rate Topic: -----

#1 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 09-February 11

Posted 27 March 2012 - 12:07 AM

Cloning inputs was annoying me because JQrelcopy does not automatically handle the 'name' attributes of the inputs. For example, if you were to clone not just a field but a model field you would want your data like this:

0
field1 #part of modelA
field2 #part of modelA
field3 #part of modelA

1
field1 #part of modelA
field2 #part of modelA
field2 #part of modelA

The default implementation does not provide for that. You would instead get:
0
field1
1
field2
2
field3
and so on...

For many reasons I did not want to deal with that in my app.

Bottom line, this patched version will produce output like the following:
name="Model[$index][field]" id="Model_$index_field"

The attached file is a drop-in replacement for the default JS file with no further modifications needed. Props to Ianare for the idea. His fork (which is non working for my purposes) is here:
https://github.com/d...k/yii-jqrelcopy

This script works with the datepicker as well. Hope someone finds this helpful.

Attached File(s)


0

#2 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 28 March 2012 - 11:32 AM

Thanks for this! I'm still using the jappendo extension with a controller that handles sets of "model[]" inputs.....hardly a robust solution, but has been working...looking forward to trying this method.

My form currently uses autocomplete fields, so I don't know if I'll be able to implement this anytime soon...

Thanks!
0

#3 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 09-February 11

Posted 28 March 2012 - 06:39 PM

Check out addAnother.js by Jeremiah something. He has solved the autocomplete issue (but his extension still has the model [] thingy problem). You may be able to hack that into relcopy.
0

#4 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 28 March 2012 - 09:19 PM

thanks I haven't seen that plugin before. It looks like there's some code to handle the row index in there too

http://www.michaelpe...--jquery-plugin
0

#5 User is offline   fructose 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 09-December 11

Posted 18 June 2012 - 06:33 PM

Used this and it seems to be working. Thanks for sharing!
0

#6 User is offline   pinky 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 27-July 12

Posted 27 July 2012 - 10:48 AM

I tried replacing the javascript file with yours and it did not work. After replacement, it works the same as the original jqrelcopy extension before replacement. The textfield name is still the same for all the duplicated textfields, only the textfields ids are incremental.

Is there any other things I have to delete, replace or change? What about JQRelcopy.php file? Do I need to replace it?

Any idea why this happens and how I can solve this problem?
0

#7 User is offline   Sage 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 29-August 12
  • Location:California, USA

Posted 27 September 2012 - 10:13 AM

View Postpinky, on 27 July 2012 - 10:48 AM, said:

I tried replacing the javascript file with yours and it did not work. After replacement, it works the same as the original jqrelcopy extension before replacement. The textfield name is still the same for all the duplicated textfields, only the textfields ids are incremental.

Is there any other things I have to delete, replace or change? What about JQRelcopy.php file? Do I need to replace it?

Any idea why this happens and how I can solve this problem?


Did you clear the contents of the assets folder in the main directory of your app?

EDIT: Looks like it's the same here as well.
0

#8 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 09-February 11

Posted 27 September 2012 - 06:36 PM

I do not get this result.

In my installation the ID and name fields both increment the ID when a row is cloned.

Can you show how you are using it?
0

#9 User is offline   Tsumi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 09-October 12

Posted 09 October 2012 - 08:40 AM

Thank you very much for sharing, it works fine for me :D
0

#10 User is offline   CobraF 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 02-December 12

Posted 02 December 2012 - 09:35 AM

Hello,

Thanks very much for the solution. May I check how the 'view' should be setup based on the updated JS file? I tried clearing the /assets folder but using the 'view' definition below.

<div class="row user">
	<?php echo CHtml::label('First Name',''); ?>
	<?php echo CHtml::textField('Users[][firstname]','',array('style'=>'width:100px')); ?>
</div>

0

#11 User is offline   CobraF 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 02-December 12

Posted 02 December 2012 - 10:55 AM

View PostCobraF, on 02 December 2012 - 09:35 AM, said:

Hello,

Thanks very much for the solution. May I check how the 'view' should be setup based on the updated JS file? I tried clearing the /assets folder but using the 'view' definition below.

<div class="row user">
	<?php echo CHtml::label('First Name',''); ?>
	<?php echo CHtml::textField('Users[][firstname]','',array('style'=>'width:100px')); ?>
</div>




Folks - I think I have it. Include a 0 in the square brackets.

<div class="row user">
	<?php echo CHtml::label('First Name',''); ?>
	<?php echo CHtml::textField('Users[0][firstname]','',array('style'=>'width:100px')); ?>
</div>

0

#12 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 09-February 11

Posted 08 January 2013 - 12:01 AM

I should have mentioned that. I am not a strong jquery coder and I thought it would be simplest to just start with 0 index.

If you display existing 'rows' (fetch from DB) this will still work to increment the last value, but if you are starting new you want to use 0 for your first row.
0

#13 User is offline   selectstar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 11-June 13

Posted 11 June 2013 - 02:47 AM

I have managed to achieve the same results in generating unique IDs just by using some Javascript

this is my code

$this->widget('ext.jqrelcopy.JQRelcopy',
array(
      'id' => 'copylink',
      'removeText' => 'remove',
      'jsAfterNewId' => "if(typeof this.attr('name') !== 'undefined'){ this.attr('name', this.attr('name').replace('new', 'new_'+counter));}",
));


assuming the fields are structured as the following

<input type="input" name="controllerName[new][title]">
<textarea name="controllerName[new][description]"></textarea>

Each time the copy action is triggered the Javascript searches for all "name" attributes and replaces [new] with [new_1], [new_2]. The key [new_x] increments using the JS "counter" variable used in jqrelcopy Javascript.
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