0

我正在使用从 gridview 中选择的键传递(通过 post)一个数组,但我总是得到相同的 URL 错误:

POST http://localhost/cngrx/web/index.php/ponenciaresumen/[object%20Object] 404 (Not Found) 

似乎代码生成总是这种模式: path/myController/[object%20Object] 我设置的 url 无关紧要,代码生成总是相同的 url,这不是我需要的。我的代码是这样的:

<script>
  $(document).ready(function(){
    $('#MyButton').click(function(){

       var keys = $('#w1').yiiGridView('getSelectedRows');
        $.post({
           url: 'myController/myAction', 
           dataType: 'json',
           data: {keylist: keys}

        });
    });
  });
</script>

我已经尝试过绝对网址和亲戚之一,但没有。怎么会这样??

这是我在 myController 中的操作:

 public function actionMyAction(    ) {
    if (isset($_POST['keylist'])) {
        $keys = \yii\helpers\Json::decode($_POST['keylist']);

        // you will have the array of pk ids to process in $keys
        // perform batch action on these keys and return status
        // back to ajax call above
    }
}

感谢提前!

4

4 回答 4

0

我认为这会发生,因为您将网格放入 pjax 容器中?

如果你不使用 Pjax,你应该使用元素 id #w1 而不是 #grid。

Yii2 渲染这个脚本

    jQuery(文档).ready(函数(){
    jQuery('#modal-upload').modal({"show":false});
    jQuery('#w0').yiiGridView({"filterUrl":"/advanced/backend/web/index.php?r=state%2Findex","filterSelector":"#w0-filters input, #w0-filters select "});
    jQuery('#w1').yiiGridView({"filterUrl":"/advanced/backend/web/index.php?r=state%2Findex","filterSelector":"#w1-filters input, #w1-filters select "});
    jQuery('#w1').yiiGridView('setSelectionColumn', {"name":"selection[]","multiple":true,"checkAll":"selection_all"});
    jQuery('#w2').yiiGridView({"filterUrl":"/advanced/backend/web/index.php?r=state%2Findex","filterSelector":"#w2-filters input, #w2-filters select "});
    });

当我在我的 GridView 上使用 Pjax 时,我没有得到任何值或键但是当我使用元素 ID #w2 而不是#grid 时我得到了键。

你可以在你的视图上使用它



    $script = '
    jQuery(document).ready(function() {
      btnCheck  = $("#btn-check");
      btnCheck.click(function() {
        var keys = $("#w2").yiiGridView("getSelectedRows");
        alert(keys);
        $.ajax({
            type: "POST",
            url: "'.\yii\helpers\Url::to(['/controller/action']).'", 
            dataType: "json",
            data: {keylist: keys}
        });
      });
    });';
    $this->registerJs($script, \yii\web\View::POS_END);

于 2015-04-03T11:10:59.907 回答
0

See http://www.yiiframework.com/doc-2.0/yii-grid-checkboxcolumn.html

Need Class yii\grid\CheckboxColumn

var keys = $('#grid').yiiGridView('getSelectedRows');

于 2015-02-26T19:11:58.453 回答
0
$('#MyButton').click(function(){ 
    var keys = $('#w0').yiiGridView('getSelectedRows');
    $.post("index.php?r=myController/myAction", {keylist: keys}, function(result){
        $("span").html(result);
    });
}); 
于 2016-08-09T07:20:44.867 回答
0

查看您的代码源并检查复选框是否具有 id "w1" 或 "w0" .. 默认情况下对我来说它是 'w0' 并且我能够解决我的问题:

var keys = $('#w0').yiiGridView('getSelectedRows');
于 2016-07-19T14:23:26.647 回答