如何在GridView中仅发送一行POST

时间:2019-06-17 05:35:16

标签: php gridview yii2

我正在创建表,客户可以在其中对记录进行排序。如何仅发布整个表中的单行?

我已经尝试过将整个GridView表包装成表格,结果是当我提交时,整个表都被POST了,而不是特定的记录。

这是GridView:

        <?=Html::beginForm(['list-profile/resort'], 'post', ['class' => 'form-inline', 'name' => 'resort-channel-list']);?>
        <?=GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'pager' => [
                'firstPageLabel' => Yii::t('app', 'First'),
                'lastPageLabel' => Yii::t('app', 'Last'),
            ],
            'columns' => [
                [
                    'label'=>'#',
                    'attribute' => 'channel.sort',
                    'value' => 'channel.sort',
                ],
                [
                    'label'=>'Sort No.',
                    'attribute'=>'no',
                    'format' => 'raw',
                    'value'=> function ($data) {
                        return Html::textInput("sort",$data->no,array("style"=>"width:40px;"));
                    },
                ],
                [
                    'label'=>'Action',
                    'attribute'=>'no',
                    'format' => 'raw',
                    'value'=> function ($data) {
                        return Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']);
                    },
                ],
                [
                    'attribute' => 'channel.name',
                    'format' => 'raw',
                    'value' => function ($data) {
                        return Html::a($data->getChannelName($data->channel_id), ['update', 'id' => $data->id, 'name' => Yii::$app->request->get('name')], ['data-pjax' => 0]);
                    },
                ],
                 'create_time',
                 'update_time',
                ['class' => 'yii\grid\ActionColumn',
                    'template' => '{remove} | {save}',
                    'buttons' => [
                        'remove' => function ($url, $model, $key) {
                            return Html::a('<span class="glyphicon glyphicon-remove text-danger"></span>', ['list-profile/index', 'rem_id' => $model->id, 'name' => Yii::$app->request->get('name')], ['title' => Yii::t('app', 'Remove from profile'),
                             ]
                            );
                        },
                    ],
                    'contentOptions' => ['style' => 'min-width: 80px;text-align: center;'],
                ],
            ]
        ]);?>
        <?=Html::endForm();?>

以下是表格的样子:

Picture of table

2 个答案:

答案 0 :(得分:1)

有一个data-key属性,可将行与记录中的记录相关联 Gridview,您可以使用它来定位该特定行并收集输入。

然后为什么要在Data列中分配按钮,您应该将其放置在Action列中。

要做的事

  • 您应该首先将Html::submitButton()更改为普通按钮,并添加类submitdata-key属性,然后将其移至Action列。 / p>

    return Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']);
    

    return Html::button(Yii::t('app', 'Save'), ['class' => 'btn btn-success submit' , 'data-key' => $key]);
    
  • 为我正在使用GridView的{​​{1}}分配一个ID。

  • 将我们创建的新按钮从数据列(代码)移至操作列

    如下所示

    mygrid
  • 在视图顶部添加以下脚本

    [
        'class' => 'yii\grid\ActionColumn',
        'template' => '{remove} | {save} {submitRow}',
        'buttons' => [
            'submitRow' => function ($url, $model, $key) {
                return Html::button('Submit', ['class' => 'btn btn-success submit', 'data-key' => $key]);
            },
            'remove' => function ($url, $model, $key) {
                return Html::a(
                    '<span class="glyphicon glyphicon-remove text-danger"></span>',
                    ['list-profile/index', 'rem_id' => $model->id, 'name' => Yii::$app->request->get('name')],
                    [
                        'title' => Yii::t('app', 'Remove from profile'),
                    ]
                );
            },
        ],
        'contentOptions' => ['style' => 'min-width: 80px;text-align: center;'],
    ],
    

完成上述操作后,您的数据将位于$js = <<<JS $('.submit').click(function(){ //get the row key from target button var rowNum=$(this).data('key'); var data={}; //add the id to the collection for update data["id"]=rowNum; //collect the inputs data from the td inside the row $('#mygrid table').find('[data-key="'+rowNum+'"]').find('td').each(function(){ $(this).find('input').each(function(){ data[$(this).attr('name')]=$(this).val(); }); }); //send an ajax call $.ajax({ url:'list-profile/resort', method:'post', data:data, success:function(response){ alert(response); }, error: function (XHR, status, error) { alert("Oops! Status " + status + " returned with error "+error); } }); }); JS; $this->registerJs($js, \yii\web\View::POS_END); ?> 数组下,您可以像这样

post

答案 1 :(得分:0)

您可以删除beginForm。并执行以下操作:

1 -对其进行更改:

return Html::textInput("sort",$data->no,array("style"=>"width:40px;"));

收件人:

return Html::textInput($data->no,$data->no,array("style"=>"width:40px;"));

2 -对其进行更改:

return Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']);

收件人:

return Html::a('list-profile/resort', 'send', ['class' => 'btn btn-success','id' => $data->no,]);

然后添加代码:

<?php
$script = <<< JS
$(document).on('click', 'a', function(){
    event.preventDefault();
    var key =$(this).attr('no');
    var dat = $('input[name='+key+']').val();
    $.post("list-profile/resort",
    {
      'textinput' : dat
    },
    function(data,status){
        alert("Data: " + data + "Status: " + status);
    });
});
JS;
$this->registerJs($script);
?>

如果要通过Ajax进行操作:

 $.ajax({
url: 'list-profile/resort',
 type: 'POST',
data: {'textinput' : dat },
 success:function(result){
 alert(dat)
 },
 error: function(){
 alert('Error!')
 }
 });

是否要以beginForm开头? (请勿删除beginFormsubmitButton

return Html::textInput($data->no,$data->no,array("style"=>"width:40px;"));

return Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success','id' => $data->no]);


$('form').find(':submit').on('click', function(event) {
    // Prevent default anchor click behavior
    event.preventDefault();

    var key =$(this).attr('no');
    var dat = $('input[name='+key+']').val();
        $.post("list-profile/resort",
        {
          'textinput' : dat
        },
        function(data,status){
            alert("Data: " + data + "Status: " + status);
        });
});

对于URL,您还可以在JS中使用以下代码:

 var url= $(this).attr('href');