Here I am planning to display an HTML table, It gives all the values but Productname2 value.
$row.append($('').html(val.ProductName2));
Please, someone tells me where I am wrong ??
var subTot = 0;
$('#Discount').val(0);
var PID = 0;
var ProductName;
$(function () {
$('select#DProducts').change(function () {
ProductName = $("[id*='DProducts'] :selected").text();
});
});
$(document).ready(function () {
var orderItems = [];
$('#add').click(function () {
var isValidItem = true;
if ($('#DProducts').val() == '') {
isValidItem = false;
$('#DProducts').siblings('span.error').css('visibility', 'visible');
}
else {
$('#DProducts').siblings('span.error').css('visibility', 'hidden');
}
if (!($('#quantity').val() != '' && !isNaN($('#quantity').val()))) {
isValidItem = false;
$('#quantity').siblings('span.error').css('visibility', 'visible');
}
else {
$('#quantity').siblings('span.error').css('visibility', 'hidden');
}
if (!($('#UnitPrice').val()!= '' && !isNaN($('#UnitPrice').val()))) {
isValidItem = false;
$('#UnitPrice').siblings('span.error').css('visibility', 'visible');
}
else {
$('#UnitPrice').siblings('span.error').css('visibility', 'hidden');
}
if (isValidItem) {
orderItems.push({
ProductId: PID,
Quantity: parseInt($('#quantity').val()),
UnitPrice: parseFloat($('#UnitPrice').val()),
SubTotal: parseInt($('#quantity').val()) * parseFloat($('#UnitPrice').val())
});
subTot = subTot + (parseInt($('#quantity').val()) * parseFloat($('#UnitPrice').val()))
$('#quantity,#UnitPrice').val('');
}
GeneratedItemsTable();
$('#SubTotal').val(parseFloat(subTot));
$('#NetTotal').val(parseFloat(subTot) - parseFloat($('#Discount').val()));
});
$('#submit').click(function () {
var isAllValid = true;
if (orderItems.length == 0) {
$('#orderItems').html('<span style="color:red;">Please add order items</span>');
isAllValid = false;
}
if ($('#AccountInvNo').val().trim() == '') {
$('#AccountInvNo').siblings('span.error').css('visibility', 'visible');
isAllValid = false;
}
else {
$('#AccountInvNo').siblings('span.error').css('visibility', 'hidden');
}
if ($('#SaleDate').val().trim() == '') {
$('#SaleDate').siblings('span.error').css('visibility', 'visible');
isAllValid = false;
}
else {
$('#SaleDate').siblings('span.error').css('visibility', 'hidden');
}
if ($('#Customer').val().trim() == '') {
$('#Customer').siblings('span.error').css('visibility', 'visible');
isAllValid = false;
}
else {
$('#Customer').siblings('span.error').css('visibility', 'hidden');
}
if (isAllValid) {
var data = {
AccntInvoiceNo: $('#AccountInvNo').val(),
SaleDate: $('#SaleDate').val(),
SubTotal: $('#SubTotal').val(),
Discount: $('#Discount').val(),
NetAmount: $('#NetTotal').val(),
SaleType: 1,
CustomerId: 2,
UserId: 1,
SaleDescs: orderItems
}
$(this).val('Please wait...');
$.ajax({
url: '/Sale/SaveOrder',
type: "POST",
data: JSON.stringify(data),
dataType: "JSON",
contentType: "application/json",
success: function (d) {
if (d.status == true) {
alert('Successfully done.');
orderItems = [];
$('#AccountInvNo').val('');
$('#saleDate').val('');
$('#orderItems').empty();
$('#SubTotal').val('');
$('#Discount').val('');
$('#NetTotal').val('');
}
else {
alert('Failed');
}
$('#submit').val('Save');
},
error: function (exception) {
alert('Exeption:' + exception);
$('#submit').val('Save');
}
});
}
});
function GeneratedItemsTable() {
if (orderItems.length > 0) {
var $table = $('<table/>');
$table.append('<thead><tr><th>Item</th><th>Quantity</th><th>Unit Price</th><th>Total</th></tr></thead>');
var $tbody = $('<tbody/>');
$.each(orderItems, function (i, val) {
var $ProductName2 = $("[id*='DProducts'] :selected").text();
var $row = $('<tr/>');
$row.append($('<td/>').html(val.ProductName2));
$row.append($('<td/>').html(val.Quantity));
$row.append($('<td/>').html(val.UnitPrice));
$row.append($('<td/>').html(val.SubTotal));
var $remove = $('<a href="#">Remove</a>');
$remove.click(function (e) {
e.preventDefault();
orderItems.splice(i, 1);
GeneratedItemsTable();
});
$row.append($('<td/>').html($remove));
$tbody.append($row);
});
$table.append($tbody);
$('#orderItems').html($table);
}
else {
$('#orderItems').html('');
}
}
});
What I have tried:
I tried to give text.Product2
but the problem still exists.