简体   繁体   中英

Is it possible to get the parent and child of table row using each function of Jquery?

I have problem regarding inserting the parent and the child of each items. I want to separately insert the parent and the child to the database. I will show you guys the pseudocode of my problem. and the below I give the jsfiddle , This is web app is like a WEB POS of a restaurant.

I design my web app like this, having a child item or it called as condiments.

喜欢这个

Pseudocode:

  1. Insert the Main item in table database A.

  2. After the Main item already inserts in the database table A. In the inside of success function of Ajax. set the condition if this Main item has a Sub Item. if the Main item has a Sub Item get the last insert of the Main item and insert the Sub item in table B of the database. else if the Main item has no Sub Item, The Main item will insert only in table A.

Output should be on the database.

Ex.

Table A (Table for Main Item only) 表A.

Table B (Table for Sub Item) 表B.

 $('button#add_to_cart').on('click', function() { var customer_id = $('#hidden_customer_id').val(); var z = $('#noun_chaining_order').find('tr.condimentParent'); var x = $('#noun_chaining_order').find('tr.editCondiments').add(x).add(z).each(function() { var $tds_menu = $(this).find("td"); Qty_menu = $tds_menu.eq(0).text(), Item_menu = $tds_menu.eq(1).text(), Price_menu = $tds_menu.eq(2).text(); console.log(this); console.log(Item_menu); }); }); 
 <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title></title> </head> <body> <table class="table table-hover upsize_check" id="noun_chaining_order" style="border:none;"> <input type="hidden" name="" value="" id="hidden_customer_id"> <thead> <tr style="font-size: 15px; color:white;"> <th scope="col">Qty</th> <th scope="col">Items</th> <th scope="col">Price</th> <th>Action</th> </tr> </thead> <tbody style="font-size:14px;" id="tbody_noun_chaining_order"> <tr class="condimentParent"> <td></td> <td>$5.00 Extra Crispy 2 Piece Box</td> <td class="total">5.00</td> <td><button class="removeorderWithCondi btn btn-danger form-control">Delete</button></td> </tr> <tr class="editCondiments"> <td class="condiments_order_quantity">2</td> <td>*Standard</td> <td class="total">0.00</td> <td class="allow_to_open_condiments_conditional" style="display:none;">Yes</td> <td class="condi_section_id" style="display:none;">3</td> </tr> <tr class="editCondiments"> <td class="condiments_order_quantity">2</td> <td>*Individual Fries</td> <td class="total">0.00</td> <td class="allow_to_open_condiments_conditional" style="display:none;">Yes</td> <td class="condi_section_id" style="display:none;">2</td> </tr> <tr class="editCondiments"> <td class="condiments_order_quantity">1</td> <td>*Buttery Bread</td> <td class="total">0.00</td> <td class="allow_to_open_condiments_conditional" style="display:none;">No</td> <td class="condi_section_id" style="display:none;">4</td> </tr> <tr class="editCondiments"> <td class="condiments_order_quantity">1</td> <td>*Chocolate Chip Cookie</td> <td class="total">0.00</td> <td class="allow_to_open_condiments_conditional" style="display:none;">No</td> <td class="condi_section_id" style="display:none;">5</td> </tr> <tr class="editCondiments"> <td class="condiments_order_quantity">1</td> <td>*355ml Pepsi</td> <td class="total">0.00</td> <td class="allow_to_open_condiments_conditional" style="display:none;">No</td> <td class="condi_section_id" style="display:none;">6</td> </tr> </tbody> </table> <center> <button type="button" class="btn btn-primary" style="background-color:#3D0081; border-color:#3D0081;" id="add_to_cart">Click to process the order</button> </center> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html> 

据我所知 - 你想使用数据库上下文来添加数据库中的实体?

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