2017-11-21 14 views
-1

テーブルから行をクリックすると、顧客の購入した製品を検索しようとしています。選択したテーブル行に基づいてテーブルに値を設定します

これは、顧客が購入しquantityamountcustomer idproduct idを持っている私のInventoryエンティティです。

public class InventoryEntity { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "id") 
private int id; 

@Column(name = "iQty") 
private int iqty; 

@Lob 
@Column(name = "TransactionDate") 
@Temporal(TemporalType.DATE) 
private Date transacDate; 

@Column(name = "subTotal") 
private double subTotal; 

@OneToOne 
@JoinColumn(name = "Customer_ID") 
private CustomerEntity c_inv; 

@OneToOne 
@JoinColumn(name = "Product_ID") 
private ProductEntity p_inv; 

public InventoryEntity() { 

} 

//getters and setters omitted for brevity 

public InventoryEntity(int iqty,Date transacDate, double subTotal) { 
    super(); 
    this.iqty = iqty; 
    this.transacDate = new Date(); 
    this.subTotal = subTotal; 

    } 
} 

マイCustomerEntity

public class CustomerEntity { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "cid") 
private int cid; 

@Column(name = "cName") 
private String cName; 

@Column(name = "cLname") 
private String cLname; 

@Column(name = "cEmail") 
private String cEmail; 

@Column(name = "cAddress") 
private String cAddress; 

@OneToOne(mappedBy = "c_inv", cascade = CascadeType.ALL, orphanRemoval = true) 
private InventoryEntity c_inventory;  

//getters and setters 

public CustomerEntity() { 

} 

public CustomerEntity(String cName, String cLname, String cEmail, String cAddress) { 
    super(); 
    this.cName = cName; 
    this.cLname = cLname; 
    this.cEmail = cEmail; 
    this.cAddress = cAddress; 
    } 
} 

製品のエンティティカスタマー購入Proの パブリッククラスProductEntity {

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "pid") 
private int pid; 

@Column(name = "pName") 
private String pName; 

@Column(name = "pPrice") 
private double price; 

@Column(name = "pQty") 
private int qty; 

@Column(name = "pStatus") 
private String status; 

/getters,setters and constructors 

@OneToOne(mappedBy = "p_inv", cascade = CascadeType.ALL, orphanRemoval = true) 
private InventoryEntity p_inventory; 
} 

HTMLダクト

<div class="container"> 
     <div class="row"> 
      <div class="col-lg-12 col-sm-12 col-xs-12"> 
       <table id="cust" class="table table-hover"> 
        <thead class="thead-inverse"> 
         <tr> 
          <th>NAME</th> 
          <th>SURNAME</th> 
          <th>EMAIL</th> 
          <th>ADDRESS</th> 
         </tr> 
        </thead> 
        <tbody> 
         <tr th:each="customer : ${customerTable} "> 
          <td th:text="${customer.cName}"></td> 
          <td th:text="${customer.cLname}"></td> 
          <td th:text="${customer.cEmail}"></td> 
          <td th:text="${customer.cAddress}"></td> 
         </tr> 
        </tbody> 
       </table> 
      </div>    
     </div> 

     <!-- HOW TO POPULATE THIS? WHEN I CLICK A ROW FROM CUSTOMER TABLE?--> 
     <label for="purchased_products">PURCHASED PRODUCTS</label> 
     <div class="row"> 
      <div class="col-lg-7 col-sm-7 col-xs-7"> 
       <table class="table table-striped table-hover"> 
        <thead class="thead-inverse"> 
         <tr> 
          <th>PRODUCT</th> 
          <th>QUANTITY</th> 
          <th>SUBTOTAL</th> 
          <th>DATE</th> 
         </tr> 
        </thead> 
        <tbody> 
         <tr th:each="inventory : ${inventoryTable}"> 
          <td value="${inventory.p_inv.pName}"></td> 
          <td th:text="${inventory.iqty}"></td> 
          <td th:text="${inventory.subTotal}"></td> 
          <td th:text="${inventory.transacDate}"></td> 
         </tr> 
        </tbody> 
       </table> 
      </div> 
     </div> 

     <div class="row float-right"> 
      <div class="col-xd-12" > 
       <div> 
        <button type="submit" class="btn btn-outline-secondary btn-block">BACK</button> 
       </div> 
      </div> 
     </div> 
    </div> 

EDIT:これは私がこれまで行ってきたものです。..

$(document).ready(function() { 
     var pid; 
     var cTable = $('#cust').DataTable({ 
      select: { 
       style: 'single' 
      } 
     }); 



     $('#cust tbody').on('click', 'tr', function() { 
      var id = $(this).children("td").map(function() { 
       return $(this).text(); 
      }).get(); 

      $.ajax({ 
       type : 'GET', 
       url : '/admin/customer/getDetails', 
       data : { 
        id : id[0] 

        'pname': id[1] 
        'iqty': id[2] 
        'subTotal': id[3] 
        'transacDate': id[4] 

       }, 
       success : function(r) { 
        alert(r); 

        $("#purchase tbody").append("<tr><td>" + data.pname + " 
</td><td>" + data.iqty + "</td><td>" + data.subTotal + "</td><td>" + 
data.transacDate +"</td></tr>"); 



       } 
      }); 


      pid = id[0]; 

     }); 

     $('#purchase').DataTable({ 
      "processing": true, 
      "searching": false, 
      "responsive": true, 
      "ajax": { 
       "url": "/admin/customer/getDetails", 
       "type": "GET", 
       "data": function (c){ 
        c.p_inv = p_inv; 
        c.iqty = iqty; 
        c.subTotal = subTotal; 
        c.transacDate = transacDate; 
       } }, 
       "columns": [{"data": "productn"}, {"data": "productq"}, {"data": "products"}, {"data": "productd"}] 
      }); 
     } 
    }); 

RetrieveProduct.java

@RestController 
public class RetrieveProducts { 


@Autowired 
InventoryRepository iRepo; 

@Autowired 
CustomerRepository cRepo; 

@Autowired 
ProductRepository pRepo; 



@GetMapping(value="/admin/customer/getDetails") 
public InventoryEntity getProduct(@RequestParam(value = "id") Integer id) { 
    System.out.println(id); 


    InventoryEntity inventoryEntity = iRepo.findOne(id); 
    return inventoryEntity; 

    } 
} 

私は私が提供する詳細を願っていますがするのに十分ですわかる。これで私を助けることができますか?

+1

これまでに何を試しましたか? –

+0

@MaciejKowalski私はこれを試していますhttps://stackoverflow.com/questions/18740771/populate-a-table-b-based-on-row-clicked-on-table-aこれは道場にあるので、ハードです私が他の答えを探すために。 –

+0

正確な問題がどこにあるか教えてください。誰かが助けてくれるかもしれません。ここですぐに解決策を考えないでください –

答えて

0

在庫オブジェクトを見つけるためにRestControllerを作成しました。

@RestController 
public class RetrieveProducts { 


@Autowired 
InventoryRepository iRepo; 

@Autowired 
CustomerRepository cRepo; 

@Autowired 
ProductRepository pRepo; 

@GetMapping(value="/admin/customer/getDetails") 
public InventoryEntity getProduct(@RequestParam(value = "id") Integer id) { 

    InventoryEntity inventoryEntity = iRepo.findOne(id); 
    return inventoryEntity; 

    } 
} 

顧客の購入した製品を見つけるのjavascript:

$(document).ready(function() { 
     var pid; 
     var cTable = $('#cust').DataTable({ 
      select: { 
       style: 'single' 
      } 
     }); 

     $('#purchase').DataTable(); 

     $('#cust tbody').on('click', 'tr', function() { 
      var id = $(this).children("td").map(function() { 
       return $(this).text(); 
      }).get(); 

      $.ajax({ 
       type : 'GET', 
       url : '/admin/customer/getDetails', 
       dataType : 'json', 
       data : { 
        id : id[0] 
       }, 
       success : function(r) { 
        $('table#purchase tbody tr #productName').text(r['p_inventory']['pname']); 
        $('table#purchase tbody tr #qty').text(r['iqty']); 
        $('table#purchase tbody tr #total').text(r['subTotal']); 
        $('table#purchase tbody tr #date').text(r['transacDate']); 


       } 
      }); 


      pid = id[0]; 

     }); 
    }); 

私は私が悪いの質問をしますが、ここで参照するための答えを知っています。

関連する問題