动态更新 Laravel 视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28107672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:45:50  来源:igfitidea点击:

Update Laravel View dynamically

phpajaxlaravel

提问by mboeckle

I am loading data from an API through JS send the data to the Laravel Controller and save them into the Database. After loading all the Ajax I want to display the data in a subview/section of the master.blade - Is it possible to render a View dynamically after the page finish loading, - also for later I want to update database rows and display the new data in the view dynamically.

我正在通过 JS 从 API 加载数据,将数据发送到 Laravel 控制器并将它们保存到数据库中。加载所有 Ajax 后,我想在 master.blade 的子视图/部分中显示数据 - 是否可以在页面完成加载后动态呈现视图, - 稍后我想更新数据库行并显示新的视图中的数据动态。

//afater Ajax loading - update / display the data in the view
public function loadcomments() {
    $comments = Comment::all();
    $childcomment = Childcomment::all();
    return View::make('partial')
    ->with(compact('comments'))
    ->with(compact('childcomments'));
}

in user.blade.php (main site) I am defining

在 user.blade.php (主站点)中,我正在定义

@section('comments')
    @include('partial')
@stop

and in the master.blade.php I am defining the yields:

在 master.blade.php 中,我定义了产量:

@yield('content')

@yield('comments')

any idea how to render the site with the updated content?

知道如何使用更新的内容呈现网站吗?

采纳答案by Everon

Once the page has finished loading, with out making further AJAX calls to the Laravel app its self it will have no further part in the request.

一旦页面完成加载,如果不对 Laravel 应用程序本身进行进一步的 AJAX 调用,它将不再参与请求。

You could just update the front end markup with JS or you can make a call back to your Laravel application using AJAX/jQuery to pull the data back out after it has added it to the database.

您可以只使用 JS 更新前端标记,或者您可以使用 AJAX/jQuery 调用回 Laravel 应用程序,以在将数据添加到数据库后将其拉回。

Use a resource controller or similar implementation to allow insertion and reading of the comments (CRUD) so you can pull data when you need using AJAX.

使用资源控制器或类似的实现来允许插入和读取注释 (CRUD),这样您就可以在需要使用 AJAX 时提取数据。

Edit

编辑

There are different ways to make a page dynamic on the front end all of these will usually include Javascript or another front end scripting language.

有多种方法可以在前端使页面动态化,所有这些通常包括 Javascript 或其他前端脚本语言。

In the past I have used jQuery to handle updates of the page content using either JSON or XML/HTML but lately I have started to use AngularJS. Another library is EmberJS which I am currently learning to use but I feel front end languages are out of scope for the question.

过去我使用 jQuery 来处理使用 JSON 或 XML/HTML 的页面内容更新,但最近我开始使用 AngularJS。另一个库是 EmberJS,我目前正在学习使用它,但我觉得前端语言超出了这个问题的范围。

There are many tutorials on updating the HTML after a page has loaded by making a call back to a controller or other resourceful route.

有很多关于在页面加载后通过回调控制器或其他资源丰富的路由来更新 HTML 的教程。

Say the posts have been saved to the database, if this is done AFTER the view has been returned to the browser you WILL have to use javascript to pull out the data and most likely have a piece of js code to tick over that "polls" your resource controller for new comments.

假设帖子已保存到数据库中,如果这是在视图返回到浏览器之后完成的,您将不得不使用 javascript 来提取数据,并且很可能有一段 js 代码来勾选那个“民意调查”您的资源控制器以获取新评论。

If a new comment has been detected a second request is made to pull the comments out OR the comments are returned from the polling requests using AJAX.

如果检测到新评论,则会发出第二个请求以提取评论,或者使用 AJAX 从轮询请求中返回评论。

On the laravel side, a partial view could be returned or JSON, for simplicity we'll replace the view. You would make a jQuery selector for the current existing partial on the browser and then replace it with the one pulled from Laravel with AJAX.

在 Laravel 方面,可以返回部分视图或 JSON,为简单起见,我们将替换视图。您将为浏览器上的当前现有部分创建一个 jQuery 选择器,然后将其替换为从 Laravel 中提取的带有 AJAX 的选择器。

$('#some-page .my-view-to-update').html(somedata);

$('#some-page .my-view-to-update').html(somedata);

My jQuery is VERY rusty so read up on the relevant documentation to update the HTML correctly.

我的 jQuery 非常生疏,因此请阅读相关文档以正确更新 HTML。

回答by Zeshan Tariq