Laravel如何使用Ajax将数据从控制器传递到模式对话

时间:2018-06-30 20:18:58

标签: php jquery laravel

我有项目,每个项目都有相关的项目,所以当我打开主页时会显示所有项目,当我想单击任何项​​目时,ajax会将这个项目ID传递给控制器​​以获取与此相关的项目项目,问题是我想在模式对话框中显示相关项目,该模式对话框现在将所有相关项目显示给所有项目,而不是当前项目。 我认为问题是因为包含foreach!的主页中包含模式,希望您能帮助我解决这个问题

路线

Route::get('/',['as'=>'showItems','uses'=>'HomeController@getItem']);
Route::get('Get_relateditem',['as'=>'Get_relateditem','uses'=>'HomeController@getItem']);

ajax

$(function(){
$('.Item_root').on("click", function () {
var item_id = $(this).data('id');
 $.ajax({
    type:'get',
     url: '/',
       data: {
        '_token': $('input[name=_token]').val(),
       'item_id':item_id,                    
      },
    success:function(data){}
    }); 
   });
  });

控制器

 public function getItem(Request $request)
 { 
 $currentitemid =$request->item_id;
  $ritems = Relateditem::orderBy('id', 'asc')->where('ritemf_id','LIKE','%'.$currentitemid.'%')->with('items')->get()->groupBy('ritemf_id');

 $items = Item::orderBy('category_id', 'asc')->with('category')->get()->groupBy('category_id');

$categories = Category::orderBy('category_id', 'asc')->get();

 return view('home')->with('items',$items)->with('categories',$categories)->with('ritems',$ritems);
}
}

模式

@foreach($ritems as $item_id => $realtedItems)

 @foreach($realtedItems as $ritem)

  <div class="SuggestedItem_container">
 <label color="red" class="Checker_root Checker_red Checker_left">
      <input type="checkbox" class="Checker_input" value="on">
      <div class="SuggestedItem_nameContainer">
        <div>
     <span class="SuggestedItem_name">{{$ritem->riteml_id}}</span>

     <span class="SuggestedItem_price styles_small styles_base styles_spacing-base">+$3.95</span></div></div>

    <div class="SuggestedItem_description styles_small styles_base styles_spacing-base">
    <span class="SuggestedItem_category styles_bold">Appetizers</span>

    <span> · Edamame soybean pods harvested right before the beans begin to harden are lightly boiled and seasoned with sea salt.</span>
    </div>
  </label>
  </div>
 @endforeach
 @endforeach

2 个答案:

答案 0 :(得分:0)

修改路线:

Route::get('/','HomeController@getItem');
Route::get('/get_related_items/{id}','HomeController@getRelatedItems');

修改getItem以仅获取项目和类别:

public function getItem(Request $request)
{
    $items = Item::orderBy('category_id', 'asc')
      ->with('category')->get()
      ->groupBy('category_id');
    $categories = Category::orderBy('category_id', 'asc')->get();
    return view('home',['items' => $items,'categories' => $categories]);
}

获取单个商品ID的相关商品:

public function getRelatedItems(Request $request, $id)
{
    $ritems = Relateditem::orderBy('id', 'asc')
        ->where('ritemf_id',$id)
        ->with('items')
        ->get()
        ->groupBy('ritemf_id');
    return response()->json($ritems);

}

现在进入js部分:

$(function(){
  $('.Item_root').on("click", function () {
    var item_id = $(this).data('id');
    $.ajax({
      type:'get',
      url: '/get_related_items/' + item_id,
      data: {
        '_token': $('input[name=_token]').val()
      },
      success:function(data){
        if(data && data.length){
          var con_el = $('.SuggestedItem_container');
          for(var i = 0; i < data.length;i++){
            var related_item_el = "<div class='related_item'><p>" + data[i].id + "</p></div>"
            con_el.append(related_item_el);
          }
        }else{
          console.log('no related items found');
        }
      }
    });
  });
});

这会将所有相关项目插入SuggestedItem_container

我没有编写视图模板,因为该部分很简单,请注意,我仅包含相关项目ID作为示例,因为我不知道该项目包含哪些字段。 我希望这对您有帮助

答案 1 :(得分:0)

您应该使用js获取ajax获取结果并以模式显示数据。您可以在此处找到用于显示模态数据的示例。

https://github.com/durmus-aydogdu/real-time-application

我希望这会有所帮助。