yii网格视图中的批量操作

时间:2014-04-03 12:58:25

标签: javascript php yii

我是Yii的新手。我需要在CGrid视图中进行批量删除等批量操作。我尝试在javascript中执行并使用AJAX调用执行操作。但我得不到我需要的东西。请帮我解决这个问题。 我的观点:

<?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",{});},              
                    });
            }
            });
    ');


    ?>

我的批量操作代码无效。请纠正我出错的地方。提前谢谢。

1 个答案:

答案 0 :(得分:0)

回答yii2:

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

触发javascript并发送如下网址的按钮: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>