在ajax后重新加载laravel foreach [英] Reload laravel foreach after ajax

查看:157
本文介绍了在ajax后重新加载laravel foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图中有一个foreach循环

I have a foreach loop in my view

<div class="col-lg-6 collapse" >
    <div class="job-summ-panel" id="job-summ-panel" >
        @foreach($jobs as $job)
            {{$job['active']}}
        @endforeach
    </div>
</div>

页面上还有一个带有地图标记的Google地图,当我单击地图标记时,会进行ajax调用以获取与该页面相关的所有信息

I also have a Google map on the page with map markers and when I click a map marker an ajax call is made to get all the info related to the page

$.ajax({
    method: "get",
    url: '/joblocation/jobsummary',
    data: {
        job_ref: marker.getCustomData('job_ref'),
    },
    success: function (response) {
        $('.collapse').collapse('show');
        $('#job-summ-panel').html(response.jobs)
    },                               
}); 

作业最初加载页面时最初是空的,因此它不会显示任何内容.我不知道如何刷新/重新加载div并使用新的响应数据再次执行foreach.

Jobs will initially be empty when they first load the page so it wont display anything. I dont get how to refresh/reload the div and do the foreach again with the new response data.

推荐答案

有两种方法可以实现此目的.首先是服务器端生成,其次是客户端应用程序.

There are two ways to achieve this. First, is server-side generation, and second is client-side application.

您可能希望将页面的该部分移至其他刀片模板.然后,通过AJAX调用,您可以重建页面并将输出发送到浏览器.例如:

路线定义

You might want to move that part of the page to a different blade template. Then with a AJAX call, you can rebuild the page and send the output to the browser. For example:

Route Definition

Route::post("generateList", "Partials@generateList");

部分模板定义

Partials \ loopContainer.blade.php

Partials\loopContainer.blade.php

<div class="col-lg-6 collapse" >
    <div class="job-summ-panel" id="job-summ-panel" >
        @foreach($jobs as $job)
            {{$job['active']}}
        @endforeach
    </div>
</div>

您的部分内容生成器

<?php 

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    class Partials extends Controller {

    public function generateList(Request $request){
        // generate data from the parameters again
        $input = $request->input('job_ref');
        $generatedData = GenerateDataFromJobRef($input);
        return View::make("partials/loopContainer", ["jobs" => $generatedData]);
    }
?>

客户端刷新

$.ajax({
    method: "POST",
    url: '/generateList',
    data: {
        job_ref: marker.getCustomData('job_ref'),
    },
    success: function (response) {
        $('.collapse').collapse('show');
        $('#job-summ-panel').html(response)
    },                               
}); 





路线定义

Route::post("generateList", "Partials@generateList");

您的部分内容生成器

<?php 

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    class Partials extends Controller {

    public function generateList(Request $request){
        // generate data from the parameters again
        $input = $request->input('job_ref');
        $generatedData = GenerateDataFromJobRef($input);
        return json_encode(["data" => $generatedData]);
    }
?>

客户端刷新

$.ajax({
    method: "POST",
    url: '/generateList',
    data: {
        job_ref: marker.getCustomData('job_ref'),
    },
    success: function (response) {
        $('.collapse').collapse('show');            
        var appendString = "";
        for(var i = 0; i < response.data.length; i++){
            appendString += "<div>" + response.data[i].info + "</div>";    
        }
        $('#job-summ-panel').empty().append(appendString);
    },                               
}); 





这篇关于在ajax后重新加载laravel foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆