2014-04-03 28 views
0

我是Yii的新手。我需要在CGrid视图中执行批量删除等批量操作。我通过在JavaScript中执行操作并使用AJAX调用执行操作。但我没有得到我所需要的。请帮我解决这个问题。 我的观点:yii Grid View中的批量操作

<?php $details=$details; 
    ?> 
    <h3><?php echo $name; ?></h3> 
    <div id="content" style="margin: 0"> 
    <?php $form=$this->beginWidget('CActiveForm', array(
     'id'=>'nimsoft-host-form', 
     //'enableAjaxValidation'=>true, 
      'enableClientValidation' => true, 
      'clientOptions' => array(
       'validateOnSubmit' => true, 
       'validateOnChange' => true, // allow client validation for every field 
      ), 
      'htmlOptions' => array('enctype' => 'multipart/form-data'), 
    )); ?> 
      <?php $this->endWidget();?> 
     <h3><?php echo htmlspecialchars($title); ?></h3> 
     <h3><?php echo $title; ?></h3> 
     <div style="padding: 10px;"> 
      <a href="<?php echo $this->createUrl('/Nimsoft/create?id='.$details->host_customer_id);?>" title="Create New Host" class="btn btn-primary circle_ok" style="text-decoration: none;" >Add New Host to Customer</a> 
      <a href="<?php echo $this->createUrl('/Nimsoft/Search_all?id='.$details->host_customer_id);?>" title="View All Hosts" class="btn btn-primary circle_ok" style="text-decoration: none;" >View All Hosts</a> 
      <a href="<?php echo $this->createUrl('/Nimsoft/Search_all',array('id'=>$details->host_customer_id,'isXLSDownload'=>1));?>" title="Export" class="btn btn-primary circle_ok" style="text-decoration: none;" >Export</a> 
      <a href="<?php echo $this->createUrl('/Nimsoft/Search_all',array('id'=>$details->host_customer_id,'isXLSDownload'=>2));?>" title="Export For All Customers" class="btn btn-primary circle_ok" style="text-decoration: none;" >Export For All Customers</a> 
      <div style="float:right"> 
          <?php 
          echo CHtml::link('Upload Customer CSV', array('/Nimsoft/uploadCustomers?id='.$details->host_customer_id), array(
           'onclick'=>'return hs.htmlExpand(this, { objectType: "iframe", wrapperClassName: "full-size", align: "center" })', 
           'class'=>'btn btn-primary', 
           'id'=>'upload_link', 
           )); 
          ?>       
         </div> 
      &nbsp;&nbsp;<?php //echo CHtml::Button('Export CSV', array('class' => 'btn btn-primary circle_ok exportCSV-button')); 
      echo '<form method="post" name="exportCSV-form" id="exportCSV-form" action="' . yii::app()->createUrl('/export/exportCSV') . '">'; 
        echo '<input type="hidden" name="filename" value="HostName">'; 
        $this->renderPartial('_export_list', array(
         'model' => $model, 
        )); 
        echo '</form>'; 
        ?> 



     <div class="innerLR"> 
      <div class="row-fluid"> 
       <?php 
       Yii::app()->clientScript->registerScript('search_inc', " 
         $('.exportCSV-button').click(function() { 
          $.fn.yiiListView.update('exportListView', { 
           data: $('#search-pm-form').serialize() + '&export=true', 
           complete: function() { 
            $('#exportCSV-form').submit(); 
           } 
          }); 
         }); 
         ");?> 
       <?php 
    echo CHtml::button("Add Date Entries",array("id"=>"butt")); 
    ?> 
    <?php 
    $obj=$this->widget('bootstrap.widgets.TbExtendedGridView', array(
     'id'=>'yiisession-grid',//'host_grid', 
     'dataProvider'=>$dataProvider, 
     'type' => 'striped bordered', 
     'columns'=>array(
            array(
            'id' => 'selectedIds', 
            'class' => 'CCheckBoxColumn', 
             'selectableRows'=>2, 
           ), 

        array(   // display 'create_time' using an expression 
           'name'=>'host_name', 
             'value'=>'$data->host_name', 
        ), 
        array(
           'name'=>'host_serviceid', 
           'value'=>'$data->host_serviceid', 
        ), 
            array(
           'name'=>'status', 
           'value'=>'$data->status', 
        ), 
        array(
             'class'=>'CButtonColumn', 
             'template'=>'{edit_date}{update}{delete}', 
             'htmlOptions'=>array('width'=>'95px'), 
             'buttons' => array(
               'edit_date' => array(//the name {reply} must be same 
               'label' => 'Add Date', // text label of the button 
                'url' => 'Yii::app()->createAbsoluteUrl("NimsoftHostsDetails/View", array("id"=>$data->host_id))', 
                 'imageUrl' => Yii::app()->baseUrl.'/images/icons/pencil.png', // image URL of the button. If not set or false, a text link is used, The image must be 16X16 pixels 

                ), 
               ),) 

        ), 


    )) 
      ; 

    ?> 

     </div> 

    <html lang="en"> 
    <head> 
     <meta charset="utf-8"> 

    </head> 
    <body> 

    </body> 
    </html> 

      <div class="separator bottom"></div> 
     </div> 
    </div> 
    <div class="clearfix"></div> 
    <div id="footer" class="hidden-print"> 
    <?php $this->renderPartial('application.views.layouts._footer_inc'); ?> 
    </div> 
    </div> 
    <script> 
      hs.preserveContent = false; 
      hs.dimmingOpacity = 0.75; 
      hs.zIndexCounter = 10000; 
      hs.showCredits = false; 

      // this will disable close when we click on other area than close button 
      hs.onDimmerClick = function() { 
       return false; 
      } 

     $('.exportCSV-button').click(function() { 
       $.fn.yiiListView.update('exportListView', { 
        data: $('#search-faq-form').serialize() + '&export=true', 
        complete: function() { 
         $('#exportCSV-form').submit(); 
        } 
       }); 
     }); 

    </script> 

    <script type="text/javascript"> 
     var gridId = "yiisession-grid"; 

     $(function(){ 
      $(document).on('click','#yiisession-grid a.bulk-action',function() { 
       return false; 
      }); 
     }); 
     function batchActions(values){ 
      var url = $(this).attr('Nimsoft/select_all'); 
      var ids = new Array(); 
      if(values.size()>0){ 
       values.each(function(idx){ 
        ids.push($(this).val()); 
       }); 
       $.ajax({ 
        type: "POST", 
        url: url, 
        data: {"ids":ids}, 
        dataType:'json', 
        success: function(resp){ 
         //alert("Data Saved: " + resp); 
         if(resp.status == "success"){ 
          $.fn.yiiGridView.update(gridId); 
         }else{ 
          alert(resp.msg); 
         } 
        } 
       }); 
      } 
     } 
    </script> 

    <?php 
    Yii::app()->clientScript->registerScript('edit_date',' 
    $("#butt").click(function(){ 
      var checked=$("#host_grid").yiiGridView("getChecked","host_grid_c1"); 
      var count=checked.length; 
      if(count>0 && confirm("Do you want to delete these "+count+" item(s)")) 
      { 
        $.ajax({ 
          data:{checked:checked}, 
          url:"'.CHtml::normalizeUrl(array('item/remove')).'", 
          success:function(data){$("#host_grid").yiiGridView("update",{});},    
        }); 
      } 
      }); 
    '); 


    ?> 

我的大动作代码是行不通的。请纠正我出错的地方。提前致谢。

+0

你能具体说明错误吗? – Naeem

+0

当我点击添加日期按钮无效时,页面仍然是 – user3318138

+0

控制台中的任何javascript错误? – davey

回答

0

答案yii2:触发一个JavaScript和发送这样的URL

Checkbox column in a gridview: 
    GridView::widget([ 
     'dataProvider' => $dataProvider,  
     'columns' => [ 
      ['class' => 'yii\grid\CheckboxColumn'], 
      'id'=>'grid', 
      'country', 
     ], 
    ]); 

按钮:index.php?r=mycontroller/bulk&action=1&ids=2,6,7,8

这是按钮:

<a href="#" onclick="bulkAction('p');"> 

这是JavaScript:

<script> 
    function bulkAction(a) { 
     var keys = $('#grid').yiiGridView('getSelectedRows'); 
     window.location.href='<?php echo Url::to(['mycontroller/bulk']); ?>&action='+a+'&ids='+keys.join(); 
    } 
</script>