如何从yii2中的gridview获取所选行

时间:2017-11-14 11:01:39

标签: gridview yii2

我正在使用yii2高级框架。公司名称有一个select2下拉列表。  在view / companies / index.php

Select2::widget([
    'id' => 'front_companies_select',
    'name' => 'state_10',
    'data' => $companyNameList,
    'options' => [
        'placeholder' => 'Select ...',
        'multiple' => true
    ],
]);

和按钮获取信息

<?= Html::SubmitButton( 'Get info', [ 'class' => 'btn btn-success' , 'id' =>'info']) ?>

这里是myjsfile

  $("#getinfo").click(function(){
    var selval = '';
    $("#front_companies_select option:selected").each(function () {
     var $this = $(this);
     if ($this.length) {
      var selText = $this.val();
      selval = selText+','+selval;
    }
  });
    var searchurl = 'index.php?r=companies&id'+selval;
    window.location.replace(searchurl);
    alert (selval); //here I can get id of selected rows but I want complete row
  });

我想显示所选公司的gridview行。 怎么解决这个?

2 个答案:

答案 0 :(得分:1)

你可以在#info的click jquery函数上写一下,然后获取select2 box用户选择的值,之后你可以获得当前的url绑定选择的值到url那个url传递seachmodel值

$('#info'),on('click',function(){
var selval = '';
$("#front_companies_select option:selected").each(function () {
   var $this = $(this);
   if ($this.length) {
    var selText = $this.val();
    selval = selText+','+selval;
   }
});
var searchurl = 'someurl&searchvalues'+selval;
window.location.replace(searchurl);

});

答案 1 :(得分:0)

在_form中使用它只需放置你的字段和表: -

&#13;
&#13;
     <?= $form->field($model, 'your_field')->widget(Select2::classname(), [
        'data' => arrayhelper::map(your_table::find()->all(),'id','your_field'),
        'language' => 'en',
        'options' => ['placeholder' => 'Select a state ...'],
        'pluginOptions' => [
        'allowClear' => true
        ],
    ]); ?>
&#13;
&#13;
&#13;