简体   繁体   中英

Refresh a content page without refreshing master page in laravel

I have an admin layout

在此输入图像描述


I have a master layout contain my :

  • top nav
  • left side nav

Each page is a view, I extend the master layout view, and update only the content section.

My goal is to refresh only a content page without refreshing master page.


Is there any laravel package for something like this ?

Short answer: use AJAX . There no real need to learn a JavaScript framework if it's that simple (unless you really want to). The below example uses jQuery and assumes you're clicking on the Users link in the navigation (all links there should have a nav-item class so they can be easily identified via that selector) and the link has the attribute href="/admin/users" . When clicked it loads the contents in the element with the class content :

$('.item-nav').click(function (event) {
    // Avoid the link click from loading a new page
    event.preventDefault();

    // Load the content from the link's href attribute
    $('.content').load($(this).attr('href'));
});

If the route admin/users would return the users table with it's contents:

get('admin/users', function () {
    return view('users')->with('users', Users::all());
});

The above Laravel route example is of course simplified to showcase the idea that each item in your navigation should link to the contents that need to be displayed dynamically.

You can use renderSections().

In your main controller action ( say AdminUsersController@getIndex ) you can render a particular section of your view depending on the request. This way if you do an ajax request to the same page you are on you can return only the section you require.

 if($request->ajax()) {
         return view('admin.users',$users)->renderSections()['content'];
 }

 return view('admin.users',$users);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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