简体   繁体   English

在Ajax成功之后,Laravel将数据追加到$ cList

[英]Laravel append data to $cList after ajax success

I dont know how to append new data from controller to the html view. 我不知道如何将新数据从控制器添加到html视图。

my ajax post (it works): 我的ajax帖子(有效):

   $.ajax({
    type: 'get',
    dataType: "json",
    url:getWizardSelectionUri,
    data: ....
    success:function(data){ 
        $('#bikeliste').empty()
         .....

here is my html with the static request 这是我的静态请求的HTML

 @foreach(App\Bike::all() as $cList)
    <li class="list-group-item">
        <table border="0" width="100%">
          <colgroup width="200" span="3"></colgroup>
            <td>   
                <a href="{{$cList->link}}" target="_blank">

                    <img src="{{$cList->bildlink}}" height="150" width= "auto"> 
                </a>
                <br>
            </td>
            <td>
                <h5>
                    <a href="{{$cList->link}}" target="_blank">
                        <strong>{{$cList->hersteller}} </strong>
                        {{$cList->modell}} 
                        {{$cList->modelljahr}} 
                        <br>

                    </a>
                </h5>
            </td>
            <td>
                @if ($cList->gewicht)
                    <strong>Gewicht: </strong> {{$cList->gewicht}}<br>
                @endif
                @if ($cList->laufraddurchmesser)
                    <strong>Radgröße: </strong>{{$cList->laufraddurchmesser}}<br>
                @endif


            </td>
        </table>
        <br>
    </li>
@endforeach

is there a posibility to change the data without reloading the page? 是否可以在不重新加载页面的情况下更改数据?

You are sending data from your ajax to your Controller asynchronously, so in your controller method you just return a json response: 您是将数据从ajax异步发送到Controller,因此在controller方法中,您只返回json响应:

public function store(Request $request) {
    ... your store function

    if($store) {
        return response()->json([
            'data' => $data
        ]);
    }
    return response()->json([
        'data' => $data
    ]);

}

$data contains the stuff your saved in your database you return that data to your ajax promise and get the data. $data包含您保存在数据库中的内容,然后将该数据返回给ajax promise并获取数据。 So in your ajax success function you have to do something like this: 因此,在ajax成功函数中,您必须执行以下操作:

success:function(response){ 
    ...
    var data = response.data // holds the data
    yourhtml.append(data); // dissables this one ...

In your success you now holds the saved data and need your append that now into your html. 为了成功,您现在拥有保存的数据,需要将其追加到html中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM