Get selected chekbox in GridView

how to get data from selected value in chekboxColumn ?

I’ve tried several ways but not succeeded.

i tried this forum too My link

this my code:




<?php echo CHtml::ajaxLink("Show Selected",

 $this->createUrl('/account/DoChacked'),

 array("type" => "post",

 "data" => "js:{ids:$.fn.yiiGridView.getSelection('gridcust')}",

"update" => "#output")); ?>


<div id="output"></div>



the problem is nothing data send to controller when clik Show Selected.

gridcust is id of CGridView




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

    'id'=>'gridcust',

    'dataProvider'=>$model,

    'columns'=>array(    

		array(

			  'class'=>'CCheckBoxColumn',	

			  'selectableRows'=>2,

			  'id'=>'chk',

		),   



Note that $.fn.yiiGridView.getSelection returns key values of SELECTED rows

$.fn.yiiGridView.getChecked returns key values of CHECKED rows

To understand the difference… read the docs for CCheckBoxColumn->selectableRows - http://www.yiiframework.com/doc/api/1.1/CCheckBoxColumn#selectableRows-detail

thanks for your response.

But there is still a problem because the data in gridview can not be displayed (blank) for the ids.

To clarify I attach a file from my test results with firebug. there be seen that the value of the ids ids [] while in the picture above I select 3 rows.

if I checked all the data even ids are not displayed at all. Presumably there is a solution to this problem.

just added that data like this that I get when I run it (I just check again turned out to have results like this):

no data on every row in the array.

You need to give more information if you want help…

How do you call the getChecked? Can you post your new code?

Note that getChecked has 2 parameters (gridID and columnID)… if you use firebug… you can put a breakpoint on getChecked and see if it gets the column values…

This code in view.php




<?php echo CHtml::ajaxLink("Show Selected",

 $this->createUrl('/account/DoChacked'),

 array("type" => "post",

 "data" => "js:{chk:$.fn.yiiGridView.getSelection('gridcust')}",

"update" => "#output")); ?>


<div id="output"></div>

<?php


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

    'dataProvider'=>$model,

	'id'=>'gridcust',

    'enableSorting'=>true,

	'selectableRows'=>2,

	

    'columns'=>array(    

		array(

			  'class'=>'CCheckBoxColumn',	

			  'id'=>'chk',

		),           

        array(

            'header'=>'Customer Name',

            'value'=>'$data[firstname]." ".$data[lastname]',

        ),

        

    ),

	

));



controller:




public function actionDoChacked(){

		//if(Yii::app()->request->isAjaxRequest){

	if(isset($_POST['chk'])) {

		print_r( $_POST['chk']);

                foreach($_POST['chk'] as $val) {

                        echo $val . '<br/>';

                }

              }


		//}

	}



I just tried this example and it works for me… so it’s something on your end…

First of all…

in this code you use the CGridView::selectableRows=>2… so this means that you can select more rows… and at the same time the checkbox will be checked… so this means that all rows that has a checkbox checked would be selected… but in your previous post in the picture… there are rows with checkbox checked… but not selected…

I cannot help you more than this… you need to debug a bit… use firebug…

BTW… if you are not… use the latest Yii version (1.1.8 ) … as there where some bug fixes about this…

yes, i changed code add




'selectableRows'=>2



i use the latest version - 1.1.8

Hi…

this work well if use ActiveRecord but not if use Sql. beacause i use CSqlDataProvider.

what do you think about this ? to make sure please try using csqldataProvider.

so, what the solution ?

setSelection and getChecked reads data from the rendered keys - http://www.yiiframew…nderKeys-detail

It seems that your don’t get those data…

To get this data, fetchKeys() is used… http://www.yiiframew…etchKeys-detail

Can you try that on your CSqlDataProvider to see if it returns the correct values…

It could be that you need to define the keyField - http://www.yiiframew…keyField-detail

ok, working now :), the problem occurs on this:




 $sql ="SELECT a.*,b.name as groupname,

   concat(a.firstname,' ',a.lastname) as custname FROM `customer` as a

   left join customer_group as b

  on a.customer_group_id = b.customer_group_id";


$dprov = new CSqlDataProvider($sql,array(

        'totalItemCount'=>$count,

	'keyField'=>'a.customer_id',



i changed to be:




$dprov = new CSqlDataProvider($sql,array(

        'totalItemCount'=>$count,

	'keyField'=>'customer_id',



I do not know, why can not use alias name.

Hi guys,

i follow the code above but cant get any value from CCheckBoxColumn. Here is my code. Is there any mistaken in my code?? pls help.

View code::


<?php

    echo CHtml::ajaxLink("Show Selected", $this->createUrl('default/getvalue'), array("type" => "post",

        "data" => "js:{ids:$.fn.yiiGridView.getSelection('detailgrid')}",

        "update" => "#output"));

    <div id="output">        </div>


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

            'id' => 'detailgrid', 

            'selectableRows' => '2',

            'dataProvider' => $dataProvider,

            'columns' => array(

                array(

                    'class' => 'CCheckBoxColumn',

                    'id' => 'ids',                  // ids represent what value inside???

                ),

                array(

                    'name' => 'id',

                ),

                array(

                    'name' => 'description',

                ),



Controller code::




 public function actionGetvalue()

    {

//        if(Yii::app()->request->isAjaxRequest)

//        {


        $model = new ProductForm('getRoutePlanning');

        print 124;                           // work till this line.

        if(isset($_POST['ids']))             // method $_POST can't get any value, code stop at here. 

        {

            print 123;

            print_r($_POST['ids']);

            foreach($_POST['ids'] as $val)

            {

                echo $val . '<br/>';

            }

        }

//        }

    }




Your controller is "default" ?

Anyway… you need to check with a tool like "firebug" if the ajax call is made… what values are sent with the call… what values are returned…

Hi mdomba,

what you mean?


Your controller is "default" ?


 $this->createUrl('default/getvalue')

i din install "firebug" to debug or checking. will install it. Thanks.

I was just checking if your controller is named "default" as you used ¨default/getvalue"

Hi mdomba,

yes… i put


public function actionGetvalue()

in default controller. Suppose


 $this->createUrl('default/getvalue')

is correct right***

hi guys,

How to retrieve and return more than one value in same rows when user click the row?? $.fn.yiiGridView.getSelection only c pass one value to js. Can keyField store array??

example :

‘keyField’=>array(‘id’,‘name’),


$.fn.yiiGridView.getSelection(target_id)

There is less example for method $.fn.yiiGridView.getChecked(id,column_id),

column_id is refer to what in CGridview??

Please helps.

Regards,

Jowen Jiun.

You are already asking for a second problem… did you solve the first problem? post here the solution… .then we can get to another problem…

keyField - check it’s type in the documentation - it’s string - so no array

The value returned from getSelected() or getChecked() is the value of the key of that row - that value does not need to be displayed on the grid… but you can always use jQuery to get any other value that is shown in the selected row.

for the getChecked() - columnId is the ID of the checkbox column - check it’s documentation - http://www.yiiframework.com/doc/api/1.1/CCheckBoxColumn#selectableRows-detail

Hi mdomba,

SOrry for late reply. i only want render another page when user click the selected row. i find this solution from other forums.

Hope useful for other ppls.


 'selectionChanged'=>"function(id){if ($.fn.yiiGridView.getSelection(id) != '') {window.location='" . Yii::app()->createUrl('client/displayDocument/id') . "' + '/' + $.fn.yiiGridView.getSelection(id);}}",

    ));



cgridview-selected-rows/and filter-on-gridview-from-related-model/

Hi mdomba,


but you can always use jQuery to get any other value that is shown in the selected row.



Can guide me some example to write on?? I newbie in jQuery… THANKS thanks. =)