Yii Framework Forum: [solved]nyimpen data dari dynamic text field - Yii Framework Forum

Jump to content

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

[solved]nyimpen data dari dynamic text field Rate Topic: -----

#1 User is offline   ressaince 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 10-January 11
  • Location:Jakarta

Posted 13 January 2011 - 07:44 AM

gan ane punya textfield yang bisa bertambah terus kalo di teken tombol [add more]
nah waktu ane teken submit yang kesimpen cuman textfield yang terakhhir
yang laennya kagak..
seharusnya kan looping dan ngesave semua nya ya??

gimana caranya yah..
di view ane coba kayak gini:

<?php echo textField($model,'product_number[]',array(size=>60....));

ternyata ga kesimpen gan,,hmmm..array nya ga kebaca
ada yang bisa bantu ga?

apa saya pake html biasa aja yahh..hmm...
take it or leave it
0

#2 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 13 January 2011 - 11:05 AM

bisa liat kode seluruhnya gak?
dari mulai view, js, sama di controllernya?
0

#3 User is offline   ressaince 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 10-January 11
  • Location:Jakarta

Posted 13 January 2011 - 08:12 PM

bisa2 bro...

ini viewnya..
<?php
	Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/css/javascript/jquery.min.js',CClientScript::POS_HEAD);
	Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/css/javascript/reCopy.js',CClientScript::POS_HEAD);
	Yii::app()->clientScript->registerScript("","
	$(function(){
	var removeLink = ' <a class=\"remove\" href=\"#\" onclick=\"$(this).parent().slideUp(function(){ $(this).remove() }); return false\">remove</a>';
$('a.add').relCopy({ append: removeLink});	
});
	",CClientScript::POS_HEAD);
	?>
<p class="clone">
		
		<?php echo $form->labelEx($model,'product_number'); ?>
		<?php //echo $form->textField($model,'product_number[]',array('size'=>30,'maxlength'=>128)); 
		echo CHtml::activeTextField($model,'product_number[]');
		?>
		<?php echo $form->error($model,'product_number'); ?>
	
	
		<?php echo $form->labelEx($model,'product_desc'); ?>
		<?php 
		echo CHtml::activeTextField($model,'product_desc[]');
		//echo $form->textField($model,'product_desc[]',array('size'=>30,'maxlength'=>128)); ?>
		<?php echo $form->error($model,'product_desc'); ?>
	
	
	
		<?php echo $form->labelEx($model,'quantity'); ?>
		<?php 
		echo CHtml::activeTextField($model,'quantity[]');
		//echo $form->textField($model,'quantity[]'); ?>
		<?php echo $form->error($model,'quantity'); ?>
	
	</p>
	<p><a href="#" class="add" rel=".clone">Add More</a></p>
	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Simpan' : 'Save'); ?>
	</div>


ini js reCopy nya
/**
 * jQuery-Plugin "relCopy"
 * 
 * @version: 1.1.0, 25.02.2010
 * 
 * @author: Andres Vidal
 *          code@andresvidal.com
 *          http://www.andresvidal.com
 *
 * Instructions: Call $(selector).relCopy(options) on an element with a jQuery type selector 
 * defined in the attribute "rel" tag. This defines the DOM element to copy.
 * @example: $('a.copy').relCopy({limit: 5}); // <a href="example.com" class="copy" rel=".phone">Copy Phone</a>
 *
 * @param: string	excludeSelector - A jQuery selector used to exclude an element and its children
 * @param: integer	limit - The number of allowed copies. Default: 0 is unlimited
 * @param: string	append - HTML to attach at the end of each copy. Default: remove link
 * @param: string	copyClass - A class to attach to each copy
 * @param: boolean	clearInputs - Option to clear each copies text input fields or textarea
 * 
 */

(function($) {

	$.fn.relCopy = function(options) {
		var settings = jQuery.extend({
			excludeSelector: ".exclude",
			emptySelector: ".empty",
			copyClass: "copy",
			append: '',
			clearInputs: true,
			limit: 0 // 0 = unlimited
		}, options);
		
		settings.limit = parseInt(settings.limit);
		
		// loop each element
		this.each(function() {
			
			// set click action
			$(this).click(function(){
				var rel = $(this).attr('rel'); // rel in jquery selector format				
				var counter = $(rel).length;
				
				// stop limit
				if (settings.limit != 0 && counter >= settings.limit){
					return false;
				};
				
				var master = $(rel+":first");
				var parent = $(master).parent();						
				var clone = $(master).clone(true).addClass(settings.copyClass+counter).append(settings.append);
				
				//Remove Elements with excludeSelector
				if (settings.excludeSelector){
					$(clone).find(settings.excludeSelector).remove();
					
				};
				
				//Empty Elements with emptySelector
				if (settings.emptySelector){
					$(clone).find(settings.emptySelector).empty();
				};								
				
				// Increment Clone IDs
				if ( $(clone).attr('id') ){
					var newid = $(clone).attr('id') + (counter +1);
					$(clone).attr('id', newid);
					
				};
				
				// Increment Clone Children IDs
				$(clone).find('[id]').each(function(){
					var newid = $(this).attr('id') + (counter +1);
					$(this).attr('id', newid);
				});
				
				//Clear Inputs/Textarea
				if (settings.clearInputs){
					$(clone).find(':input').each(function(){
						var type = $(this).attr('type');
						switch(type)
						{
							case "button":
								break;
							case "reset":
								break;
							case "submit":
								break;
							case "checkbox":
								$(this).attr('checked', '');
								break;
							default:
							  $(this).val("");
						}						
					});					
				};
				
				$(parent).find(rel+':last').after(clone);
				
				return false;
				
			}); // end click action
			
		}); //end each loop
		
		return this; // return to jQuery
	};
	
})(jQuery);

jquery.min.js nya bisa diambil di http://ajax.googleap...2/jquery.min.js

ini controllernya
public function actionCreate()
	{
		$model=new ProductDelivery;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		//tadinya nyoba begini
                if(isset($_POST['ProductDelivery'])
                $model->attributes=$_POST['ProductDelivery'];
		$array=$_POST['ProductDelivery']['product_number']
                foreach($array as $isi)
                {
                 $model->product_number=$isi; ///ada 3 field yang diclone,ini cuma coba2 aja buad masukin product_number dulu...
                 $model->save();
                 }
                 
			
			//if($model->save())
				//$this->redirect(array('view','id'=>$model->id));

		$this->render('create',array(
			'model'=>$model,
		));
	}

take it or leave it
0

#4 User is offline   fastcrash 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 10-March 10
  • Location:South Jakarta

Posted 14 January 2011 - 02:42 AM

Quote

ternyata ga kesimpen gan,,hmmm..array nya ga kebaca


$_POST['ProductDelivery']['product_number']


klo di
print_r($_POST['ProductDelivery']['product_number'])

datanya mmg ada 1?

klo di view source attribut name-nya pasti seperti ini
name="ProductDelivery[product_number][]"


tp klo seperti ini pasti kebaca array-nya
name="product_number[]"


waktu itu karna ga mo repot, saya pake html biasa ;D (syndrome?)
0

#5 User is offline   ressaince 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 10-January 11
  • Location:Jakarta

Posted 14 January 2011 - 04:06 AM

setelah saya print_r($model->attributes=$_POST['ProductDelivery']) seperti ajaran agan ternyata:
Array
(
    [delivery_order_no] => iousrij
    [order_cust_no] => jhskrd
    [invoice_no] => jr
    [delivery_date] => 2011-01-14
    [order_cust_date] => 2011-01-14
    [invoice_date] => 2011-01-14
    [customer] => sjd
    [forwarding_agent] => sjd
    [product_number] => Array
        (
            [0] => sjdi
            [1] => ajshjhja
        )

    [product_desc] => Array
        (
            [0] => sjdij
            [1] => hduwhuw
        )

    [quantity] => Array
        (
            [0] => 21
            [1] => 3
        )

)



ternyata udah bener : <?php echo $form->textField($model,'quantity[]'); ?>

sekarang lagi mempersiapkan logic buat penyimpanannya ajah,cuman dari tadi pake foreach gagal melulu,wkwkwk

by the way ini belom kupisah jadi 2 model,hehehehe,,,,,males tadi..

oh ya thanks ya masta
take it or leave it
0

#6 User is offline   fastcrash 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 10-March 10
  • Location:South Jakarta

Posted 14 January 2011 - 05:41 AM

$array = $model->attributes=$_POST['ProductDelivery']['product_desc'];
foreach($array as $isi)
{
   $new= new myModelproduct_desc;  //nama modelnya/tabel
   $new->product_number=$isi;
   $new->save();
}


perhatikan untuk field yg lain di db boleh null atau tidak.
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