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']) ){

		$inputField=$_GET['inputField'];

	}

	

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

		$attributes = $_GET['Barang'];

		$model->attributes=$attributes;

	}                                                                                                    

	

	$this->renderPartial('caribarang',array(

	'model'=>$model,

	'inputField'=>$inputField,

	),false,true);

}

....




 

//in /views/barang/caribarang.php

//this is what I am going to show 

<?php

$dp = $model->search();

      $dp->keyAttribute='id';

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

        'id'=>'barang-grid',

        'dataProvider'=>$dp,

        'filter'=>$model,

        'columns'=>array(

                array(

                  'name'=>'id',

                  'value'=>'$data->id',

                ),

                'nama',

                'nilai_satuan',

                'satuan',

        ),

));?>


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

<?php

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

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

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

            if ( p_sel != "") {

                $("#dialog_ground").html("");

                $("#'.$inputField.'").val(p_sel);

            }

        }'),array('id'=>'selectButton')); 

      

?>

</div>




	

//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'); ?>

		<?php 

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

                array('inputField'=>'PurchaseOrderDet_barang_id')),

                array('update'=>'#dialog_ground'),

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

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

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

	</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…