kali ini saya akan berbagi tips bagaimana membuat input data lebih dari satu baris (Multiple row input) pada yii. Kali ini extension yang akan digunakan adalah Jqrelcopy yang dapat didownload di sini  http://www.yiiframework.com/extension/jqrelcopy/. Baiklah langsung saja ke TKP  berikut tahapannya :

1. Extract extension ke forlder extension anda

2. Buat script pada controller seperti berikut :

  protected function saveTbkategori($formData)
    {
        if (empty($formData))
            return;
 
        $result = array();
            $idx=0;
 
                //You will get 3 arrays in $formData: id, firstname, lastname
 
        foreach($formData['nama_kategori'] as $name)
        {
                try{   
                    $model = new Tbkategori;
                   $model->nama_kategori = $name;
 
                   //The other attributes can be found at the same postion in the formData
                   //$model->value = $formData['value'][$idx];
                   //                $model->comment = $formData['comment'][$idx];
 
                   //no id is submitted for new items 
                   if(!empty($formData['id'][$idx]))
                       $model->id_kategori = $formData['id'][$idx];
                    }
                    catch (Exception $c)
                {
                throw new CException('asd');
                }

                      
                   if(!$model->save())
                      return false;
 
           $idx++;
        }
            return true;
    }

   
    public function actionCreate()
    {
    if(isset($_POST['Tbkategori']))
        {
                    if($this->saveTbkategori($_POST['Tbkategori']))

                        $this->redirect(array('admin','id'=>$model->id_kategori));
        }
  
        $model=new Tbkategori;
        //create an array with one model Pogo
        $data = array($model);
  
        $this->render('create',array(
                              'model'=>$data, //array of 'Pogo'
                   ));
       
    }

3. Buat script pada _form seperti berikut :

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'tbkategori-form',
        'enableAjaxValidation'=>true,
        'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>

                          $removeText = 'Hapus baris';
                        $this->widget('ext.jqrelcopy.JQRelcopy',array(
                         'id' => 'add_kategori',
                         'removeText' => $removeText,                       
                         'options' => array('excludeSelector'=>'.nocopy'),
                          'removeHtmlOptions' => array('style'=>'color:red'),
                          
                     ));
?>
erorSummary($model); ?>    


       $idx = 0;

        $count = count($data);
        foreach($data as $pogo):
          //the last added row is the row to copy
          $copyClass = ($idx == $count-1) ? ' copy' : '';
 
        ?>
         //don't allow to edit the id, don't copy the id to new record
         echo CHtml::hiddenField('Tbkategori[id][]',$pogo->id_kategori,array('class'=>'nocopy')); 
               echo CHtml::tag('span',array('class'=>'nocopy'),$pogo->id_kategori);   ?>            
          nama_kategori);  ?>
            $idx++;
           endforeach;
        ?>
    endWidget(); ?>