Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ??

JavaScript
var subTot = 0;
$('#Discount').val(0);
var PID = 0;
var ProductName;

$(function () {
    $('select#DProducts').change(function () {
        ProductName = $("[id*='DProducts'] :selected").text();
      ////  alert(ProductName);
      //  $.getJSON("/Sale/SelectedItemId/" + $("#DProducts > option: selected").attr("value"), function (data) {
      //      PID = data.Val();
      //  });
    });
});


$(document).ready(function () {


    var orderItems = [];
    //Add button click function
    $('#add').click(function () {
        //Check validation of order item
        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');
        }

        //get ProductId


        //Add item to list if valid
        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()))


            //Clear fields
          //  $('#DProducts').val('').focus();
            $('#quantity,#UnitPrice').val('');


        }
        //populate order items
        GeneratedItemsTable();

        $('#SubTotal').val(parseFloat(subTot));
        $('#NetTotal').val(parseFloat(subTot) - parseFloat($('#Discount').val()));


    });
    //Save button click function
    $('#submit').click(function () {
        //validation of order
        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');
        }

        //Save if valid
        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) {
                    //check is successfully save to database
                    if (d.status == true) {
                        //will send status from server side
                        alert('Successfully done.');
                        //clear form
                        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 for show added items in table
    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.
Posted
Updated 31-Aug-16 23:20pm
v2
Comments
F-ES Sitecore 1-Sep-16 4:14am    
If the problem is val.ProductName2 then there is no property called ProductName2 on "val", or it is empty. As we don't know what orderItems is it's impossible to say why that might be. If the issue is $ProductName2 can't be used then there is no element that matches the selector, and as we can't see the mark-up it's impossible ofr us to say why that might be. You need to learn to debug your code, or at least provide more relevant information.
Member 11883599 1-Sep-16 6:53am    
Yes, you are correct. I did not add productname to orderitems array.
now it works fine.
thanks a lot for your immediate support.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900