【问题标题】:Passing data from gridview button to modal window将数据从gridview按钮传递到模态窗口
【发布时间】:2013-08-03 21:46:15
【问题描述】:

我正在尝试将数据从 gridview 中的按钮传递到模式窗口。我需要传递记录的 ID,以便在模式窗口中提交表单后能够引用它。

我正在为此苦苦挣扎。首先,我需要能够将 ID 变量传递给模式,然后在单击提交按钮时进行 ajax 调用以在数据库中创建新记录。

网格视图

if(isset($results)){
    $this->widget('bootstrap.widgets.TbExtendedGridView', array(
        'id'=>'searchgrid',
        'fixedHeader' => true,
        'headerOffset' => 40, // 40px is the height of the main navigation at bootstrap
        'type'=>'condensed',
        'dataProvider'=>$results,
        'responsiveTable' => true,
        'template'=>"{items}",
        'columns'=>array(
            array('name'=>'title', 'header'=>'Name'),
            array('name'=>'city', 'header'=>'City'),
            array('name'=>'state', 'header'=>'State'),
            array('name'=>'leads', 'header'=>'Leads', 'value'=>'Parkslist::model()->leadRange($data["leads"])'),
            array('name'=>'pastbid', 'header'=>'Previous', 'value'=>'Parkslist::model()->pastBid($data["pasthighbid"])'),
            array('name'=>'currentbid', 'header'=>'Current', 'value'=>'Parkslist::model()->highBid($data["currenthighbid"], $data["secondhighbid"], $data["countcurrenthighbid"])'),
            array('name'=>'minimumbid', 'header'=>'Minimum', 'value'=>'Parkslist::model()->minimumBid($data["currenthighbid"], $data["secondhighbid"], $data["countcurrenthighbid"])'),
            array('name'=>'userhighbid', 'header'=>'Your Bid'),
            array('name'=>'placebid', 'header'=>'Bid', 'value'=>'CHtml::textField("bid" . $data["id"])', 'type'=>'raw'),
            array('name'=>'report', 'header'=>'Report',
                'value'=>function($data){
                    $this->widget('bootstrap.widgets.TbButton', array(
                        'label' => 'Click me',
                        'type' => 'primary',
                        'htmlOptions' => array(
                            'data-toggle' => 'modal',
                            'data-target' => '#myModal',
                            'data-id' => '$data["id"]',
                        ),
                    ));
                }
            ),
        ),
    ));
}

模态

<?php
$this->beginWidget('bootstrap.widgets.TbModal', array('id' => 'myModal')); ?>

    <div class="modal-header">
        <a class="close" data-dismiss="modal">&times;</a>
        <h4>Why should this park be removed?</h4>
    </div>
    <form>
    <div class="modal-body">
        <select>
            <option>Duplicate</option>
            <option>Closed</option>
        </select>
    </div>

    <div class="modal-footer">
        <?php $this->widget('bootstrap.widgets.TbButton', array(
        'type' => 'primary',
        'buttonType'=>'submit',
        'label' => 'Save changes',
        'url' => '#',
        'htmlOptions' => array('data-dismiss' => 'modal'),
    )); ?>
        <?php $this->widget('bootstrap.widgets.TbButton', array(
        'label' => 'Close',
        'url' => '#',
        'htmlOptions' => array('data-dismiss' => 'modal'),
    )); ?>
    </div>
    </form>
<?php $this->endWidget(); ?>

【问题讨论】:

    标签: php jquery twitter-bootstrap yii modal-dialog


    【解决方案1】:

    我能够得到这个工作。我认为可能有更好的解决方案,但这似乎可行。

    首先,在gridview 的按钮内部,我将按钮ID = 设置为记录的ID。接下来,我创建了一个名为 includeData 的 JavaScript 函数并包含了按钮 ID。

    按钮代码

    array('name'=>'report', 'header'=>'Report',
                    'value'=>function($data){
                        $this->widget('bootstrap.widgets.TbButton', array(
                            'label' => 'Click me',
                            'type' => 'primary',
                            'htmlOptions' => array(
                                'id'=>$data["id"],
                                'data-toggle' => 'modal',
                                'data-target' => '#myModal',
                                'data-id' => '$data["id"]',
                                'onClick' => 'includeData(this.id);'
                            ),
                        ));
                    }
                ),
    

    JS 代码

    <script type='text/javascript'>
    function includeData(parkid){
            $('#reportparkid').val(parkid);
    
    }
    </script>
    

    JS 函数只是将隐藏字段的值设置为等于 buttonid。我很想看到一些更好的方法来处理这个问题。

    谢谢

    【讨论】:

    • 我也有和你类似的情况。你能告诉我如何在modal-body中使用reportparkid。
    猜你喜欢
    • 2019-03-06
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多