Please refer to this image, so I can explain the issue better. I have Master/Detail form (Bill of Materials in this case), which stores data in 2 database tables.
The top of the form (Product,Quantity) is stored in the Master table, and with the inserted id(as FK), the Details table is populated with as many components and quantity entered below.
But, until the final SAVE(submit) is pressed, I'm storing the component ID (value from the dropdown) and the quantity (from the input field) into JavaScript object within array:
var products = [];
$("#add").click(function(){
var prodname_fk = $("select").val();
var prodname = $("select option:selected").text();
var quantity = $("#quantity").val();
//Checks if the product or quantity has not been selected
if (quantity == '' || prodname_fk == '')
{
alert("Please select product and quantity!");
return false;
}
//Pushes the Objects(products [id,quantity,prodname,uom]) into the Array
products.push({id:prodname_fk,prodname:prodname,quantity:quantity});
//Test Purpose CONSOLE output
var i = 0;
$.each(products, function(){
console.log(products[i].id + " --> " + products[i].prodname + " --> " + products[i].quantity);
i++;
});
//Emptys the product and quantity
$("select").val("");
$("#quantity").val("");
});
I also get the product name (text from the dropbox) for displaying purposes in the table below.
So, my question is:
How can I output the Component and Quantity in the table where it says "No Records!", after each ADD is pressed? (ADD does not submit or refresh the page)
How can I add "delete" function in the same table for each object from the array?
How can I check if a component exists already in the array, and just add up the quantity?
This is my form:
Thank you very much in advance!
p.s. I'm trying to achieve something like this: How to store temporary data at the client-side and then send it to the server