Perform search on HTML nagular js - javascript

I am need to perform search on the below code ..
When I type something on search textbox all table data is hiding but never coming back. what is happening here..please help me on this
my search textbox is inside the body of the table and panels are required, can't remove those panels
When I type something on search textbox all table data is hiding but never coming back. what is happening here..please help me on this
my search textbox is inside the body of the table and panels are required, can't remove those
When I type something on search textbox all table data is hiding but never coming back. what is happening here..please help me on this
my search textbox is inside the body of the table and panels are required, can't remove those
<!DOCTYPE html>
<html>
<head>
js code:
enter code here
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
// Search on name column only
$('#SearchValue').keyup(function () {
// Search Text
var search = $(this).val();
// Hide all table tbody rows
$('table tbody tr').hide();
// Count total search result
var len = $('table tbody tr: td:nth-child(1):contains("' + search + '")').length;
if (len > 0) {
// Searching text in columns and show match row
$('table tbody tr: td:contains("' + search + '")').each(function () {
$(this).closest('tr').show();
});
}
else {
$('table tbody tr').show();
}
});
});
//// Case-insensitive searching (Note - remove the below script for Case sensitive search )
//$.expr[":"].contains = $.expr.createPseudo(function (arg) {
// return function (elem) {
// return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
// };
//});
</script>
</head>
<body>
<div class="col-md-4 col-xs-12 sideBlock">
<div class="panel panel-default">
<div class="panel-heading">
Site Details Search:
<input type="text" id="SearchValue" placeholder=" search name"/>
</div>
<div class="panel-body">
<div class="table-responsive" id="SiteDetails" style="height:430px;overflow-y: auto;">
<table class="table table-hover" id="tblSiteDetails">
<tbody>
<tr class="test testremove" id="Site46">
<td class="Normal"> 1. AnotherTestSite<button type="button" class="pull-right btn btn-grey btn-xs" id="site_46" onclick="ChangeColorforSensor('Site46', 'Normal'); ShowSitePopup('[object Object]', '(38.627002, -90.199404)', 'AnotherTestSite', '46', 'Normal', '8050 West Florissant', 'CSK Ltd.', '2');">Normal</button></td>
</tr>
<tr class="test testremove" id="Site43">
<td class="Normal"> 2. CB2<button type="button" class="pull-right btn btn-grey btn-xs" id="site_43" onclick="ChangeColorforSensor('Site43', 'Normal'); ShowSitePopup('[object Object]', '(25.556044, 84.66033)', 'CB2', '43', 'Normal', 'HYD', 'CSK Ltd.', '1');">Normal</button></td>
</tr>
<tr class="test testremove" id="Site48">
<td class="Normal"> 3. Eswar-TestSite<button type="button" class="pull-right btn btn-success btn-xs" id="site_48" onclick="ChangeColorforSensor('Site48', 'Normal'); ShowSitePopup('[object Object]', '(17.385044, 78.486671)', 'Eswar-TestSite', '48', 'Normal', 'plot 203, pushparesidency, kamalanagar, medipalli, Uppal', '3334', '1');">Normal</button></td>
</tr>
<tr class="test testremove" id="Site32"><td class="Critical"> 4. FFT_Test_Site<button type="button" class="pull-right btn btn-danger btn-xs" id="site_32" onclick="ChangeColorforSensor('Site32', 'Critical'); ShowSitePopup('[object Object]', '(17.451788, 78.372044)', 'FFT_Test_Site', '32', 'Critical', 'TMTC', 'CSK Ltd.', '3');">Critical</button></td></tr>
<tr class="test testremove" id="Site35"><td class="Critical"> 5. NMC<button type="button" class="pull-right btn btn-grey btn-xs" id="site_35" onclick="ChangeColorforSensor('Site35', 'Critical'); ShowSitePopup('[object Object]', '(38.627002, -90.199404)', 'NMC', '35', 'Critical', '8050 W. Florissant Ave', 'Test Motor Corporation', '1');">Critical</button></td></tr>
<tr class="test testremove" id="Site38"><td class="Normal"> 6. PROD SETUP<button type="button" class="pull-right btn btn-grey btn-xs" id="site_38" onclick="ChangeColorforSensor('Site38', 'Normal'); ShowSitePopup('[object Object]', '(38.627002, -90.199404)', 'PROD SETUP', '38', 'Normal', 'St Louis', 'Test Motor Corporation', '1');">Normal</button></td></tr>
<tr class="test testremove" id="Site39"><td class="Critical"> 7. Plexus_Site<button type="button" class="pull-right btn btn-danger btn-xs" id="site_39" onclick="ChangeColorforSensor('Site39', 'Critical'); ShowSitePopup('[object Object]', '(-15.230583, 129.494306)', 'Plexus_Site', '39', 'Critical', 'Plexus', 'CSK Ltd.', '1');">Critical</button></td></tr>
<tr class="test testremove" id="Site40"><td class="Fair"> 8. Plexus_Test_STL<button type="button" class="pull-right btn btn-warning btn-xs fairbtnwidth" id="site_40" onclick="ChangeColorforSensor('Site40', 'Fair'); ShowSitePopup('[object Object]', '(37.065525, -90.441103)', 'Plexus_Test_STL', '40', 'Fair', '8050 W Florissant', 'CSK Ltd.', '2');">Fair</button></td></tr>
<tr class="test testremove" id="Site36"><td class="Normal"> 9. SD_Card_Site<button type="button" class="pull-right btn btn-success btn-xs" id="site_36" onclick="ChangeColorforSensor('Site36', 'Normal'); ShowSitePopup('[object Object]', '(51.50735, -0.127758)', 'SD_Card_Site', '36', 'Normal', 'Test', 'CSK Ltd.', '1');">Normal</button></td></tr>
<tr class="test testremove" id="Site33"><td class="Normal"> 10. Sam Test Site<button type="button" class="pull-right btn btn-grey btn-xs" id="site_33" onclick="ChangeColorforSensor('Site33', 'Normal'); ShowSitePopup('[object Object]', '(28.084589, 104.979385)', 'Sam Test Site', '33', 'Normal', 'TMTC, Hyderabad', 'CSK Ltd.', '1');">Normal</button></td></tr>
<tr class="test testremove" id="Site44"><td class="Normal"> 11. T123<button type="button" class="pull-right btn btn-success btn-xs" id="site_44" onclick="ChangeColorforSensor('Site44', 'Normal'); ShowSitePopup('[object Object]', '(37.061973, -97.038371)', 'T123', '44', 'Normal', 'XXX', 'TestCmp', '0');">Normal</button></td></tr>
<tr class="test testremove" id="Site3"><td class="Normal"> 12. TMTC_Subscriber_Site<button type="button" class="pull-right btn btn-grey btn-xs" id="site_3" onclick="ChangeColorforSensor('Site3', 'Normal'); ShowSitePopup('[object Object]', '(55.632955, 99.223156)', 'TMTC_Subscriber_Site', '3', 'Normal', 'Begumpet, Hyderabad', 'CSK Ltd.', '1');">Normal</button></td></tr>
<tr class="test testremove" id="Site42"><td class="Critical"> 13. TestSite01<button type="button" class="pull-right btn btn-danger btn-xs" id="site_42" onclick="ChangeColorforSensor('Site42', 'Critical'); ShowSitePopup('[object Object]', '(62.3919, 6.598008)', 'TestSite01', '42', 'Critical', 'Vijayawada', 'CSK Ltd.', '1');">Critical</button></td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>

You selector has some issues there:
`.table tbody tr td:nth-child(1):contains(${search})`
JQuery:
$(document).ready(function () {
// Search on name column only
$('#SearchValue').keyup(function () {
// Search Text
var search = $(this).val();
// Hide all table tbody rows
$('table tbody tr').hide();
const select2 = `.table tbody tr td:nth-child(1):contains(${search})`;
// Count total search result
var len = $(select2).length;
console.log(select2);
if (len > 0) {
// Searching text in columns and show match row
$(select2).each(function () {
$(this).closest('tr').show();
});
}
else {
$('table tbody tr').show();
}
});
});
https://jsfiddle.net/ramseyfeng/p34bfyzw/

Related

Bootstrap 4 - DataTable search not working on rows added by jQuery

I have a problem with the Bootstrap 4 search tool. If the rows are added manually in tbody the search works, but if I add a new row from jQuery then search doesn't see those rows.
This is the body of the table.
Below is the function that adds a new row to the table:
function addProduct(name, id, price, quantity = 1)
{
var template = $("#product-template").clone(true, true);
var existingRow = $("#product-table").find('#product' + id);
template.removeClass("disabled");
if (existingRow.length > 0)
{
var quantity = parseInt(existingRow.find('.prod-quantity').text()) + 1;
existingRow.find('.prod-quantity').text(quantity);
calculateTotalPrice(id);
return;
}
template.attr("unit-price", price);
template.attr("id", "product" + id);
template.find('.prod-name').attr("title", name);
template.attr("productID", id);
template.find('.prod-name').text(name);
template.find('.prod-quantity').text(quantity);
template.find('.total-price').text(price);
$(template).css("display", "table-row");
$("#product-table").append(template);
calculateTotalPrice(id);
orderTableData();
calculateTotalPayment();
}
<tbody id="product-table" class="prd">
<tr id="product-template" class="tr-prod disabled">
<td class="text-center nr-order" id=""></td>
<td class="prod-name" contenteditable="true" title="" style="background-color: white; color:black;">a</td>
<td class="prod-quantity text-center" contenteditable="true" style="background-color: white; color:black">1</td>
<td class="total-price text-center"></td>
<td class="col-sm-3 text-center " style="width: 25px!important;">
<i class="fas fa-minus-circle remove" title="Elimină" style="font-size:20px;color:#F08080;width:20px">
</i>
</td>
</tr>
<tr id="product-template-1" class="tr-prod disabled">
<td class="text-center nr-order" id=""></td>
<td class="prod-name" contenteditable="true" title="" style="background-color: white; color:black;">b</td>
<td class="prod-quantity text-center" contenteditable="true" style="background-color: white; color:black">1</td>
<td class="total-price text-center"></td>
<td class="col-sm-3 text-center " style="width: 25px!important;">
<i class="fas fa-minus-circle remove" title="Elimină" style="font-size:20px;color:#F08080;width:20px">
</i>
</td>
</tr>
</tbody>
I don't know why, but the rows added with the addProduct() function don't appear in the search, and not only that, but they disappear completely when I'm searching for something.
What am I missing, maybe I should update the table after I add a new row?
<script>
$(document).ready(function () {
var table = $('#product-table').DataTable();
$('.dataTables_length').addClass('bs-select');
function addProduct(name, id, price, quantity = 1)
//instead $("#product-table").append(template)
table.row.add([
'td 1',
'td 2',
'td 3',
...
'td n',
])
table.draw();
}
});
</script>

How to pass the value to the bootbox in the loop

I have a problem. How to pass the value of the variable to the bootbox in the loop.
I have an array taken from the database and displayed in a loop:
<tr>
<td>Order nr1</td>
<td></td>
<td>
<button id="confirm-delete-modal" type="button" class="btn btn-danger btn-sm">Delete</button>
</td>
</tr>
<tr>
<td>Order nr2</td>
<td></td>
<td>
<button id="confirm-delete-modal" type="button" class="btn btn-danger btn-sm">Delete</button>
</td>
</tr>
And Js Bootbox:
$('#confirm-delete-modal').click(function() {
var tesst = "aaa";
bootbox.confirm({
message: "Delete?",
buttons: {
confirm: {
label: 'YES',
className: 'btn-danger'
},
cancel: {
label: 'NO',
className: 'btn-success'
}
},
callback: function (result) {
if(result)
{
window.location.href = "/"+ tesst
}
}
});
});
I would like to pass a separate Id to the link for each row in the loop.How can I do this?
I added a data-index="" to the HTML, this way you can send "parameters" to your click function.
$('.btn').click(function() {
var index = $(this).data("index");
alert(index);
});
<button id="confirm-delete-modal" type="button"
class="btn btn-danger btn-sm" data-index="1">Delete</button>
<button id="confirm-delete-modal" type="button"
class="btn btn-danger btn-sm" data-index="2">Delete</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Notice that I also changed your selector, since it was selecting just one HTML element id (you have two elements with the same id). Try to create a new class and selecting this class instead (in this example I used just the normal btn class).

How to edit a cell and delete a row by clicking icon in Angular JS 1.3?

Still be a angular learner.
I have my page like
The code is:
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div ng-controller="EmpCtrl">
<div>
<h2 align="center">Angular JS Basic Example</h2>
<h5 align="center">Employee List</h5>
<table border="1" cellpadding="10" align="center">
<tr>
<th>
Edit
</th>
<th>
Employee Id
</th>
<th>
Employee Name
</th>
<th>
Address
</th>
<th>
Email Id
</th>
<th>
Delete
</th>
</tr>
<tr ng-repeat="emp in employees">
<td>
<a href="#" class="btn btn-default btn-sm">
<i class="glyphicon glyphicon-edit"></i>
</a>
</td>
<td>
{{emp.EmployeeId}}
</td>
<td>
{{emp.EmployeeName}}
</td>
<td>
{{emp.Address}}
</td>
<td>
{{emp.EmailId}}
</td>
<td>
<a href="#"
onclick="return confirm('Delete this Employee?');"
class="btn btn-default btn-sm">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
</table>
</div>
</div>
I have the js code for getting all employees. It works well.
app.controller("EmpCtrl", function ($scope, EmployeeService) {
GetAllEmployee();
function GetAllEmployee() {
var getAllEmployee = EmployeeService.getEmployee();
getAllEmployee.then(function (emp) {
$scope.employees = emp.data;
}, function () {
alert('Data not found');
});
}
});
My asp mvc service code to delete methos is
[HttpDelete]
public void DeleteEmployee(int id)
{
using (DatabaseContext db = new DatabaseContext())
{
var employeeList = db.EmployeeModels.ToList();
var temp = employeeList.Find(x => x.EmployeeId == id);
employeeList.Remove(temp);
db.SaveChanges();
}
}
I want to click the edit/delete icon to edit or delete, I guess that I have to add other javascript functions to do that.(Maybe ng-clcik??) But I don't know exactly, so how to edit a cell or delete a row?
//html
<td>
<a href="#"
ng-click="delFn(emp.EmployeeId,$index)"
class="btn btn-default btn-sm">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
//this way u can achieve it
$scope.delFn = function(id, index){
var response = confirm('Delete this Employee?');
if(response) {
$http.delete('url/' + id).then(function(result) {
result.data ? $scope.employees.splice(index, 1) : null;
}
}
}
This will depend on how your UI looks. Delete is easy, just declare a function in your controller and pass the index of the row to the function like this:
In your controller:
$scope.delFn = function(index){
var response = confirm('Delete this Employee?');
if(response) {
$scope.employees.splice(index, 1);
}
}
In your view: instead of onclick, use ng-click
<td>
<a href="#"
ng-click="delFn($index)"
class="btn btn-default btn-sm">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
where $index is implicitly available from ng-repeat. Or you can do this to make it explicit stated.
<tr ng-repeat="emp in employees track by $index">
Can you figure out the edit yourself? since you are using table tag. It looks like you can make a popup/modal, pass in the index of the row in your edit function, get the selected employee from $scope.employees, display the selected one in the popup and make it editable with input tag.
<a href="#"
ng-click="delFn(emp.EmployeeId)"
class="btn btn-default btn-sm">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
//this way u can achieve it
$scope.delFn = function(id){
EmployeeService.delEmployee(id).then(function (data) {
GetAllEmployee();
}, function () {
alert('Data not found');
});
}
then add service
this.delEmployee= function (id) {
return $http.post('controller/delEmployee', data={id:id});
}

Knockout onClick with dynamic data as parameter

I have an addToCart function I'd like to utilize that is NOT in my viewModel.
In my view model I have an object {"items":[ {"name":"siren","id":2,"image":"s3.amazon.com"}])
And in my knockout app:
<div id="cart" class="shopify-buy__cart-scroll">
<div class="shopify-buy__cart-items" data-bind="foreach: newcart.items">
<div class="shopify-buy__cart-item">
<div data-bind="style: { 'background-image': 'url(' + images + ')'}" class="shopify-buy__cart-item__image" alt="Product" style="background-repeat:no-repeat;background-size: contain;"></div>
<span class="shopify-buy__cart-item__title" data-bind="text: name"></span>
<span class="shopify-buy__cart-item__price" data-bind="text: price "></span>
<div class="shopify-buy__quantity-container">
<button class="shopify-buy__btn--seamless shopify-buy__quantity-decrement" type="button">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M4 7h8v2H4z"></path></svg>
</button>
<input class="shopify-buy__quantity shopify-buy__cart-item__quantity-input" type="number" min="0" aria-label="Quantity" data-bind="attr: {value: quantity}" style="height: 30px; border:solid 1px #d3dbe2 !important;padding-left:13px;" />
<button class="shopify-buy__btn--seamless shopify-buy__quantity-increment" type="button" databind="click: addToCard(id)" >
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M12 7H9V4H7v3H4v2h3v3h2V9h3z"></path></svg>
</button>
</div>
</div>
</div>
<div class="shopify-buy__cart-bottom">
<p class="shopify-buy__cart__subtotal__text" >SUBTOTAL</p>
<p class="shopify-buy__cart__subtotal__price"data-bind="text: total"></p>
<p class="shopify-buy__cart__notice">Shipping and discount codes are added at checkout.</p>
<button class="shopify-buy__btn shopify-buy__btn--cart-checkout" type="button">CHECKOUT</button>
</div>
</div>
In this line: <button class="shopify-buy__btn--seamless shopify-buy__quantity-decrement" type="button">
I want to add some a function with a dynamic parameter.
<button class="shopify-buy__btn--seamless shopify-buy__quantity-incriment" type="button" databind="attr: {onclick:addToCart( id ) }">
But this doesn't work. How can I do this without adding the function to my viewmodel. It's only an object and I'd like to keep it that way.
Thanks!
how about using the html5 data attribute. to get your knockout id into your click function.
data-bind="attr: {'data-idfromko': id }"
run snippet below.
function model() {
var self = this;
this.theArray = ko.observableArray([{
'id': 1,
'foo': 'bar'
}, {
'id': 2,
'foo': 'bar2'
}, {
'id': 3,
'foo': 'bar3'
}, ]);
}
var mymodel = new model();
$(document).ready(function() {
ko.applyBindings(mymodel);
$('.button').click(function(event) {
value = $(this).data("idfromko" )
alert(value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table>
<thead>
<tr>
<th>id</th>
<th>foo</th>
<th></th>
</tr>
<</thead>
<tbody data-bind=foreach:theArray>
<tr>
<td data-bind="text:id"></td>
<td data-bind="text:foo"></td>
<td>
<button class="button"
data-bind="attr: {'data-idfromko': id }">
click
</button>
</td>
</tr>
</tbody>
</table>

Change button to link in html table?

I have buttons in my html table. When I click on button I want to change link button to "Un-Extend" and I click on "Un-Extend" switch back previous button. How can I do that?
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#item.MemberCustomerName
</td>
<td>
#item.PositionCodeDescription
</td>
<td>
#item.MemberMasterCustomer
</td>
<td>
#item.MemberCustomerEmail
</td>
<td>
#if (item.EndDate != null)
{
#item.EndDate.Value.ToShortDateString()
}
</td>
<td>
<button id="btnExtend" type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#saveExtend" data-value='"#item.CommitteeMasterCustomer"'>Extend</button>
</td>
</tr>
}
Do you want like this.
<button id="btnExtend" type="button" class="btn btn-info btn-xs un-extended" data-toggle="modal" data-target="#saveExtend" data-value='"#item.CommitteeMasterCustomer"'>Extend</button>
jQuery(document).on("click",".un-extended",function(){
if(!$(this).hasClass("extend")){
$(this).addClass("extend");
$(this).text("Un-Extend");
}else{
$(this).removeClass("extend");
$(this).text("Extend");
}
});
You can do something like this: DEMO
$('button').click(function(){
if($(this).text() == "Extend") {
$(this).text('Un-Extend');
} else {
$(this).text('Extend');
}
});
Do you mean change the button tag to a tag?
As the button elements are inside the loop iteration, you can't use the same ID(s) name, instead put classname btnExtend(you name it), and register click handler for its, like following :
$(document).on('click', '.btnExtend', function() {
$(this).text(($(this).text() == 'Extend' ? 'Un-Extended' : 'Extend'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="btnExtend" type="button" class="btn btn-info btn-xs btnExtend" data-toggle="modal" data-target="#saveExtend" data-value='"#item.CommitteeMasterCustomer"'>Extend</button>
You might want to try this.
$("#btnExtend").on("click",function(){
$(this).text($(this).text() == 'Extend' ? 'Un-Extend': 'Extend');
});
Change into below code and apply below funion of jquery into code:
tbody>
#foreach (var item in Model)
{
<tr>
<td>
#item.MemberCustomerName
</td>
<td>
#item.PositionCodeDescription
</td>
<td>
#item.MemberMasterCustomer
</td>
<td>
#item.MemberCustomerEmail
</td>
<td>
#if (item.EndDate != null)
{
#item.EndDate.Value.ToShortDateString()
}
</td>
<td>
<button id="btnExtend" type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#saveExtend" data-value='"#item.CommitteeMasterCustomer"' onclick ="showhide(this);" >Extend</button>
</td>
</tr>
}
<script> function showhide(victim)
{
var text = $(victim).closest('td').find('input:button').text();
if (text == "Extend") {
$(victim).closest('td').find('input:button').text('Un-Extend');
}
else {
$(victim).closest('td').find('input:button').text('Extend');
}
} </script>

Categories

Resources