Yii Framework Forum: Cgridview With Textfield - Yii Framework Forum

Jump to content

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

Cgridview With Textfield Rate Topic: -----

#1 User is offline   fiela 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 26-January 13

Posted 28 January 2013 - 04:17 AM

Hi... I'm pretty new to Yii and I have been creating an admin system. I got stuck with my cgridview, I want to have one textfield from a fuction in my model and a dropdownlist. I have looked around and can't really find the answer I'm looking for online. Think it's my controller that i am doing wrong. I want to save those fields to different table in my database.
DB tables used
Visitor_master (name,surname,mobile,email, invited by)
visitor_comment (id,comment,visitor_id,date)
visitor_status (id, description, visitor_id, date)

In my view... admin
/************************************************************************************************************/
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'menu-grid',
'enableAjaxValidation'=>true,
)); ?>

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'menu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'name',
'surname',
'mobile',
'email',
'invited_by',

array(
'header'=>'comment',
'value'=>'CHTML::textField($test->comment,$data->Comments(),array(\'width\'=>20,\'maxlength\'=>3))',
'type'=>'raw',
'htmlOptions'=>array('width'=>'20px'),
),

array(
'header' => 'Comment',
'type' => 'text',
'value' => ('$data->Comments()'),
),
array(
'header' => 'status',
'type' => 'text',
'value' => ('$data->Statuses()'),

),

array(
'class'=>'CButtonColumn',
'template'=>'{update}',

),
),
)); ?>
<div class="row buttons">
<?php echo CHtml::submitButton($test->isNewRecord ? 'Save' : 'Save'); ?>
<!--<button type="sumbit">Next Step</button>-->
</div>
<?php $this->endWidget(); ?>
/***********************************************************************************************************/
model : visitor_master
/************************************************************************************************************/

public function Comments()
{

$comments = array();
$count_array = count($this->visitorComments);
$v=0;
foreach($this->visitorComments as $comment){
$v++;
$comments[] = $comment->comment;
if ($count_array = $v){
//echo implode($comments);
}
return implode($comments);

}
}
// function to diplay all status for the visitors
public function Statuses()
{

$statuses = array();
$count_array = count($this->visitorStatuses);
$v=1;
foreach($this->visitorStatuses as $status){

if ($count_array = $v){
$statuses[] = $status->description;
}
return implode("\n", $statuses);
$v++;
}
}

/**
* @return string the associated database table name
*/
public function tableName()
{
return 'visitor_master';
}

/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, surname, mobile, email, invited_by, leader_id', 'required'),
array('name, surname, invited_by', 'length', 'max'=>30),
array('mobile', 'length', 'max'=>12),
array('email', 'length', 'max'=>40),
array('leader_id', 'length', 'max'=>20),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, surname, mobile, email, invited_by, leader_id', 'safe', 'on'=>'search'),
);
}

/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(

'visitorComments' => array( self::HAS_MANY, 'visitor_comment', 'visitor_id'),
//'order'=>'VisitorComments.comment DESC'),
'visitorEntries' => array(self::HAS_MANY, 'visitor_entry', 'visitor_id'),
'leader' => array(self::BELONGS_TO, 'leader_master', 'leader_id'),
'visitorStatuses' => array(self::HAS_MANY, 'visitor_status', 'visitor_id'),

);
}

/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'surname' => 'Surname',
'mobile' => 'Mobile',
'email' => 'Email',
'invited_by' => 'Invited By',
'leader_id' => 'Leader',
);
}

/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$action = $_GET['id'];

if (!isset($_GET['id'])){
$action = Yii::app()->session['min_leader_id'];
}
//if ($action = 0){
//$action = Yii::app()->session['leader_id'];
//}

$criteria=new CDbCriteria;

$criteria->compare('id',$this->id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('surname',$this->surname,true);
$criteria->compare('mobile',$this->mobile,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('invited_by',$this->invited_by,true);
$criteria->compare('leader_id',$this->leader_id,true);

$criteria->addCondition("leader_id = $action");


return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
/*************************************************************************************************************/
Controller

public function actionAdmin()
{
$model=new visitor_master('search');
$test = new visitor_comment;
//if(Yii::app()->request->isPostRequest)
//{

$fakeid = 82;
$date=date('y-m-d');


if(isset($_GET['visitor_master'],$_GET['visitor_comment'] )){


$test->date=$date;
$test->visitor_id=$fakeid;
$test->comment='Hello';

$model->attributes=$_GET['visitor_master'];
$test->attributes=$_GET['visitor_comment'];

// $model->save();
$test->save();
//}
}


// $model->unsetAttributes(); // clear any default values

$this->render('admin',array(
'model'=>$model,
'test'=>$test,
));

}

Please if someone can help me, have been stuck on this for age now
0

#2 User is offline   Shahzad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 16-March 12
  • Location:Islamabad, Pakistan

Posted 28 January 2013 - 06:03 AM

Please elaborate, what do you want to do? the problem is not clear. What actions you want to perform and how (button click etc)?

Always paste your code in code block (that is <>), so that it can be read easily.
0

#3 User is offline   fiela 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 26-January 13

Posted 28 January 2013 - 07:32 AM

Yeah sorry first time that I used the forum....

Basically what needs to happen when you click save it must take all the textfields data that has been entered and save them all into the database the same will happen for the status. the function that I created in the model finds the comment that was inserted in the db and displays that in cgridview that bit works fine, but I cannot save any data into the db.

This is in my view file... few thing commented out... as I was trying to figure it out

<h1>Visitor</h1>

<?php //echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
	'model'=>$model,
	
	
)); ?>
</div><!-- search-form -->
<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'visitor_master-grid',
        'enableAjaxValidation'=>true,
)); ?>

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'visitor_master-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'name',
		'surname',
		'mobile',
		'email',
		'invited_by',
		
			array(
                        'name'=>'comment',
						'type'=>'raw',
                        'value'=>'CHTML::textField($test->comment[$data->id],$data->Comments(),array(\'width\'=>20,\'maxlength\'=>20))',
                        'filter'=>'visitor_comment::model()->findAllAttributes(null, true))',
                ),				
		array(
		'header'=>'comment',
        'value'=>'CHTML::textField($test->comment,$data->Comments(),array(\'width\'=>20,\'maxlength\'=>20))',
        'type'=>'raw',
        'htmlOptions'=>array('width'=>'20px'),
      ), 
	  array(
		'header'=>'Status',
        'value'=>'CHTML::dropdownList($status->status,$data->                           statuses(),array("",Member,Regular,Visited,Contacted,Not_Interested,To-Followup))',
        'type'=>'raw',
        'htmlOptions'=>array('width'=>'20px'),
      ),
	  /*
		array(
                        'header' => 'Comment',
                        'type' => 'text',
                        'value' => ('$data->Comments()'),
         ),
		array(
                        'header' => 'status',
                        'type' => 'text',
                        'value' => ('$data->Statuses()'),
						
         ),*/
		
			array(
			'class'=>'CButtonColumn',
			'template'=>'{update}',
				
		),
	),
)); ?>
<div class="row buttons">
   		<?php echo CHtml::submitButton($model->isNewRecord ? 'Save' : 'Save'); ?>
		<!--<button type="sumbit">Next Step</button>-->
	</div>
    
<?php $this->endWidget(); ?>
<?php // echo CHtml::endForm(); ?>




and my controllers code

public function actionAdmin()
	{
		
		
		//$model=$this->loadModel($id);
		$model=new visitor_master('search');
		$test = new visitor_comment;
		$status = new visitor_status;
		$date=date('y-m-d');
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
if(isset($_GET['visitor_master'] )){
		if(isset($_POST['visitor_comment'], $_POST['visitor_status']))
		{
			$test->date=$date;
			//$model->attributes=$_POST['visitor_master'];
			$model->attributes=$_GET['visitor_master'];
			$test->attributes=$_POST['visitor_comment'];
			
			$status->attributes=$_POST['visitor_status'];
			
			
			$status->date=$date;
			
			
			
			
			$model->save();
			$test->save();
			$status->save();
			
			
			
				$this->redirect(array('admin'/*,'id'=>$model->id*/));
		}
}

		$this->render('admin',array(
			'model'=>$model,
			'test'=>$test,
			'status'=>$status,
		));
	}

0

#4 User is offline   Shahzad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 16-March 12
  • Location:Islamabad, Pakistan

Posted 29 January 2013 - 04:28 AM

This link should be helpful to solve your problem:-

http://www.yiiframew...in-admin-panel/
0

#5 User is offline   fiela 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 26-January 13

Posted 29 January 2013 - 06:54 AM

View PostShahzad, on 29 January 2013 - 04:28 AM, said:

This link should be helpful to solve your problem:-

http://www.yiiframew...in-admin-panel/


I did try to follow that example, but I have no idea what to do in my controller file
0

#6 User is offline   Shahzad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 16-March 12
  • Location:Islamabad, Pakistan

Posted 30 January 2013 - 01:58 AM

View Postfiela, on 29 January 2013 - 06:54 AM, said:

I did try to follow that example, but I have no idea what to do in my controller file


Hmm, but your code does not show that you followed it correctly. For example,
i) you have used submitButton, wiki article used ajaxSubmitButton
ii) You dont have scripts in you view file, for example reloadgrid and preceeding
iii) You have not used foreach loop to save each row's data into models, like in the wiki (in the controller action)

try to follow it again, i have used this approach in my project successfully :)
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