将AJAX输出附加到HTML表

时间:2018-02-15 06:41:54

标签: javascript jquery ajax laravel

我完成了我的研究,似乎无法找到合适的答案。

这是问题所在。首先,这个问题可能很广泛。我是ajax的新手。我正在使用laravel,我刚刚创建了一个控制器,它处理两个日期whereBetween以查找结果集,而ajax正在处理get请求。我有两个问题。

首先,我不知道如何附加到我现有的表格。另一个问题有点复杂。目前我正在使用碳diffForHumans()来显示所花费的时间。

还使用if else语句。这是我当前表的样子(这是从控制器循环的样本数据。这不是ajax):

<table class="table">
    <thead>
    <tr>

        <th>Room Number</th>
        <th>Cleaned By</th>
        <th>Total Time</th>
        <th>Supervised</th>
        <th>Issues</th>
        <th>Cleaned</th>
        <th>View</th>
    </tr>
    </thead>
    <tbody>
    @foreach($clean as $cleans)
        <tr>

            <td>{{$cleans->roomnumber}}</td>
            <td>{{$cleans->users->name}}</td>
            <td>{{$cleans->roomnumber}}</td>
            @if($cleans->verified == 1)<td class="verified">Yes</td>@else()<td class="notverified">No</td>@endif
            @if($cleans->img1 || $cleans->img1 || $cleans->img1 == !null)<td class="verified">Yes</td>@else()<td class="notverified">No Issues</td>@endif
            <td>{{$cleans->created_at->diffForHumans()}}</td>
            <td><a href="{{route('starlord.show', $cleans->id)}}"><i class="lnr lnr-eye"></i></a></td>
        </tr>
    @endforeach
    </tbody>
</table>

如上所示,我在我的控制器中使用with()来使用user_id列获取用户名。和diffForHumans()。还有一些if else语句。

到目前为止,这是我的AJAX:

$(function() {
    $('.form').submit(function(event) {

        // get the form data in value key pair using jquery serialize
        var data = $(this).serialize();
        // get the url we are posting to from the forms action attribute
        //var url  = $(this).attr('/admin/search');

        // process the form
        var search = $.ajax({
            type: "POST", // define the type of HTTP verb we want to use (POST for our form)
            url: "search", // the url where we want to POST
            data: data, // the url where we want to POST, set in variable above
            dataType: "json", // what type of data do we expect back from the server
            encode          : true
        })

        // using the done promise callback (success)
        search.done(function(data) {

            // log data to the console so we can see
            console.log(data);


            // here we will handle errors and validation messages
        });

        // stop the form from submitting the normal way and refreshing the page
        event.preventDefault();

    });//submit function

});//doc ready end

那么我如何附加到上面的表并仍然使用user_id列(关系)维护那些diffForHumans(),if else语句以及最重要的用户名?

更新(控制器):

public function search(Request $request){

    $data =  $request->only(['from', 'to']);
    $data['to'] .= ' 23:59:59';
    $output = Clean::whereBetween('created_at', $data)->get();
    //return view('admin/search-test', compact('output'));

    //return Response($output);
    return Response::json(array($output));
}

0 个答案:

没有答案