【问题标题】:How to load the content of a show method in laravel on the index method using jquery?如何使用 jquery 在索引方法上加载 laravel 中显示方法的内容?
【发布时间】:2016-07-18 02:13:27
【问题描述】:

我有两个方法 show 和 index 我在 index 方法上有一个项目列表,当用户单击它时,它会将她带到另一个页面,其中包含一些属于该 id 的数据。我不想这样做,而是想使用 jquery 在同一页面上加载数据。我的index.blade.php 视图中有以下内容。我如何在 laravel 中实现这一点?

  @foreach ($categories as $category)

    <div class="body">
    <h4><a style="text-decoration: none; " href="{{ URL::route('category.show', $category->id) }}">{{$category->name}}</a></h4>

    </div>
   @endforeach

<?php
 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use illuminate\HttpResponse;
 use App\Http\Requests\todolistRequest;
 use App\Http\Requests\CreateCategoryRequest;
 use App\Http\Requests;
 use App\Companylist;
 use App\Category;
 use  Illuminate\Support\Facades\DB;
 class CategoryController extends Controller
 {

public function create(){
return view('category.create');
 }

 public function index(){
 $categories=Category::all();
 return view('category.index',compact('categories'));
 }
  public function store(CreateCategoryRequest $request){
 $category = new Category($request->all());
 $category->save();

  return \Redirect::route('category.create')->with('message',      'Your list has been created!'); 
  }
  public function show($id)
  {
  $category = Category::findOrFail($id)->companylist()->get();
  $cat=Category::findOrFail($id);
// this my route
   Route::resource('category','CategoryController');

      return                           view('category.show')->with('category',                          $category)->with('cat',$cat);
 }

// }

【问题讨论】:

  • “shift”键是键盘左下角的那个长按钮。 Use this regularly,它大大提高了您获得有用答案的机会。

标签: php jquery laravel


【解决方案1】:

使用引导模式或弹出窗口。 根据您的需要设计您的category.show 视图。 调用 ajax 并将您返回的视图响应附加到该模态主体并显示它,以便它与您想要的视图完全相同。

示例代码用于替换,ajax 类似

这是替换模式

<!-- Show Modal content-->
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">Show data</h4>
  </div>
  <div class="modal-body replace">

  </div>
</div>

这将是你的 ajax 方法

      $('.showModalButton').on('click',function(e){
        var id = $(this).data('id'); // get id of resource you want to show
        $('.replace').empty(); // 
        $.ajax({
            url: 'your route/'+id,
            type: "GET",

            success:function(data) {
                $('.replace').html(data);
                },
            error:function(jQXHR, textStatus, errorThrown) {
                console.log(jQXHR);
                console.log(textStatus);
                console.log(errorThrown);
                }
        }); //end Ajax call

    });

这将是你在控制器中的显示方法

public function show($id)
   {
     $category = Category::findOrFail($id)->companylist()->get();
     $cat=Category::findOrFail($id);
     // this my route
     Route::resource('category','CategoryController');

      return view('category.show')->with('category',$category)->with('cat',$cat);
    }

设计 category.show 但您想显示您的数据。

【讨论】:

  • 任何关于如何做到这一点的链接
  • 它对@ShowoleOladayo 有帮助吗?
  • 还在整理,这家伙在说删除和编辑,只是想把这样的show view的内容加载到index view上
  • 检查我的答案@ShowoleOladayo 如果有帮助,标记它已解决。
  • 我已尽我所能让您理解...@ShowoleOladayo
猜你喜欢
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 1970-01-01
  • 2011-02-07
相关资源
最近更新 更多