how to change checkbox by icon validate - javascript

i have two forms, form1 contains the date and the display button, the show button allows you to hide the form1 and display the form2, the form2 contains the chosen date and a datatable with checkboxes ,I want when I display the table just the checkboxes that verify these two conditions become valid icon, not the table input.
index.php
<script type="text/javascript">
$(document).ready(function() {
$('#check_all').on('click', function(e) {
if ($(this).is(':checked', true)) {
$(".checkbox").prop('checked', true);
} else {
$(".checkbox").prop('checked', false);
}
});
$('.checkbox').on('click', function() {
if ($('.checkbox:checked').length == $('.checkbox').length) {
$('#check_all').prop('checked', true);
} else {
$('#check_all').prop('checked', false);
}
});
$("#form2").hide();
$("#hide").click(function(){
$("#form1").hide();
$("#form2").show();
let dat = $("#dateS").val();
$("#da").text(dat);
$("tr").each(function(i, r) {
let mat = $(r)[0].cells[2].innerText;
for (var i = 0; i < 2; i++){
if(dat=="2020-02-17" && mat == "52"){
$(r).find("input").first().replaceWith('<span style="color: green;font-weight: bolder;">✔</span>');
//✓ -
}
}
});
});
})
</script>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div id="form1">
<div class=" col-md-10 col-md-offset-1">
<div class="form-group col-md-3">
<label for="titre">date</label>
</div>
<div class="form-group col-md-5">
<input type="date" name="dateS" id="dateS" class="form-control">
</div>
</div>
<div class="col-md-6 col-md-offset-3">
<div class="form-group col-md-2">
<button class="btn btn-primary" id="hide" data-url="">show</button>
</div>
</div>
</div>
<div id="form2">
<div class="col-md-10 col-md-offset-1">
<h4>dateS : <span id="da"></span></h4>
</div>
<table class="table table-bordered" id="mytable">
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>nom</th>
<th>matricule</th>
<th>adresse</th>
<th>prime</th>
</tr>
<tr>
<td><input type="checkbox" class="checkbox"></td>
<td>najib</td>
<td>52</td>
<td>tihit</td>
<td><input type="text" name="prime" class="form-control prime" value="0"></td>
</tr>
<tr>
<td><input type="checkbox" class="checkbox"></td>
<td>adil</td>
<td>62</td>
<td>tagmast</td>
<td><input type="text" name="prime" class="form-control prime" value="0"></td>
</tr>
</table>
<div class="form-group col-md-offset-5 ">
<button class="btn btn-success add-all" type="submit" id="hide">Pointage men</button>
</div>
</div>

Related

jQuery validation on dynamic table form not working on using blur

I am creating form dynamically which has n rows (coming from array having n elements). Form fields have two type of columns (text & number) which I have given different id.
Although JQuery validation is working but "validate parts button" is not responding correctly. From the code condition it should not be enabled in case text is not valid.
Thanks..!
<div class="container-sm">
<div class="col-md-12 text-center">
<input class="btn btn-outline-primary" type="button" onclick = "buildTagPartsTable()" id="partNum" style="height:40px; width:220px" value="Enter Form Details#">
</div>
</div>
<div class="container-sm">
<div class="row">
<form id = "partsQuantForm">
<div class="col-lg-12" style="width:700px;overflow:auto; max-height:400px;">
<div class="partsTagsForm">
<table class="table table-striped" id="validTagsParts" style="width:100%;">
<thead>
<tr>
<th>Valid Row</th>
<th>Part </th>
<th># </th>
<th>Part </th>
<th># </th>
</tr>
</thead>
<tbody id="tagsPartsTable"></tbody>
</table>
</div>
<div class="col-md-12 text-center">
<input class="btn btn-outline-primary" type="button" id="tagsParts" style="height:40px; width:220px" value="Validate Parts ">
</div>
</div>
</form>
</div>
</div>
<script>
function buildTagPartsTable(){
validTagsInfo = ['Row1', 'Row2', 'Row3', 'Row4', 'Row5'];
var table = "<table>";
for (let key in validTagsInfo) {
table += `<tr>
<td>${validTagsInfo[key]}</td>
<td><input type="text" class="form-control" id = "partInfo" name="partInfo[0]" style="width:5em" minlength="5" maxlength="5" ></td>
<td><input type="number" class="form-control" id = "partQuantInfo" name="partQuantInfo[0]" style="width:3em" minlength="1" maxlength="1"></td>
<td><input type="text" class="form-control" id = "partInfo" name="partInfo[1]" style="width:5em" minlength="5" maxlength="5"></td>
<td><input type="number" class="form-control" id = "partQuantInfo" name="partQuantInfo[1]" style="width:3em" minlength="1" maxlength="1"></td>
</tr>`;
}
table += "</table>";
document.getElementById("tagsPartsTable").innerHTML = table;
}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation#1.17.0/dist/jquery.validate.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$('input').on('blur', function() {
if ($("#partsQuantForm").valid()) {
$('#tagsParts').prop('disabled', false);
} else {
$('#tagsParts').prop('disabled', true);
}
});
jQuery("#partsQuantForm").validate({
rules: {
'partInfo[]': {
required: true,minlength: 5,maxlength: 5
},
'partQuantInfo[]': {
required: true,minlength: 2,maxlength: 2
}
}
});
});
</script>

$(...).cells is undefined in jQuery

i have two forms, form1 contains the date and the display button, the show button allows you to hide the form1 and display the form2, the form2 contains the chosen date and a datatable with checkboxes and button valider, i want when i click the show button console show matricules of salaries.
$(document).ready(function() {
$('#check_all').on('click', function(e) {
if ($(this).is(':checked', true)) {
$(".checkbox").prop('checked', true);
} else {
$(".checkbox").prop('checked', false);
}
});
$('.checkbox').on('click', function() {
if ($('.checkbox:checked').length == $('.checkbox').length) {
$('#check_all').prop('checked', true);
} else {
$('#check_all').prop('checked', false);
}
});
$("#form2").hide();
$("#hide").click(function() {
$("#form1").hide();
$("#form2").show();
let dat = $("#dateS").val();
$("#da").text(dat);
$("tr").each(function(i, r) {
let mat = $(r).cells[2].innerText;
console.log(mat);
});
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- form 1 -->
<div id="form1">
<div class=" col-md-10 col-md-offset-1">
<div class="form-group col-md-3">
<label for="titre">date</label>
</div>
<div class="form-group col-md-5">
<input type="date" name="dateS" id="dateS" class="form-control">
</div>
</div>
<div class="col-md-6 col-md-offset-3">
<div class="form-group col-md-2">
<button class="btn btn-primary" id="hide" data-url="">show</button>
</div>
</div>
</div>
<!-- form 2 -->
<div id="form2">
<div class="col-md-10 col-md-offset-1">
<h4>dateS : <span id="da"></span></h4>
</div>
<table class="table table-bordered" id="mytable">
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>nom</th>
<th>matricule</th>
<th>adresse</th>
<th>prime</th>
</tr>
<tr>
<td><input type="checkbox" class="checkbox"></td>
<td>najib</td>
<td>52</td>
<td>tihit</td>
<td><input type="text" name="prime" class="form-control prime" value="0"></td>
</tr>
<tr>
<td><input type="checkbox" class="checkbox"></td>
<td>adil</td>
<td>62</td>
<td>tagmast</td>
<td><input type="text" name="prime" class="form-control prime" value="0"></td>
</tr>
</table>
<div class="form-group col-md-offset-5 ">
<button class="btn btn-success add-all" type="submit" id="hide">valider</button>
</div>
</div>
Instead of $(r).cells[2].innerText use jQuery
$(r).find('td').eq(2).text()
or:
$(this).find('td:eq(2)').text();
cells is used in vanilla JavaScript to get the cells from TableRowElement, while your $(r) is a reference to a collection of elements wrapped into a jQuery object
therefore if you want to use JavaScript you still can, but like:
r.cells[2].textContent
also, notice the use of the preferred textContent vs innerText

best approach in adding a comma to a number like 123,456.78 in jquery

found this solution in this solution
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return val;
}
in this link add commas to a number in jQuery
I am trying to apply it in my project. it works in one column but it doesn't work in price and sub total
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return val;
}
var rowCount = 1;
$('#add').click(function() {
rowCount++;
$('#orders').append('<tr id="row'+rowCount+'"><td><input class="form-control product_price" type="number" data-type="product_price" id="product_price_'+rowCount+'" name="product_price[]" for="'+rowCount+'"/></td><input class="form-control" type="hidden" data-type="product_id" id="product_id_'+rowCount+'" name="product_id[]" for="'+rowCount+'"/><td><input class="form-control quantity" type="number" class="quantity" id="quantity_'+rowCount+'" name="quantity[]" for="'+rowCount+'"/> </td><td><input class="form-control total_cost" type="text" id="total_cost_'+rowCount+'" name="total_cost[]" for="'+rowCount+'" readonly/> </td><td><button type="button" name="remove" id="'+rowCount+'" class="btn btn-danger btn_remove cicle">-</button></td></tr>');
});
// Add a generic event listener for any change on quantity or price classed inputs
$("#orders").on('input', 'input.quantity,input.product_price', function() {
getTotalCost($(this).attr("for"));
});
$(document).on('click', '.btn_remove', function() {
var button_id = $(this).attr('id');
$('#row'+button_id+'').remove();
});
// Using a new index rather than your global variable i
function getTotalCost(ind) {
var qty = $('#quantity_'+ind).val();
var price = $('#product_price_'+ind).val();
var totNumber = (qty * price);
var tot = totNumber.toFixed(2);
tot = commaSeparateNumber(totNumber);
$('#total_cost_'+ind).val(tot);
calculateSubTotal();
}
function calculateSubTotal() {
var subtotal = 0;
$('.total_cost').each(function() {
subtotal += parseFloat($(this).val());
});
$('#subtotal').val(commaSeparateNumber(subtotal));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="col-md-12">
<div class="line line-dashed line-lg pull-in"></div>
<div class="row">
<table class="table table-bordered" id="orders">
<tr>
<th>Price</th>
<th>Quantity</th>
<th>Total Cost</th>
<th>
</th>
</tr>
<tr>
<td><input class="form-control product_price" type='number' data-type="product_price" id='product_price_1' name='product_price[]' for="1"/></td> <!-- purchase_cost -->
<td><input class="form-control quantity" type='number' id='quantity_1' name='quantity[]' for="1"/></td>
<td><input class="form-control total_cost" type='text' id='total_cost_1' name='total_cost[]' for='1' readonly/></td>
<td><button type="button" name="add" id="add" class="btn btn-success circle">+</button></td>
</tr>
</table>
<input class="form-control" type='hidden' data-type="product_id_1" id='product_id_1' name='product_id[]'/>
</div>
</div>
<div class="line line-dashed line-lg pull-in" style="clear: both;"></div>
<div class="col-md-12 nopadding">
<div class="col-md-4 col-md-offset-4 pull-right nopadding">
<div class="col-md-8 pull-right nopadding">
<div class="form-group">
<td><input class="form-control subtotal" type='text' id='subtotal' name='subtotal' readonly/></td>
</div>
</div>
<div class="col-md-3 pull-right">
<div class="form-group">
<label>Subtotal</label>
</div>
</div>
</div>
</div>
</body>
</html>
Thank you so much in advance!
The problem is that parseFloat fails to correctly parse the value from the total cost textbox because it contains one or more ,. You need to remove the , from the string before parsing to float. You can remove any ,'s by calling .replace(/,/g,'') on the textbox value in the calculateSubTotal function.
It seems that for some values the function commaSeparateNumber doesn't work as expected. I removed it from the snippet below and replaced it with toLocaleString() which takes parameters for locale and minimum/maximum fraction digits .toLocalString("en-US", { maximumFractionDigits: 2}). Set the locale to one you know uses , to separate thousands. If you know that won't be an issue you can pass undefined for the locale.
Your snippet has been updated below:
var rowCount = 1;
$('#add').click(function() {
rowCount++;
$('#orders').append('<tr id="row'+rowCount+'"><td><input class="form-control product_price" type="number" data-type="product_price" id="product_price_'+rowCount+'" name="product_price[]" for="'+rowCount+'"/></td><input class="form-control" type="hidden" data-type="product_id" id="product_id_'+rowCount+'" name="product_id[]" for="'+rowCount+'"/><td><input class="form-control quantity" type="number" class="quantity" id="quantity_'+rowCount+'" name="quantity[]" for="'+rowCount+'"/> </td><td><input class="form-control total_cost" type="text" id="total_cost_'+rowCount+'" name="total_cost[]" for="'+rowCount+'" readonly/> </td><td><button type="button" name="remove" id="'+rowCount+'" class="btn btn-danger btn_remove cicle">-</button></td></tr>');
});
// Add a generic event listener for any change on quantity or price classed inputs
$("#orders").on('input', 'input.quantity,input.product_price', function() {
getTotalCost($(this).attr("for"));
});
$(document).on('click', '.btn_remove', function() {
var button_id = $(this).attr('id');
$('#row'+button_id+'').remove();
});
// Using a new index rather than your global variable i
function getTotalCost(ind) {
var qty = $('#quantity_'+ind).val();
var price = $('#product_price_'+ind).val();
var totNumber = (qty * price);
// .toLocaleString
var tot = totNumber.toLocaleString("en-US", { maximumFractionDigits: 2});
$('#total_cost_'+ind).val(tot);
calculateSubTotal();
}
function calculateSubTotal() {
var subtotal = 0;
$('.total_cost').each(function() {
// replace ',' here
subtotal += parseFloat($(this).val().replace(/,/g,''));
});
// toLocaleString
$('#subtotal').val(subtotal.toLocaleString("en-US", { maximumFractionDigits: 2}));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="col-md-12">
<div class="line line-dashed line-lg pull-in"></div>
<div class="row">
<table class="table table-bordered" id="orders">
<tr>
<th>Price</th>
<th>Quantity</th>
<th>Total Cost</th>
<th>
</th>
</tr>
<tr>
<td><input class="form-control product_price" type='number' data-type="product_price" id='product_price_1' name='product_price[]' for="1"/></td> <!-- purchase_cost -->
<td><input class="form-control quantity" type='number' id='quantity_1' name='quantity[]' for="1"/></td>
<td><input class="form-control total_cost" type='text' id='total_cost_1' name='total_cost[]' for='1' readonly/></td>
<td><button type="button" name="add" id="add" class="btn btn-success circle">+</button></td>
</tr>
</table>
<input class="form-control" type='hidden' data-type="product_id_1" id='product_id_1' name='product_id[]'/>
</div>
</div>
<div class="line line-dashed line-lg pull-in" style="clear: both;"></div>
<div class="col-md-12 nopadding">
<div class="col-md-4 col-md-offset-4 pull-right nopadding">
<div class="col-md-8 pull-right nopadding">
<div class="form-group">
<td><input class="form-control subtotal" type='text' id='subtotal' name='subtotal' readonly/></td>
</div>
</div>
<div class="col-md-3 pull-right">
<div class="form-group">
<label>Subtotal</label>
</div>
</div>
</div>
</div>
</body>
</html>

How to select a table row data and send it to a Edit Modal for update data using jQuery, JavaScript and AJAX?

I have a dynamic table, which contains some data.In my table there is a option called Edit,by clicking the edit option a SideNav is opening.I want to fetch that particular row from the table and want to place those information into my sideNav.
My HTML and JS file is below.
/index.html
<table class="table-bordered mytable">
<thead class="table-head">
<tr>
<th>Name</th>
<th>Email</th>
<th>Status</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody class="table-body mytabBody">
</tbody>
</table>
<div id="mySidenav" class="sidenav">
<div class="border">
×
</div>
<div class="border">
<form class="navbar-form" id="editUserInfo">
<div class="form-group">
<input type="text" class="form-control" id="user_name" placeholder="User name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="email_id" placeholder="Enter email">
</div>
<div class="form-group">
<input type="text" class="form-control" id="address" placeholder="Enter Address">
</div>
<button type="submit" value="signin" class="btn btn-custom" onclick="editUser()">Edit</button>
</form>
</div>
</div>
/table.js
$(function(){
$.get("/api/dashboard/v1", function (response) {
// console.log(response);
var myhtmlsec= "";
for(var i=0; i<response.data.length; i++) {
myhtmlsec += "<tr>";
myhtmlsec +="<td>"+response.data[i].user_name+"</td>";
myhtmlsec +="<td>"+response.data[i].email+"</td>";
myhtmlsec +="<td></td>";
myhtmlsec +="<td>"+response.data[i].address+"</td>";
myhtmlsec +="<td>\
<a href='#' onclick='myEdit(this);return false;' class='table-edit img-size'>\
<img src='image/Edit.png'>\
</img>\
</a>\
<a href='#' onclick='myDelete();return false;' class='table-delete img-size'>\
<img src='image/Delete.png'>\
</img>\
</a>\
</td>";
myhtmlsec +="</tr>"
}
$('.table-body').append(myhtmlsec);
});
});
function myEdit(a) {
document.getElementById("mySidenav").style.width = "250px";
/*console.log(a);
var b = $(a).closest('tr');
console.log(b);
*/
};
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
};
function editUser() {
var par = $(this).parent().parent();
var tdName = par.children("td:nth-child(1)");
var tdEmail = par.children("td:nth-child(2)");
var tdAddress = par.children("td:nth-child(3)");
//want to edit/update userinformation through this "api/updateUser/v1:id" using put method
}
function myDelete() {
alert();
};
using jquery
function myEdit(elem)
{
$('#user_name').val($(elem).parent().parent().find('td:nth-child(1)').html());
...
/* fill other fields */
}
Here is perfect and clean solution:
$('tbody tr').click(
function(){
var name = $(this).find("td:nth-child(1)").html();
var email = $(this).find("td:nth-child(2)").html();
var addr = $(this).find("td:nth-child(3)").html();
$('#user_name').val(name);
$('#email_id').val(email);
$('#address').val(addr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<table class="table-bordered mytable">
<thead class="table-head">
<tr>
<th>Name</th>
<th>Email</th>
<th>Status</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody class="table-body mytabBody">
<tr>
<td>Pravin</td>
<td>pravin#gmail.com</td>
<td>True</td>
<td>Nagpur</td>
<td>Click</td>
</tr>
</tbody>
</table>
<div id="mySidenav" class="sidenav">
<div class="border">
×
</div>
<div class="border">
<form class="navbar-form" id="editUserInfo">
<div class="form-group">
<input type="text" class="form-control" id="user_name" placeholder="User name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="email_id" placeholder="Enter email">
</div>
<div class="form-group">
<input type="text" class="form-control" id="address" placeholder="Enter Address">
</div>
<button type="submit" value="signin" class="btn btn-custom" onclick="editUser()">Edit</button>
</form>
</div>
</div>

the data in model personalDeatail.add value is not updated automatically. what am i doing wrong?

I'm trying to add two column data automatically but the data in model personalDeatail.add value is not updated automatically. what am i doing wrong?? I can get the added data in the input box but same data is not updated in the personalDetail.add? please help
index.html
<!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <script src="script/angular.min.js"></script> <script src="script/script.js"></script> <script src="script/scripts.js"></script> <title>Dynamically Add-Remove Rows from Table</title>
<link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>
<link rel="stylesheet" href="css/style.css">
</head>
<body ng-app="myapp" ng-controller="ListController">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<form ng-submit="addNew()">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th><input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" /></th>
<th>Number1</th>
<th>number2</th>
<th>Add</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="personalDetail in personalDetails">
<td>
<input type="checkbox" ng-model="personalDetail.selected"/></td>
<td>
<input type="text" class="form-control" ng-model="personalDetail.num1" required/></td>
<td>
<input type="text" class="form-control" ng-model="personalDetail.num2" required/></td>
<td>
<input type="text" class="form-control" value="{{parseFloat(personalDetail.num1)+parseFloat( personalDetail.num2)}}"
></td>
</tr>
</tbody>
</table>
<div class="form-group">
<input ng-hide="!personalDetails.length" type="button" class="btn btn-danger pull-right" ng-click="remove()" value="Remove">
<input type="submit" class="btn btn-primary addnew pull-right" value="Add New">
</div>
</form>
{{personalDetails}}
</div>
</div>
</div>
</div>
</div> </body> </html>
script.js
var app = angular.module("myapp", []); app.controller("ListController", ['$scope', function($scope) {
$scope.personalDetails = [
{
'num1':'',
'num2':'',
'add':''
}];
$scope.addNew = function(personalDetail){
$scope.personalDetails.push({
'num1': "",
'num2': "",
'add': ""
});
};
$scope.parseFloat = function(value)
{
return parseFloat(value);
}
$scope.remove = function(){
var newDataList=[];
$scope.selectedAll = false;
angular.forEach($scope.personalDetails, function(selected){
if(!selected.selected){
newDataList.push(selected);
}
});
$scope.personalDetails = newDataList;
};
$scope.checkAll = function () {
if (!$scope.selectedAll) {
$scope.selectedAll = true;
} else {
$scope.selectedAll = false;
}
angular.forEach($scope.personalDetails, function(personalDetail) {
personalDetail.selected = $scope.selectedAll;
});
};
}]);
It's happening because you don't have ng-model set for the 3rd input box,
you could write a function to calculate the sum and assign the value to the $scope variable.
Demo
var app = angular.module("myapp", [])
app.controller("ListController", ['$scope', function($scope) {
$scope.personalDetails = [{
'num1': '',
'num2': '',
'add': ''
}];
$scope.calculateSum = function(val) {
val.add = parseFloat(val.num1) + parseFloat(val.num2);
}
$scope.addNew = function(personalDetail) {
$scope.personalDetails.push({
'num1': "",
'num2': "",
'add': ""
});
};
$scope.parseFloat = function(value) {
return parseFloat(value);
}
}]);
<html>
<head>
<meta charset="UTF-8">
<script data-require="angular.js#1.4.7" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular.js"></script>
<script src="script.js"></script>
<title>Dynamically Add-Remove Rows from Table</title>
<link rel='stylesheet prefetch' href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>
</head>
<body ng-app="myapp" ng-controller="ListController">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<form ng-submit="addNew()">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>
<input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" />
</th>
<th>Number1</th>
<th>number2</th>
<th>Add</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="personalDetail in personalDetails">
<td>
<input type="checkbox" ng-model="personalDetail.selected" />
</td>
<td>
<input type="text" ng-keyup="calculateSum(personalDetail)" class="form-control" ng-model="personalDetail.num1" required/>
</td>
<td>
<input type="text" ng-keyup="calculateSum(personalDetail)" class="form-control" ng-model="personalDetail.num2" required/>
</td>
<td>
<input type="text" ng-model="personalDetail.add" class="form-control" >
</td>
</tr>
</tbody>
</table>
<div class="form-group">
<input ng-hide="!personalDetails.length" type="button" class="btn btn-danger pull-right" ng-click="remove()" value="Remove">
<input type="submit" class="btn btn-primary addnew pull-right" value="Add New">
</div>
</form>
{{personalDetails}}
</div>
</div>
</div>
</div>
</div>
</body>
</html>

Categories

Resources