We help IT Professionals succeed at work.

message Alert on an empty search

Whing Dela Cruz
on
99 Views
Last Modified: 2017-03-20
Hi experts, I wish to put message alert during search, once no item found upon searching. Codes below are working but I do not know  where to position an alert messages that would tell the user "No item". Thank you!

JavaScript:

var inp=function(){  
  var inputElem=document.getElementById("myInput");
  var buttElem=document.getElementById('myButton');
  var valueInput=inputElem.value;  
  return valueInput;
 };
  var table=function(){
  var tableElem=document.getElementById('myTable');  
  return tableElem;
};

function mySearch2(inpuArg, tableArg)
{
  var filter = inpuArg.toUpperCase();
  for(var i = 0; i < tableArg.rows.length; i++) {
    var tr = tableArg.rows[i];
    var cell = tr.cells[1];

    if (cell.textContent.substr(0,filter.length).toUpperCase() == filter) {
      tr.style.display = 'table-row';
    }
    else {
      tr.style.display = 'none';
      alert("no item");
    }
  }
}
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Except you are appending a div to a table. Rather append a <Tr> keep the markup valid

Personally I would have the message after the table
<table ...>
</table>
<div class="alert" id="alert-msg">No rows found</div>

Open in new window

JavaScript
function mySearch2(inpuArg, tableArg)
{
  var filter = inpuArg.toUpperCase();
  var alert = document.getElementById('alert-msg');
  var found = 0;
  for(var i = 0; i < tableArg.rows.length; i++) {
    var tr = tableArg.rows[i];
    var cell = tr.cells[1];

    if (cell.textContent.substr(0,filter.length).toUpperCase() == filter) {
      tr.style.display = 'table-row';
      found++;
    }
    else {
      tr.style.display = 'none';
    }
    found ? alert.style.display = 'inherit' : alert.style.display = 'none';
  }
} 

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Here is an updated version of the code
<script>
var tbl = document.getElementById('myTable');
var norows = document.getElementById('no-rows-alert');
document.getElementById('myInput').addEventListener('keyup', function() {
  var matchedRow = 0;
  for(var r = 0; r < tbl.rows.length; r++) {
    var row = tbl.rows[r];
    var matchedCell = 0;
    for (var c=0; c < row.cells.length; c++) {
      var cell = row.cells[c];
      var srch = this.value.toUpperCase();
      var cellVal = cell.textContent.substr(0, this.value.length).toUpperCase();
      if (srch == cellVal) {
        matchedCell++;
        break;
      }
    }
    matchedRow += matchedCell;
    row.style.display = matchedCell ? 'inherit' : 'none'
  }
  
  norows.style.display = matchedRow ? 'none' : 'inherit';
});
</script>

Open in new window

Working sample here

Author

Commented:
Hi Julian. Leonidas solution answered my problem, but I thank you for adding solution which I impress too. However, on your solution I wish to use button before the search will start. Instead of using KeyUp function as shown on (myInput). I actually tried it but can't achieve since of limited knowledge about this language. Thank you!
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
I am confused - the code provided by Leonidas is based on your original code and appears to have a bug (From the original code)

This logic is wrong (in my view)
    if (cell.textContent.substr(0,filter.length).toUpperCase() == filter) {
      tr.style.display = 'table-row';
    }
    else {
    }

Open in new window


The reason being that for rows that don't have the matching text the message is going to be displayed. In other words

Row 1 has matching text - first part if if fires and row is retained
Row 2 does not have matching text - second part of if fires and alert is shown - even though Row 1 was found.

There needs to be some flag to say a valid row has been found to prevent the display of the message when a row has been found.

Here is the revised code for doing it with a button.
Note this code is different from my previous post as it takes into account your code specification that only 1 cell needs to be checked - not the entire row. In your code you check cell 1 - in my sample it is cell 0.
<script>
var tbl = document.getElementById('myTable');
var norows = document.getElementById('no-rows-alert');
document.getElementById('myButton').addEventListener('click', function() {
  var matchedRow = false;
  var srch = $('#myInput').val().toUpperCase();
  for(var r = 0; r < tbl.rows.length; r++) {
    var row = tbl.rows[r];
	var cell = row.cells[0];
    var cellVal = cell.textContent.substr(0, srch.length).toUpperCase();
    if (srch == cellVal) {
        matchedRow = true;
		row.style.display = 'table-row';
    }
	else {
		row.style.display = 'none';
	}
  }
  
  norows.style.display = matchedRow ? 'none' : 'inherit';
  if (!matchedRow) {
    setTimeout(function() {
      norows.style.display = 'none'
    }, 1500)
  }
});
</script>

Open in new window


Working sample here

Author

Commented:
Hi Julian, I really appreciated the way you anticipate the problem and I really love your solution as well. As I tried running your codes its work very fine. However, on the other side of my project I've got a problem. A cloneNode table your codes won't work anymore. Here's what my project goes. I have two tables, named, myTable and myTable2. I copy a data from Mytable2 to myTable using cloneNode then do certain search using your codes. In here the codes you provided will no longer work. Its very hard for me to trace the cause. I attached here the codes to make you understand. Thank you!
samt.html
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Thank you very much Julian, Your solution is working very well. More power !

Author

Commented:
Thank you so much both of you, experts, more power and God bless!
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are welcome.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions