【问题标题】:jQuery DataTable updates the first rows of the table not the selected onesjQuery DataTable 更新表的第一行而不是选定的行
【发布时间】:2015-07-01 12:32:13
【问题描述】:

我有一个 Laravel 应用程序,我在其中使用 jQuery DataTables 来更新一些记录。我从数据库中检索这些记录,我必须确认它们。在 DataTable 上,我在最后一列有一个复选框。

现在我可以在选中复选框时更新数据库中的记录,但更新的记录与复选框行上的记录不对应。这是更新前的:

这是在记录更新后:

如您所见,更新的是月份 = 3 的记录,而不是月份 = 4 的记录。

这是控制器上的代码:

public function update(Request $request)
{
    $user = Auth::user();
    $is_validated = $request->get('is_validated');
    $rec_id = $request->get('id');
    $reports = [];
    foreach($is_validated as $key => $valid){
        $id = $rec_id[$key];
        if(!empty($valid)){
            $reports[] = DB::table('reports')->where('reports.id', $id)->update(['is_validated' => 1, 'validated_by' => $user->id]);
        }else{
            $reports[] = DB::table('reports')->where('reports.id', $id)->update(['is_validated' => 0, 'validated_by' => $user->id]);
        }
    }
}

感谢所有帮助或提示。

编辑

我尝试对列重新排序,并且数据表上的第一列已更新,无论它们的 id 是什么。

编辑 2

这是设置请求参数的javascript。

columns: [
            {data: 'id', name: 'reports.id'},
            {data: 'month', name: 'reports.month'},
            {data: 'value', name: 'reports.value'},
            {data: 'comment', name: 'reports.comment'},
            {data: 'is_validated', name: 'reports.is_validated'},
        ],
        "columnDefs": [ {
            "targets": [0],

            "render": function ( data, type, full, meta ) {
                return '<input type="text" name="id[]" data-name"id-' + meta.row + '" value="'+data+'" class="hidden"/>';
            }
        },{
            "targets": [4],

            "render": function ( data, type, full, meta ) {
                return '<input name="is_validated[]" data-name"is_validated-' + meta.row + '" class="checkbox" type="checkbox" unchecked/>';
            }
        }
    ]

【问题讨论】:

  • 检查$is_validated 是否给出了正确的选择ID。请提及是否应该执行 if 或 else 阻塞?
  • $is_validated 提供复选框输入。但是$rec_id 确实给出了正确的 ID。是的,它应该执行 if/else 块
  • 请展示您的 JavaScript 代码,演示您如何设置 idis_validated 请求参数。
  • @Gyrocode.com 我更新了问题

标签: php laravel datatables


【解决方案1】:

您的代码存在多个问题,但主要是浏览器为所有文本框发送id[] 参数,但只有那些包含检查值的is_validated[] 参数,因此两个数组的长度不同,您计算的 ID 不正确在你的 PHP 中。

相反,您需要为每个复选框设置一个与记录 ID 相同的值。在服务器端,您需要检查id[]is_validated[] 中是否存在这两个ID;如果是,则选中复选框,否则不选中。

修正后的代码如下:

JavaScript:

columns: [
   {
      data: 'id', 
      name: 'reports.id',
      "render": function ( data, type, full, meta ) {
         return '<input type="text" name="id[]" data-name="id-' + meta.row + '" value="'+data+'" class="hidden"/>';
      }
   },
   {data: 'month', name: 'reports.month'},
   {data: 'value', name: 'reports.value'},
   {data: 'comment', name: 'reports.comment'},
   {
      data: 'is_validated', 
      name: 'reports.is_validated',
      "render": function ( data, type, full, meta ) {
         return '<input name="is_validated[]" data-name="is_validated-' + meta.row + '" class="checkbox" type="checkbox" value="' + full['id'] + '"/>';
      }
   },
],

PHP:

public function update(Request $request)
{
    $user = Auth::user();
    $is_validated = $request->get('is_validated');
    $rec_id = $request->get('id');
    $reports = [];
    foreach($rec_id as $id){
        if(in_array($id, $is_validated)){
            $reports[] = DB::table('reports')->where('reports.id', $id)->update(['is_validated' => 1, 'validated_by' => $user->id]);
        } else {
            $reports[] = DB::table('reports')->where('reports.id', $id)->update(['is_validated' => 0, 'validated_by' => $user->id]);
        }
    }
}

【讨论】:

  • 实际代码为Uncaught TypeError: Cannot read property 'id' of undefined。我在value=' + full['reports']['id'] + ' 和控制器方法中对其进行了一些更改,但现在似乎 if 语句中没有满足条件,即使我选中了该框。在检查 chrome 检查元素后,我看到 is_validated 输入有一个未定义的值。
猜你喜欢
  • 2012-11-07
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 2016-01-30
  • 2018-10-29
相关资源
最近更新 更多