简体   繁体   English

Yii CCheckBoxColumn如何获取CGridView上已检查列的ID

[英]Yii CCheckBoxColumn how can get id of checked column on a CGridView

I have CGridView with CCheckBoxColum : 我有CGridViewCCheckBoxColum

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'documento-financeiro-grid',
    'dataProvider' => $provider,
    'filter' => null,
    'columns' => array(
        array(
            'id' => 'autoId',
            'class' =>'CCheckBoxColumn',
            'selectableRows' => '50',
            'checked'=>'alert('
        ),
        array(
            'name' => 'id',
            'htmlOptions' => array('style' => 'width: 50px;'),
            'headerHtmlOptions' => array('style' => 'width: 50px;'),
        ),
        array(
            'name' => 'numero',
            'htmlOptions' => array('style' => 'width: 60px;'),
            'headerHtmlOptions' => array('style' => 'width: 60px;'),
        ),
 ),
)); 

And I want show a dialog window when a row was checked, but I need to know what id of row was checked, someone can help me? 我想在选中某行时显示一个对话框窗口,但是我需要知道选中了哪一行ID,有人可以帮助我吗?

I'll try to write what your code would look like. 我将尝试编写您的代码。

You have to write some javascript that listens on the change event for your checkboxes (this event gets triggered when checkboxes get clicked on). 您必须编写一些JavaScript,以侦听您的复选框的change事件(单击复选框时触发此事件)。

I've used a jquery selector to select the checkboxes based on what column they are in (this is because your grid may have other columns with checkboxes - we don't want to touch those). 我使用了一个jQuery选择器,根据它们所在的列来选择复选框(这是因为您的网格可能还有其他带有复选框的列,我们不想触碰这些复选框)。

In my example code, I've assumed your checkboxes have ID's that begin with documento_financeiro-grid_c0 (this is the ID Yii gives them by default). 在我的示例代码中,我假设您的复选框具有以documento_financeiro-grid_c0开头的ID(这是Yii默认documento_financeiro-grid_c0提供的ID)。 Please change that if this isn't the case. 如果不是这种情况,请更改。

The row id is stored in the variable item_id and variable item_checked contains true if your checkbox was checked and false if it was unchecked. 行ID存储在变量item_id ,如果选中了复选框,则变量item_checked包含true如果未选中,则false

$('#documento-financeiro-grid input[id^=documento-financeiro-grid_c0]').change(function() {
    var item_id = $(this).val();
    var item_checked = $(this).prop('checked');

    if (item_id === "1") {
        console.log('top checkbox was clicked on');
    } else {
        console.log('ID was'+item_id);
    }
});

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM