After add button click data is added in gridview jquery is not working for that row
1. Add 1 row with filling data
2. Which row you have added that row you check qty textbox changed event and rate textbox changed event is not working.
$(function () {
var currLoc = $(location).attr('href');
$.ajax({
type: "POST",
url: currLoc + "/GetCustomers",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
$("[id*=gvCustomers]").find('[id*=btnAdd]').on('click', function () {
debugger
var $tr = $(this).parents('tr');
var row = $(this).closest('tr');
var gridView = $("[id*=gvCustomers]");
var add_row;
//var prev_row = row.prev();
//var next_row = row.next();
//$('table#tbl > tbody > tr').not(':first').not(':last').addClass('highlight');
if ($tr.length > 0) {
var dummy_row = gridView.find("tr").eq(1);
add_row = dummy_row.clone();
$(add_row).find('.name').val($tr.find("[id*=txtFName]").val());
$(add_row).find('[id*=hfId]').val($tr.find("[id*=hffId]").val());
$(add_row).find('[id*=txtQty]').val($tr.find("[id*=txtFQty]").val());
$(add_row).find('[id*=txtRate]').val($tr.find("[id*=txtFRate]").val());
$("[id*=gvCustomers] tbody tr:last").before($(add_row[0]));
if (dummy_row.find('[id*=txtName]').val() == "") {
dummy_row.remove();
}
$tr.find("[id*=txtFName]").val("");
$tr.find("[id*=hffId]").val("");
$tr.find("[id*=txtFQty]").val("");
$tr.find("[id*=txtFRate]").val("");
SearchText($(add_row[0]).find('.name'), $(add_row[0]).find('[id*=hfId]'));
}
});
$("[id*=gvCustomers]").find('[id*=txtQty]').on('change', function () {
//Calculate and update Row Total.
var row = $(this).closest("tr");
//Check whether Quantity value is valid Float number.
var quantity = parseFloat($.trim($(this).val()));
if (isNaN(quantity)) {
quantity = 0;
}
//Update the Quantity TextBox.
$(this).val(quantity);
var rate = parseFloat(row.find('[id*=txtRate]').val());
row.find('[id*=txtTotal]').val(quantity * rate);
// $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
//Calculate and update Grand Total.
CalculateTotal();
});
$("[id*=gvCustomers]").find('[id*=txtRate]').on('change', function () {
//Calculate and update Row Total.
var row = $(this).closest("tr");
//Check whether Quantity value is valid Float number.
var rate = parseFloat($.trim($(this).val()));
if (isNaN(rate)) {
rate = 0;
}
//Update the Quantity TextBox.
$(this).val(rate);
var quantity = parseFloat(row.find('[id*=txtQty]').val());
row.find('[id*=txtTotal]').val(quantity * rate);
// $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
//Calculate and update Grand Total.
CalculateTotal();
});
});
function GetValue(ele) {
alert($(ele).closest('tr').find('[id*=hfId]').val());
return false;
}
function SearchText(ele, hid) {
var hidden = $(hid);
var currLoc = $(location).attr('href');
$(ele).typeahead({
hint: true,
highlight: true,
minLength: 1,
source: function (request, response) {
$.ajax({
url: currLoc + '/GetContactNames',
data: "{ 'prefixText': '" + request + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
items = [];
map = {};
$.each(data.d, function (i, item) {
var id = item.split('-')[1];
var name = item.split('-')[0];
map[name] = { id: id, name: name };
items.push(name);
});
response(items);
$(".dropdown-menu").css("height", "auto");
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
updater: function (item) {
$(hidden).val(map[item].id);
return item;
}
});
}
function OnSuccess(result) {
var row;
var customers = $($.parseXML(result.d)).find("Table");
if (row == null) {
row = $("[id*=gvCustomers] tr:last-child").prev().clone(true);
}
var footer;
footer = $("[id*=gvCustomers] tr:last").clone(true);
$("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
$.each(customers, function () {
$(row[0]).find('[id*=hfId]').val($(this).find("CustomerID").text());
$(row[0]).find('.name').val($(this).find("ContactName").text());
$(row[0]).find('[id*=txtQty]').val($(this).find("Qty").text());
$(row[0]).find('[id*=txtRate]').val($(this).find("Rate").text());
$(row[0]).find('[id*=txtTotal]').val(parseFloat($(this).find("Qty").text()) * parseFloat($(this).find("Rate").text()));
$("[id*=gvCustomers] tbody").append($(row[0]));
row = $("[id*=gvCustomers] tr:last-child").clone(true);
});
CalculateTotal();
$("[id*=gvCustomers] tbody").append(footer[0]);
$.each($('[id*=gvCustomers]').find($('.name')), function (index, item) {
SearchText($(item), $(item).closest('tr').find('input[type=hidden]'));
});
};
function CalculateTotal() {
var grandTotal = 0;
$("[id*=txtTotal]").each(function () {
grandTotal = grandTotal + parseFloat($(this).html());
});
$("[id*=gvCustomers]").find("[id*=txtFTotal]").html(grandTotal.toString());
}