Yii2-在gridview中使用多个复选框

时间:2019-05-24 10:02:27

标签: javascript jquery ajax gridview yii2

我有一个gridview,其中还有一个复选框。

    <?= Html::a('Disconnect', ['dco'], ['class' => 'btn btn-success', 'id'=>'dco']) ?>
    <?= Html::a('Connect', ['rco'], ['class' => 'btn btn-info','id'=>'rco']) ?>

<?php Pjax::begin(); ?>
        <div class="pre-scrollable">
        <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [


        ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($d) {
            return ['value' => $d['msn']];
        }],


        'customer_id',       
        'dept_name:ntext',
        'sub_div_name',
        'division_name',

        'allowed_units',
        'msn',

        'units_consumed',
        [
            'label' => 'Disconnected',
            'attribute' => 'disconnected',
            'format'=>'raw',
            'contentOptions' => ['style'=>'text-align:center'],
            'value' => function($model){
                return $model->disconnected == 1 ? '<span class="glyphicon glyphicon-ok text-success"></span>' : '<span class="glyphicon glyphicon-remove text-danger"></span>';
            },
            'filter' => Html::activeDropDownList($searchModel, 'disconnected', [''=>'All','1'=>'Yes','0'=>'No'], ['class' => 'form-control']),
        ],


        'active_energy_total_m',


        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>
        </div>

我正在使用ajax将检查过的数据发送到控制器。

有两个按钮DisconnectConnect。在选中的复选框上,单击两个按钮中的任何一个之后,相应的所选项目将被连接或断开。

$DCOurl = Url::toRoute(['/hescolog/dco']);
$RCOurl = Url::toRoute(['/hescolog/rco']);
$(document).ready(function () {      
//DCO 
 $('#dco').on('click',function(e) {


       e.preventDefault();    
 var strValue = "";        
    $('input[name="selection[]"]:checked').each(function() {

    if(strValue!=="")
        {
        strValue = strValue + " , " + this.value;

        }
    else 
       strValue = this.value;     

});

   // alert(strValue);
$.ajax({
     url: '$DCOurl',
     type: 'POST',
     dataType: 'json',
     data: {data:strValue},         
     success: function(data) {
        alert(data);
     }
  });

 });

 $('#rco').on('click',function(e) {
e.preventDefault();    
 var strValue = "";        
    $('input[name="selection[]"]:checked').each(function() {

    if(strValue!=="")
        {
        strValue = strValue + " , " + this.value;

        }
    else 
       strValue = this.value;     

});
    // alert(strValue);
$.ajax({
     url: '$RCOurl',
     type: 'POST',
     dataType: 'json',
     data: {data:strValue},         
     success: function(data) {
        alert(data);
     }
  });
  });

  });

控制器

if(Yii::$app->request->isAjax && Yii::$app->request->post())
{
   $data = explode(',',$_POST['data']);
   foreach($data as $value)
   {
      //...... other code
   }
}

现在我面临的问题是,当我选中所有复选框并单击任何按钮时,只有第一个复选框选中了一项是连接还是断开连接。

尽管在检查控制器时``我可以看到两个复选框均已选中。

array(2) { [0]=> string(13) "002995000100 " [1]=> string(13) " 002992002018" }

enter image description here

更新1

针对这两个按钮,我有一个SOAP服务

 if(Yii::$app->request->isAjax && Yii::$app->request->isPost)
    {
        $data = explode(',',$_POST['data']);


        foreach($data as $value)
        {
            $ic++;
            $msn = $value;

            $xml_post_string = /** @lang text */
                '<soapenv:Envelope 
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
            xmlns:soap="http://soap.inf.hexing.cn">
            <soapenv:Header/>
            <soapenv:Body>
            <soap:doCommand>
            <!--Optional:-->
            <arg0><![CDATA[<?xml version="1.0" encoding="utf-8"?>
            <RequestMessage
            xmlns="http://iec.ch/TC57/2011/schema/message"
            xmlns:m="http://iec.ch/TC57/2011/EndDeviceControls#"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://iec.ch/TC57/2011/schema/message 
            Message.xsd">
            <Header>
            <Verb>create</Verb>
            <Noun>EndDeviceControls</Noun>
            <Revision>2.0</Revision>
            <Timestamp>2016-01-01T00:00:00+04:30</Timestamp>
            <Source>MDM</Source>
            <AsyncReplyFlag>true</AsyncReplyFlag>
      <ReplyAddress>http://ip:port/AmiWeb/services/Metering</ReplyAddress>
            <AckRequired>true</AckRequired>
            <User>
            <UserID>'.$userName.'</UserID>
            </User>
            <MessageID>83c643e6-85c5-43c0-9e0a-fa1deb469b72</MessageID>
            <CorrelationID>1001</CorrelationID>
            <Property>
            <Name>password</Name>
            <Value>'.$password.'</Value>
            </Property>
            <Property>
            <Name>timeout(m)</Name>
            <Value>30</Value>
            </Property>
            </Header>
            <Payload>
            <m:EndDeviceControls>
            <m:EndDeviceControl>
            <m:reason>Disconnect/Reconnect</m:reason>
            <m:EndDeviceControlType ref="3.0.211.23"/>
            <m:EndDevices>
            <m:mRID>'.$msn.'</m:mRID>
            <m:Names>
            <m:name>Disconnect</m:name>
            <m:NameType>
            <m:name>ControlType</m:name>
            </m:NameType>
            </m:Names>
            </m:EndDevices>
            </m:EndDeviceControl>
            </m:EndDeviceControls>
            </Payload>
            </RequestMessage>
            ]]></arg0>
            </soap:doCommand>
            </soapenv:Body>
            </soapenv:Envelope>';

            $headers = array(
                "Content-type: text/xml;charset=\"utf-8\"",
                "Accept: text/xml",
                "Cache-Control: no-cache",
                "Pragma: no-cache",
                "Content-length: ".strlen($xml_post_string),
            ); //SOAPAction: your op URL

            $url = $soapUrl;

            // PHP cURL  for https connection

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            //curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

            // converting
            $response = curl_exec($ch);
            curl_close($ch);
            $domd=new DOMDocument();


            if(!$domd->loadXML($response)){
                throw new \RuntimeException("failed to parse XML!");
            }
            $inner_xml=$domd->getElementsByTagName("return")->item(0)->textContent;
            if(!($domd2=@DOMDocument::loadXML($inner_xml))){
                throw new \RuntimeException("failed to parse inner_xml!");
            }
            $AsyncReplyFlag=$domd2->getElementsByTagName("AsyncReplyFlag")->item(0)->textContent;


            if ($AsyncReplyFlag =='true')
            {
                $ds = 1;

                $disconnected_at = date('Y-m-d H:i:s');

                try {
                    Yii::$app->db->createCommand(/** @lang text */
                        "update 
                     `accurate_mam`.`daily_log` 
                     set
                     `disconnected` = '$ds',
                     `diconnected_at` = '$disconnected_at', 
                     `reconnected_at` = NULL
                      where `msn` = '$msn' ;

                       ")->execute(); //update master table

                    Yii::$app->db->createCommand(/** @lang text */
                        "update 
                    `accurate_mam`.`log_disconnected` 
                    set

                    `disconnected_at` = '$disconnected_at' 
                     where `msn` = '$msn'")->execute();// update log disconnected table
                } catch (Exception $e) {
                } // updating the master table
            }


        }

    }

我不知道问题是什么,为什么我不能同时处理两个记录。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

因为您留下了评论here,所以我使用了beginForm方法:

在视图中

<div class="pre-scrollable">
  <?php Pjax::begin() ?>
  <?=Html::beginForm(['test'],'post');?>

    <?= Html::submitButton('Disconnect', ['class' => 'btn btn-success', 'name'=>'dco', 'value'=>'dco', 'id'=>'dco','style'=>'margin:0 10px;']) ?>
    <?= Html::submitButton('Connect', ['class' => 'btn btn-primary', 'name'=>'rco', 'value'=>'rco','id'=>'rco']) ?>

    <?= GridView::widget([
      'dataProvider' => $dataProvider,
      'filterModel' => $searchModel,
    'columns' => [

        ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($d) {
            return ['value' => $d['msn']];
        }],

        'customer_id',       
        'dept_name:ntext',
        'sub_div_name',
        'division_name',

        'allowed_units',
        'msn',

        'units_consumed',
        [
            'label' => 'Disconnected',
            'attribute' => 'disconnected',
            'format'=>'raw',
            'contentOptions' => ['style'=>'text-align:center'],
            'value' => function($model){
                return $model->disconnected == 1 ? '<span class="glyphicon glyphicon-ok text-success"></span>' : '<span class="glyphicon glyphicon-remove text-danger"></span>';
            },
            'filter' => Html::activeDropDownList($searchModel, 'disconnected', [''=>'All','1'=>'Yes','0'=>'No'], ['class' => 'form-control']),
        ],

        'active_energy_total_m',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

  <?= Html::endForm();?> 
  <?php Pjax::end() ?>
</div>

在控制器中

public function actionTest()
{
    $searchModel  = \Yii::createObject(\app\models\TestSearch::className());
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

  if ( Yii::$app->request->post() ) {

      $arr_select=(array)Yii::$app->request->post('selection');  //An array of selected items (checkbox)
      $dco=Yii::$app->request->post('dco');  // submitButton Disconnect
      $rco=Yii::$app->request->post('rco');  // submitButton Connect

      if ($dco === 'dco') {
           //code for Disconnect here ($query)
           // example:  $modelTest::updateAll(['disconnected' => 1], ['msn' => $arr_select]);
      } elseif ($rco == 'rco') {
           //code for Connect here ($query)
      }

    }

    return $this->render('test', [
        'dataProvider' => $dataProvider,
        'searchModel'  => $searchModel,
    ]);
}

您可以在控制器中优化代码。
当然,我认为如果您使用以下方法,效果会更好(一个按钮,然后...)。
您可以轻松决定控制器。

<?= Html::dropDownList('action','',[ 'Connect' =>'Connect','Disconnect' =>'Disconnect'],['prompt' => 'Please select','class'=> 'field-black input-sm']) ?>
<?= Html::submitButton('Apply', ['class' => 'btn btn-success','style'=>'margin:0 10px;']) ?>

您可以放置​​上面的代码,而不是两个submitButton。然后根据dropDownList值确定控制器。如下所示:

$_action=Yii::$app->request->post('action'); // dropDown
$arr_select=(array)Yii::$app->request->post('selection'); //selected items