Why my CGridView sometimes cannot be selected?

I use CGridView to view with ajax, but I don’t know why sometimes the CGridView cannot be selected. My code is like this.

//in /controllers/BarangController.php



public function actionCariBarang(){

	$model=new Barang('search');

	if( isset($_GET['inputField']) ){




	if(isset($_GET['Barang'])) {

		$attributes = $_GET['Barang'];











//in /views/barang/caribarang.php

//this is what I am going to show 


$dp = $model->search();


      $this->widget('zii.widgets.grid.CGridView', array(














<div class="row button" align="right">


echo CHtml::ajaxButton('Select','',

        array('success'=>'js:function() {

            var p_sel = $.fn.yiiGridView.getSelection("barang-grid");

            if ( p_sel != "") {









//in my /views/purchaseOrderDet/_form.php

//at here I am calling the ajax which will call the actionCariBarang() and show the viewbarang.php 


        <div class="row">

		<?php echo $form->labelEx($model,'barang_id'); ?>

		<?php echo $form->textField($model,'barang_id'); ?>


		echo CHtml::ajaxLink('Pilih Barang', $this->createUrl('/Barang/cariBarang',



                array('id'=>'senderDialog',)); ?> 

		<?php echo $form->error($model,'barang_id'); ?>

		<div id="dialog_ground" ></div>



At last I figured out the source of problem. There is problem in my code.

Everytime I click the ajaxLink, it will call actionCariBarang() which will show view file caribarang.php. What I later realised is, everytime I call CGridView, it will include the javascript once again. If I click ajaxlink once again, it will load the CGridView again which in turn load the javascript once again. Meaning there are more than one same javascripts running already. Meaning if I click the CGridView once, it will run the script twice!!, causing the "selected" class in the div disappear immediately after been added.

So, later in order to prevent it load more than once, I load the CGridView once, and then hide it using css. By this way, if I want to show the CGridView again, I just need to change the css via javascript. I think this is the only solution that I can use now…