How do I add a table cell that has a shopping cart "Add to Cart" button with a quantity field?
How do I add a table cell that has a shopping cart "Add to Cart" button with a quantity field?
webpointz
Posts: 126Questions: 30Answers: 4
in DataTables
I'm using the latest datatables package and I want to show a table of products with pagination and beside each product (row) I want to put an "add to cart" button that when depressed after adjusting quantity, will be disabled.
I have no idea where to start but I have a regular table that does it at the moment using javascript that updates a session array.
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Use the
columns.render
orcolumns.defaultContent
option to return the HTML you want for the button. Also setcolumns.data
tonull
for that column since it doesn't need any data from the source.There is an Editor example of that available here (ignore the Editor aspect, and focus just on how the links are added to the table).
Allan
Thanks Allan
Hi Allan
I can't seem to get things to work. Basically I want a far right column that has an <input type="number"> with a <button> that when the user clicks on the button, it will perform an ajax post of the productid and quantity to a PHP session array (shopping cart).
I have tried a bunch of things using "render" to no avail and this is the last piece I need to complete for my application.
Basically, I have a table with ONE column coming from the database (product-id) and I wish to add TWO extra columns...ONE column that has an INPUT TYPE="number" and ONE column that has the <BUTTON>.
Update:
Hi Allan, I managed to get most of it working however, I'm wondering if you can guide me on a final part?
Here is the code I have:
I can now get the quantity for the row in question (I'm currently throwing it into an alert message), but once I process the entry to the SESSION array using AJAX, I want to be able to have a user move around the datatable (pagination) with the chosen QUANTITY locked for the row they just added if they go back AND I want to change the "defaultContent" for both custom columns so that on the rows submitted, the INPUT and BUTTON class names are changed and disabled.
Any thoughts?
Unfortunately there is no way to change the default content after initialisation. You would need to use a
draw
callback event to modify the elements that the default content has put into the document.The problem you'll face there is that you are using server-side processing, so the row (as far as the client-side is concerned) only exists for as long as it is shown. You would need to submit it to the server so it can then be redisplayed in future.
Allan
Thanks Allan,
I used the following and it works FANTASTIC! :
``` $(document).ready(function() {
```