簡體   English   中英

如何將選定表行中的 ID 傳遞到我的 AJAX function 以獲取數據

[英]How can I pass an ID from selected table row to my AJAX function that fetching data

抱歉打擾,我無法將 ID 傳遞給我的 AJAX function。

我想將所選表行的 ID 傳遞給我的 AJAX function 並將其用於我的 controller 中的查詢

這是我的桌子

這是我的表格行的代碼

<div class="container">

<h2 style="margin-top: 12px;" class="alert alert-success">Details</h2><br>
    <div class="row">
      <div class="col-12">
      <table class="table table-bordered" id="">
      <thead>
      <tr>
      <th>Id</th>
      <th>Name</th>
      <th>Email</th>
      <td colspan="2">Action</td>
      </tr>
      </thead>
      <tbody id="users-crud">
      @foreach($merchants as $merchant)
      <tr id="user_id_{{ $merchant->id }}">
      <td>{{ $merchant->id}}</td>
      <td>{{ $merchant->first_name }}</td>
      <td>{{ $merchant->email }}</td>
     <td><a id="getData" onClick="getData({{$merchant->id}})" class="btn btn-info">Show</a> 
     </td>
      </tr>
      @endforeach
      </tbody>
      </table>
      {{ $merchants->links() }}
      </div> 
    </div>
  </div>

單擊顯示 ID 后應將其傳遞給我的 AJAX function,這是腳本的代碼

   <script type=text/javascript>
      $(document).ready(function() {
       });
    
       function getData(id){
      $('#myModal').modal('show');
          $.ajax({  //create an ajax request to display.php
          type: "GET",
       url: "getproducts/",
       // dataType: 'json',
        data: {id: id}, // This will be {id: "1234"}     
        success: function (data) {
       $("#id").html(data.id);
       $("#first_name").text(data.first_name);
       }
       });
    
      };
    
    </script>

ID 將用於我的 controller 中的查詢,這是我的控制器代碼,請注意我的 where 查詢中的數字 1 是硬編碼的,我想放置從所選表中獲取的 ID

public function getproducts()
{
    $merchants  = DB::table('merchants')
    ->select('merchants.*', 'product.product_name as product_names')
    ->join('product','product.id','=','merchants.id')
    // THE NUMBER 1 IS HARD CODED
    ->where('merchants.id', 1)
    ->paginate(8);
    return response()->json($test, 200);
}

Laravel Controller 中的每個方法都可以將 arguments“注入”其中,包括Request $request變量。 namespace聲明之后將其添加到 Controller 的頂部:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

然后修改你的方法:

public function getproducts(Request $request) {
   ...
}

然后,不是對值1進行硬編碼,而是從$request object 中提取它:

->where('merchants.id', $request->input('id'))

完整的文檔可以在這里看到:

https://laravel.com/docs/9.x/requests#accessing-the-request

你的代碼對我來說似乎很好。 您面臨的問題是什么? 但是您可以進一步改進代碼。

<td><a id="getData" onClick="getData({{$merchant->id}})" class="btn btn-info">Show</a> 

相反,您可以使用全局 class。在 class 中使用相同的 HTML id 也不是好的做法。

<td><a class="merchant_info" data-id="{{$merchant->id}}" class="btn btn-info">Show</a> 

現在需要修改為jQuery代碼

<script type=text/javascript>
  $(document).ready(function() {
    $('.merchant_info').click(function(e){
       e.preventDefault();
       var dataId = $(this).attr("data-id");

       // Now do the Ajax Call etc

    });
  });
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM