【问题标题】:append new rows to gridview in yii2在 yii2 中将新行添加到 gridview
【发布时间】:2017-09-28 12:07:18
【问题描述】:

我需要将一些新行附加到由 yii2 的 gridView 创建的表的顶部。 gridview 的数据由 activeDataProvider 提供。

<div class="col-sm-12" id="dataDiv">              
<?php
  echo GridView::widget([
    'dataProvider' => $model['dataProvider'],
    'columns' => [
      'id',
      'customer_name',
      'customer_lastname',
      'customer_mobile',
      'created_at:date'
    ]
  ]);
?>
</div>

我可以通过 ajax 调用控制器方法获取新行,但我不知道如何通过 ajax (jquery) 将它们附加到表中。

任何想法将不胜感激!

【问题讨论】:

  • Example - 这里有一些 JS 代码可以帮助你。

标签: jquery gridview yii2


【解决方案1】:

我知道这个答案来晚了,但对于其他想要未来解决方案的人来说:

首先,给你的 GridView 一个 id 以便我们可以在 html 中使用它:

GridView::widget([
    'id' => 'some-list',
    ...
]);

假设您直接从控制器获取新行作为响应,然后您想使用 jQuery 将列表附加到顶部:

$.post('your-saving-url', {someData:'you-want-to-save'}, function(response) {
     // append the new row to the top of our table
     $('#some-list').find('tbody').prepend(response);
});

几秒钟后进行 pjax 刷新以确保表结构仍然正确并修复分页是有意义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2017-11-08
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多