javascript - 如何在 Laravel 5.2 中获取 AJAX 表记录

标签 javascript php jquery ajax laravel

当我单击按钮显示更新的表和新内容时,如何在 AJAX Laravel 上下文中显示所有记录。但此代码对我不起作用。

我的 table :

<table class="table table-condensed text-right">
                <thead class="">
                <tr>
                    <th class="text-right">remove</th>
                    <th class="text-right">edit</th>
                    <th class="text-right">name</th>
                    <th class="text-right">#</th>
                </tr>
                </thead>
                <tbody>
                @foreach($category as $cat)
                    <tr id="append">
                        <td><a id="destroy" href="{{action('categoryController@destroy', [$cat->id])}}"><span
                                        class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td>
                        <td><a href="{{action('categoryController@update',[$cat->id])}}"><span
                                        class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
                        <td><a href="{{action('categoryController@show', [$cat->id])}}">{{$cat->category}}</a>
                        </td>
                        <th class="text-right">{{$cat->id}}</th>
                    </tr>
                @endforeach
                </tbody>
            </table>

我的 Controller :

public function index2($request)
{
    $category = Category::table($request->all())->get();
    return ['id'=>$category];
}

我的ajax代码:

                    $.ajax({
                    type: 'get',
                    url: '{!! URL::route('index2') !!}',
                    data: data,
                    success: function(data){
                        alert(data);
                    }
                })

在 Controller 中使用此代码而不使用ajax:

    public function index()
{
    $category = Category::all();
    return view('admin.category', compact('category'));
}

最佳答案

这种情况下你不能使用@foreach,因为你的数据是js,看一下这个例子https://gist.github.com/triceam/3407134

我的包裹里也有 https://github.com/marcosrjjunior/lvlgrid

关于javascript - 如何在 Laravel 5.2 中获取 AJAX 表记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35829981/

相关文章:

php - 为什么以下代码在 PHP 中上传文件时会抛出 undefined index ?

javascript - 遍历多个 div 并将信息提取到数组中?

javascript - MagicSuggest:获取模糊选择的长度

php - 转换 { key : Value } Object Into Standard Variables with jQuery

javascript - 使用此 HTML/Javascript/CSS 生成随机字符串?

javascript - 仅检测移动设备上的 Chrome

javascript - MYSQL 数据插入查询不起作用

javascript - 动态创建按钮时如何交变量?

javascript - 名称值对列表,无法增加名称宽度数字

php - Query Builder 更新语句加上当前值