Yii Framework Forum: Update Gridview After Request Call - Yii Framework Forum

Jump to content

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

Update Gridview After Request Call Rate Topic: -----

#1 User is offline   PS86K 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 15-February 13

Posted 15 February 2013 - 12:53 PM

Hello,

I start work with yii framework a few days.

My problem is with ajax request. In my view I have a textfield that call controller via ajax when user key press... in controller I make a new search in database and i render the page. How I can refresh grid with new data?

My solution:

view

<?php echo $form->textField($model,'search', array('id'=>'search','maxlength'=>128, 'placeholder'=>'filter by name',
								'onkeyup' => CHtml::ajax(array(
									'url' => CController::createUrl('/test/ajax'),
									'type' => 'POST',
									'success'=>"function(string){console.log('done'); [b]$.fn.yiiGridView.update('gridTest', {data: $(this).serialize()}) -> I think this is not correct or is not necessary is my case <-[/b];}")))); ?>
	...
	
	$this->widget('bootstrap.widgets.TbGridView', array(
				'id'=>'gridTest',
				'ajaxUpdate'=>true,
				'type'=>'striped bordered condensed',		
				'dataProvider'=>$dataProvider, 
				'template'=>"{items}\n{pager}",
				'columns'=>array(
						array('name'=>'testId', 'header'=>$model->getAttributeLabel('testId')),
						array('name'=>'name', 'header'=>$model->getAttributeLabel('name'))
				)
		));


controller

public function actionIndex()
	{
		$model = new Test();	
		$data = $model->getTestCaseList();
		$this->render('index', array('model'=>$model, 'data'=>$data));
	}
	
	public function actionAjax()
	{
		if (isset($_POST['Test'])){
			$model = new Test();
			$model->setAttributes($_POST['Test']);
			
			$criteria = $model->search;
			
			$data = $model->getTestList($criteria);
			
			try with the two render's... the data is correct from the database
			//$this->renderPartial('index', array('model'=>$model, 'data'=>$data, false, true));
			echo $this->render('index', array('model'=>$model, 'data'=>$data));
		}
	}

0

#2 User is offline   EdoFre 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 24-February 11
  • Location:Groningen, The Netherlands

Posted 15 February 2013 - 02:55 PM

I think you made some errors in your ajax request, try this

<?php echo $form->textField($model,'search', array('id' => 'search', 'maxlength' => 128, 'placeholder' => 'filter by name',
    'onkeyup' => CHtml::ajax(array(
        'url' => CController::createUrl('/test/ajax'),
        'type' => 'POST',
        'data' => array('Test' =>'js:$("input#search").val()'),
        'success' => "js:$.fn.yiiGridView.update('gridTest-grid')"))));
?>


EDIT: I forgot to change id to Test, should be correct now
Moi,
0

#3 User is offline   PS86K 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 15-February 13

Posted 15 February 2013 - 07:05 PM

Tks EdoFre, but will still not working...

I don't need 'data' => array('Test' =>'js:$("input#search").val()'), because I have a form and the value of textfield appears fine in controller side. In debug I checked that everything is going as planned, this is, get the new values with the filter. But the grid is not updated :/
0

#4 User is offline   clapas 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 21-February 11

Posted 16 February 2013 - 11:09 AM

Hi PS86K,
what is exactly your issue?

Please, use the code snippet tags to place your code for others to better help you, because otherwise is very hard to read it.

Instead of writing your event handler code in the element's onkeyup attribute, I would rather use jQuery event methods,
f.e. $('#search').keyup(function() {...});

this assures you broader compatibility.

Also note that if the view is being rendered by the controller, you call
$this->createUrl(...)
instead of CController::createUrl(), as there is no such class method AFAIK.

When you give the column names, Grid widgets use the attribute label as the default header, so you do not need to write it explicitly.

Is the controller action being called via AJAX? Is it the same action that first renders the grid? Does it look fine when it is first rendered?

I see you are using an extension (TbGridView), if every other thing goes wrong I would check if Yii's CGridView works fine to diagnose whether it is a TbGridView issue what you are having...
0

#5 User is offline   PS86K 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 15-February 13

Posted 18 February 2013 - 04:44 AM

Hi clapas,

Is the controller action being called via AJAX? Is it the same action that first renders the grid? Does it look fine when it is first rendered?

Is the same action and the first time it's fine, the grid appear with data.

I see you are using an extension (TbGridView), if every other thing goes wrong I would check if Yii's CGridView works fine to diagnose whether it is a TbGridView issue what you are having...

the behavior is the same...

The problem is not in event keyup or on CController::createUrl(), because the call to controller is made when I press a key.

public function actionAjax()
	{
		if (isset($_POST['Test'])){
			$model = new Test();
			$model->setAttributes($_POST['Test']);
			
			$criteria = $model->searchName;
			
			$data = $model->getTestCaseList($criteria);

[color="#008000"]//this point returns the expected data according to the name in the search
the problem is in render... i have another way to pass data?
[/color]


			//$this->renderPartial('index', array('model'=>$model, 'data'=>$data, false, true));
			$this->render('index', array('model'=>$model, 'data'=>$data));
		}
	}


Tks
0

#6 User is offline   PS86K 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 15-February 13

Posted 18 February 2013 - 05:07 AM

Now I will work in other solution...

I send the value by json:

public function actionAjax()
	{
		if (isset($_POST['Test'])){
			$model = new Test();
			$model->setAttributes($_POST['Test']);
			
			$criteria = $model->searchName;
			
			$data = $model->getTestCaseList($criteria);
			//$this->renderPartial('index', array('model'=>$model, 'data'=>$data, false, true));
			//$this->render('index', array('model'=>$model, 'data'=>$data));

			header('Content-Type: application/json; charset="UTF-8"');
			echo json_encode($data);
		}
	}


In view, I get the values:

'success'=>"function(data){
												console.log(data); //I get the objects
$.fn.yiiGridView.update('gridTest', {data: $(this).serialize()}); //do nothing... :/
}")))); ?>


Now I need a way to update the grid with the values by javascript
0

#7 User is offline   PS86K 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 15-February 13

Posted 18 February 2013 - 10:43 AM

If I use the grid filters the grid update well...

I don't have solution for my issue yet :/
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