Datatable "add-to-cart" button in javascript
Datatable "add-to-cart" button in javascript
Hi, good morning everyone.
Meanwhile congratulations for the mega job you are doing!
I have a problem I've noticed now:
I have a button that I charge the product js
according to this format:
Table HTML:
<table id="datatable-responsive" class="ui celled table table-striped table-bordered dt-responsive nowrap dataTable no-footer dtr-inline" cellspacing="0" role="grid" aria-describedby="datatable-responsive_info" style="width: 100%;">
<thead>
<tr>
<th>
<?php echo $LORDER; ?>
</th>
<td>
<form class="add-to-cart-form">
<div class="input-group"> <input type="number" name="quantity" value="15" min="15" class="form-control" placeholder="Type quantity here..."><span class="input-group-btn">< button type="submit" class="btn btn-primary add-to-cart"><span class="glyphicon glyphicon-shopping-cart"></span></button>
</span>
</div>
</form>
</td>
</tr>
</thead>
</table>
Footer HTML
$(document).ready(function(){ $('.add-to-cart-form').on('submit', function(){
var prod_id = $(this).closest('tr').find('.product-id').text();
var societa = $(this).closest('tr').find('.societa').val();
var utente = $(this).closest('tr').find('.utente').val();
var name = $(this).closest('tr').find('.product-name').text();
var subtotal = $(this).closest('tr').find('.subtotal').text();
var quantity = $(this).closest('tr').find('input').val();
window.location.href = "add_to_cart.php?societa=<?php echo $societa;?>&utente=<?php echo $utente;?>&prod_id=" + prod_id + "&name=" + name + "&quantity=" + quantity + "&subtotal=" + subtotal + "&page=<?php echo isset($page) ? $page : 1; ?>";```
```return false;```
});
});
The problem comes when I have to change pagination, or when the column containing the button goes to child (table-responsive), at the click of the button gives me only the amount defined in the input field instead of hooking all the produce of the field in the <tr>
I found this solution, but I can not put it into practice:
https://editor.datatables.net/examples/simple/inTableControls.html
you could kindly help me?
N.B. ajax no use since the rendering of the table needs a first filter that comes with the GET method from the address bar.
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
Answers
Second top FAQ :-). You need to use a delegated event handler rather than a static one.
Allan