2013-08-03 55 views
0

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

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

在GridView

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(); ?> 

回答

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的价值。我很想看到一些更好的方法来处理这个问题。

谢谢

+0

我也有类似的情况喜欢你。你能告诉我如何在模态体内使用reportparkid。 – prattom

相关问题