2013-08-03 58 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

相關問題