get id from a table, with a outside button - javascript

well i have a html table, and a button next to it, i try to get the id from the table when the user click that button.
This is the HTML
<table id="table_domExtra2_pro" class="col m12 bordered striped centered ">
<thead>
<tr>
<th>No.</th>
<th>PROPIETARIO DEL INMUEBLE</th>
<th>REGIMEN DE PROPIEDAD</th>
<th>COMO SE ACREDITA LA <br>POSESION DE LA PROPIEDAD</th>
<th>SUPERFICIE DE <br>LA UNIDAD</th>
</tr>
</thead>
<tbody>
<tr>
<td class="count">
</td>
<td>
<input name="propietario" id="propietario" type="text" class="validate" required>
</td>
<td>
<input name="reg_propiedad" id="reg_propiedad" type="text" class="validate" required>
</td>
<td>
<input name="pose_propiedad" id="pose_propiedad" type="text" class="validate" required>
</td>
<td>
<input name="sup_unidad" id="sup_unidad" type="text" class="validate" required>
</td>
</tr>
</tbody>
</table>
<div class="add_icon col s1 m6 ">
<a id="btn_agregar_pro" class="btn waves-effect red">Agregar</a>
</div>
<div class="add_icon col s1 m6 ">
<a id="btn_eliminar_pro" class="btn waves-effect red">Eliminar</a>
</div>
This is the js
$('#btn_eliminar_pro').on("click", function(){
var idTable3 = $(this).parent().closest("table").attr("id");
var trs4 = $("#"+idTable3+" tr").length;
if(trs4 > 2){
$("#table_domData_pro tr:last").remove();
$("#table_domExtra_pro tr:last").remove();
$("#table_domExtra2_pro tr:last").remove();
}
});
I try with .prev() to, but Im really lost, i have other tables in the document and the js just select other table, not the one previus to the button, thanks a lot for your help :D

Use siblings() of the div to find the closest table. The below gets the id of the table. I have just added siblings() to your existing code.
var idTable3 = $(this).parent().siblings().closest("table").attr("id");

Related

Show error on specific input field in html table td using jQuery

I am trying to highlight the input field which is in html table td. I am looping the html table but unable to highlight the input field if quantity is 0 in input field. what i have tried is below.
<div id="rootwizard">
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<ul>
<li>details</li>
<li>captain</li>
</ul>
</div>
</div>
</div>
<div class="tab-content">
<div class="tab-pane" id="details">
<div class="row">
<div class="col-sm-12">
<h4 class="info-text">
Let's start with the basic details.
</h4>
</div>
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-12">
<div class="persons">
<table class="table table-condensed table-hover" id="tblPurchaseOrders">
<tr>
<th>
Product Code
</th>
<th>
SKU
</th>
<th>
Product Name
</th>
<th>
Quantity
</th>
</tr>
<tr>
<td>
<input type="text" name="ProductCode" value="A" class="form-control" />
</td>
<td>
<input type="text" name="SKU" value="A1" class="form-control" />
</td>
<td>
<input type="text" name="Name1" value="A1" class="form-control" />
</td>
<td>
<input type="text" id="Quantity" value="0" class="form-control" />
</td>
</tr>
<tr>
<td>
<input type="text" name="ProductCode" value="B" class="form-control" />
</td>
<td>
<input type="text" name="SKU" value="B1" class="form-control" />
</td>
<td>
<input type="text" name="Name1" value="B1" class="form-control" />
</td>
<td>
<input type="text" id="Quantity" value="1" class="form-control" />
</td>
</tr>
</table>
</div>
</div>
</div>
<hr />
</div>
</div>
</div>
<div class="tab-pane" id="captain">
<div class="row">
<div class="form-group">
<div class="col-md-12">
<table class="table table-condensed table-hover" id="tbOrderDetail">
<tr>
<th>
Product Code
</th>
<th>
SKU
</th>
<th>
Product Name
</th>
<th>
Quantity
</th>
</tr>
</table>
</div>
</div>
</div>
</div>
<ul class="pager wizard">
<li class="previous first" style="display:none;">First</li>
<li class="previous">Previous</li>
<li class="next last" style="display:none;">Last</li>
<li class="next">Next</li>
</ul>
</div>
</div>
<div class="tab-content">
</div>
Below is my jquery code where i am looping the html table and want to highlight the input field which is in html table td.
$(document).ready(function() {
$('#rootwizard').bootstrapWizard({
onTabShow: function(tab, navigation, index) {
if (index == 1) {
$('#tblPurchaseOrders').find('tr:has(td)').each(function() {
if (parseInt(($(this).find('#Quantity')).val()) == 0)
{
//show error
}
});
}
}
});
});
You are filtering per ID (#Quantity). Two (or more) elements cannot share the same ID. Try to select elements by name, for example:
$('#tblPurchaseOrders').find('td > input[name=Quantity]').each(function() {
var val = $(this).val();
if (parseInt(val) == 0)
{
// show error
// note: if needed, you can obtain a reference
// to container <tr> using: $(this).parent('tr');
}
});
Remember to change id property of Quantity elements to name.
Note: untested code that might not work as-is, I'm just showing you the right direction.
If your intention is to use the Twitter Bootstrap Wizard (TBW from now on) for your validation flow, you probably don't want this validation to trigger when you open the tab, which is what happens, when you place your code inside of the onTabShow function.
I assume you want the validation of the fields to be done when the user wants to go to the next step, by pressing the "Next" link you have, or rather, when you want to load the "next tab". For this, you should use onNext rather than onTabShow for your validation.
$(document).ready(function() {
$('#rootwizard').bootstrapWizard({
tabClass: 'nav nav-pills',
onNext: function(tab, navigation, index) {
var res = $("input[name='Quantity']").each(function() {
var val = $(this).val();
if(parseInt(val) == 0) {
$(this).focus();
// This will stop the .each-iteration
// We only want to focus the _first_ element, otherwise it
// would keep iterating, and change focus to the next 0-valued input
return false;
}
});
// We use the result returned from running the each function
// If false was returned at any point during the .each function,
// var res will be false. Pass this onto the onNext call, to
// prevent the the user to move to next tab.
if(!res) return false;
}
});
});
For this to work, you'll have to replace id="Quantity" with name="Quantity", in both your instances.
If you want to use .focus() on the first element, regardless of which input is invalid, you should remove $(this).focus() from the previous example, and instead replace if(!res) return false with the following:
if(!res) {
$("input[name='Quantity']").first().focus();
return false;
}
Can you try this
<div id="rootwizard">
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<ul>
<li>details</li>
<li>captain</li>
</ul>
</div>
</div>
</div>
<div class="tab-content">
<div class="tab-pane" id="details">
<div class="row">
<div class="col-sm-12">
<h4 class="info-text">
Let's start with the basic details.
</h4>
</div>
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-12">
<div class="persons">
<table class="table table-condensed table-hover" id="tblPurchaseOrders">
<tr>
<th>
Product Code
</th>
<th>
SKU
</th>
<th>
Product Name
</th>
<th>
Quantity
</th>
</tr>
<tbody id="dtTable">
<tr>
<td>
<input type="text" name="ProductCode" value="A" class="form-control" />
</td>
<td>
<input type="text" name="SKU" value="A1" class="form-control" />
</td>
<td>
<input type="text" name="Name1" value="A1" class="form-control" />
</td>
<td>
<input type="text" id="Quantity" value="0" class="form-control" />
</td>
</tr>
<tr>
<td>
<input type="text" name="ProductCode" value="B" class="form-control" />
</td>
<td>
<input type="text" name="SKU" value="B1" class="form-control" />
</td>
<td>
<input type="text" name="Name1" value="B1" class="form-control" />
</td>
<td>
<input type="text" id="Quantity" name="Quantity" value="1" class="form-control" />
</td>
</tr>
</table>
</div>
</div>
</div>
<hr />
</div>
</div>
</div>
<div class="tab-pane" id="captain">
<div class="row">
<div class="form-group">
<div class="col-md-12">
<table class="table table-condensed table-hover" id="tbOrderDetail">
<tr>
<th>
Product Code
</th>
<th>
SKU
</th>
<th>
Product Name
</th>
<th>
Quantity
</th>
</tr>
</table>
</div>
</div>
</div>
</div>
<ul class="pager wizard">
<li class="previous first" style="display:none;">First</li>
<li class="previous">Previous</li>
<li class="next last" style="display:none;">Last</li>
<li class="next">Next</li>
</ul>
</div>
</div>
<div class="tab-content">
And Javascript code
$(document).ready(function () {
$('#rootwizard').bootstrapWizard({
onTabShow: function (tab, navigation, index) {
if (index == 0) {
$('#dtTable tr').each(function (i, row) {
$(this).find('[name=Quantity]').each(function () {
alert($(this).val())
})
if ($(this).val() == 1) {
///Do something
}
});
}
}
});
});

Made all the fields editable on click with angularJS

Here is the HTML, in which there is ng-repeat with which the list is fetched, and ng-readonly which has been freezed initially, and in JS on click funtion its released
<tr ng-repeat= "employeeDetail in employeeDetails">
<div ng-readonly="isReadonly">
<td><a><span>{{employeeDetail.empName}}</a></td>
<td>
<div ng-if="employeeDetail.departmentName">
{{employeeDetail.departmentName}}
</div>
<div ng-if="!employeeDetail.departmentName">
Not Specified
</div>
</td>
<td>
{{employeeDetail.empEmail}}
</td>
<td>{{employeeDetail.empContact}}</td>
<td>
<i class="fa fa-edit" ng-click="updateEmp()">
</td>
</div>
</tr>
And here is the JS file, attached to it, what I want is to make all the fields editable on click,
$scope.isReadonly = false;
$scope.addEmployeeType = function(){
var employeeDetail = new employeeDetailApi();
$scope.employeeDetails = employeeDetail.list;
}
$scope.updateEmp = function (){
$scope.isReadonly = true;
console.log("update Employee");
}
try it...
<tr ng-repeat= "employeeDetail in employeeDetails">
<div ng-click="isReadonly==true">
<td><input type="text" ng-value="employeeDetail.empName" ng-readonly="isReadonly"></td>
<td><input type="text" ng-value="employeeDetail.departmentName" ng-readonly="isReadonly"></td>
<td><input type="text" ng-value="employeeDetail.empEmail" ng-readonly="isReadonly"></td>
<td><input type="text" ng-value="employeeDetail.empContact" ng-readonly="isReadonly"></td>
<td>
<i class="fa fa-edit" ng-click="updateEmp()">
</td>
</div>
</tr>
Just update your HTML as -
<div ng-readonly="isReadonly" ng-click="!isReadonly">
That's it!

Delete table row when clicked on table data column

I want to delete a table row when i click on delete.
For this i have placed a <a> in the table row.
So onclick of that the row needs to be deleted.
I have it working for one set of rows but not for another set.
DeleteRow function:
function DeleteRow(e) {
$(e).closest(".CollectionItem").remove()
}
Works for:
<div class="collapsible-header"><i class="material-icons">place</i>Items</div>
<div class="collapsible-body" style="margin:15px">
<div id="ItemArea">
#if (Model != null)
{
foreach (var Item in Model.Items)
{
<div class="card CollectionItem">
<div class="row" style="margin:15px">
<div class="col s10">
<input placeholder="Item" id="" name="" type="text" class="validate ItemInput" value="#Item">
</div>
<div class="col s2" style="padding-top:10px">
<a class="waves-effect waves-light btn light-blue darken-3 deleteRow valign" id="DeleteItem" onclick="DeleteRow(this)">Delete</a>
</div>
</div>
</div>
}
}
</div>
<div>
Add Item
</div>
</div>
Doesn't work for:
<table id="BuildDefinitionTable">
<thead>
<tr>
<th>Team Project</th>
<th>Build Definition</th>
<th>Drop Build</th>
</tr>
</thead>
<tbody>
#if(Model != null)
{
foreach (var BuildDefinition in Model.BuildDefinitions)
{
<tr class="CollectionItem">
<td><input type='text' readonly value="#BuildDefinition.TeamProject" style="border-bottom:none"/></td>
<td><input type='text' readonly value="#BuildDefinition.Name" style="border-bottom:none"/></td>
<td><input type='text' readonly value="#BuildDefinition.IsDropBuild.ToString()" style='border-bottom:none'/></td>
<td><a class="waves-effect waves-light btn light-blue darken-3 deleteRow valign" id="DeleteItem" onclick="DeleteRow(this)">Delete</a></td>
</tr>
}
}
</tbody>
</table>
Can anyone see why this is, I cant get the delete button to even link to another page by using href.

Form is presenting same data in php while loop

I am trying to get an edit form to work with the loop in the php. I have a delete button and it works just fine. The edit form keeps presenting only the first row in the table. In firebug, it shows that all the other forms are there with the correct unique id for each but only the first form shows when edit is clicked on different rows. Can someone help me as to why?
<table id="table_id" class="table table-hover table-striped">
<thead>
<tr>
<th>ID #</th>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($search_sql)) {
?>
<tr>
<td id = "clicked"><?=$row['ID']?></td>
<td id = "clicked"><?=$row['Product']?></td>
<td id = "clicked"><?=$row['Quantity']?></td>
<td id = "clicked">$ <?=$row['Price']?></td>
<td>
<button class="btn btn-warning btn-sm" onclick="div_show2(this, '<?=$row['ID']?>'); return false;"><span class="glyphicon glyphicon-edit"></span>Edit</button>
<!-- Modal for the Edit Data Form -->
<div id ="hidden-form2">
<div id = "popupContact">
<form action= "updateData.php" id = "editForm<?=$row['ID']?>" method="POST" name="editForm">
<input type="hidden" name="ID" value="<?=$row['ID']?>">
<div class="form-group">
<label for="product">Product</label>
<input type="text" class="form-control" id="product<?=$row['ID']?>" name="product" value="<?=$row['Product']?>">
</div>
<div class="form-group">
<label for="quantity">Quantity</label>
<input type="text" class="form-control" id="quantity<?=$row['ID']?>" name="quantity" value="<?=$row['Quantity']?>">
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="text" class="form-control" id="price<?=$row['ID']?>" name="price" value="<?=$row['Price']?>">
</div>
<button type="button" class="btn btn-default" onclick="formHide2()">Close</button>
<button type="submit" id="save" class="btn btn-primary">Save changes</button>
</form>
</div>
</div>
<!--End of Edit Form-->
</td>
<td>
<!--Delete Button Form-->
<form method="post" action="deleteData.php">
<button class="btn btn-danger btn-sm" type="submit"><span class="glyphicon glyphicon-trash"></span>Delete</button>
<input type="hidden" id="ID" name="ID" value="<?=$row['ID']?>">
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
//script for calling the edit forms as a modal popup
//Function To Display Popup for Edit form
function div_show2() {
document.getElementById("editForm").reset();
document.getElementById('hidden-form2').style.display = "block";
}
//Function to Hide Popup
function formHide2(){
document.getElementById('hidden-form2').style.display = "none";
}
Your show_div2 function is only showing one element.
document.getElementById('hidden-form2').style.display = "block";
That shows you why you are only seeing the first row.
Update your HTML and Javascript as follows
HTML
<div id="hidden-form2<?=$row['ID']?>">
Javascript
function div_show2(id) {
document.getElementById("editForm"+id).reset();
document.getElementById('hidden-form2'+id).style.display = "block";
}
To avoid having any problems like this in the future, always make sure that your id's are unique.
The first 4 td I see all use the same id name clicked. Try to fix that first and see what happens.
<td class="clicked" id="clickedID<?=$row['ID']?>"><?=$row['ID']?></td>
<td class="clicked" id="clickedProduct<?=$row['ID']?>"><?=$row['Product']?></td>
<td class="clicked" id="clickedQuantity<?=$row['ID']?>"><?=$row['Quantity']?></td>
<td class="clicked" id="clickedPrice<?=$row['ID']?>">$ <?=$row['Price']?></td>

Submit multiple forms with one Submit All button in AngularJS

I have a form in Angular setup like the following.
Users can make changes and submit one at a time and it works as expected but I'd like to add a "Submit All" that would submit each item one at a time as though the user were pressing the submit button for each. Im a little stumped on the best way to do this. As of right now I cannot submit them as a batch due to API constraints. I would also like to keep jQuery out of the equation.
My first thought is to create a new object that contains the info for each item and then loop over it and submit that way. I am unsure how to set this up in my controller.
<div class="table-responsive">
<table class="table table-striped">
<thead>
<th>Name</th>
<th></th>
<th>Age</th>
<th>Kids</th>
<th></th>
</thead>
<tbody>
<tr class="pending-item animate-repeat"
data-ng-repeat="user in Users"
data-ng-form="userForm"
role="form"
data-ng-submit="submitUser(user, userDetails)"
novalidate>
<td class="img-container">
<img data-ng-src="{{user['image']['url']}}"
alt="{{user['image']['alt'] || ' '}}"
class="img-responsive" >
</td>
<td class="col-xs-6">
<div class="user-info">
<p class="user-name">
{{user['name']}}
</p>
</div>
</td>
<td>
<div class="input-group input-group-sm">
<span class="input-group-addon">Age</span>
<input type="number" min="0"
name="age"
class="form-control age"
data-ng-init="userDetails.age = user['age']"
data-ng-model="userDetails.age"
required>
</div>
</td>
<td>
<div class="input-group input-group-sm">
<input type="number" min="0" step="1"
name="kids"
class="form-control kids"
data-ng-disabled="user['kids'] === true"
data-ng-pattern="/^\d+$/"
data-ng-init="userDetails.kidsCount = user['kids']['quantity']"
data-ng-model="userDetails.kidsCount"
required>
<div class="input-group-addon"># of kids</div>
</div>
</td>
<td>
<div class="btn-group btn-col">
<button type="submit"
class="btn btn-success btn-sm"
data-ng-disabled="userForm.$invalid || userDetails.working"
data-ng-click="submitUser(user, userDetails)">
Submit
</span>
</button>
</div>
</td>
</tr>
</tbody>
</table>
<button ng-click="submitAllUsers()">Submit All Users</button>
</div>
Yes, create a object in your controller, say $scope.formData={}, and then bind it with your elements using ng-model or data-ng-model and then on button click pass this object in your controller and do the required stuff.

Categories