473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Refresh table when keypressed

Hej Everybody

I have tried do make an "instant search" in javascript, ie everytime one
hits a key the javascript sends the request to a database and returns
the result into a table.

The problem is occuring when the result i given. When one do a search
the given result is appended to the existing rowx in the table instead
of redrawing the table.

Example:

Element1
Element2
Element3
Element4
Element5

Search = 5

new result:

Element1
Element2
Element3
Element4
Element5
Element5

The wanted result is:

Element5

How do I make this happen?

JAVASCRIPT FUNCTIONS:

//This function is called everytime a key i press down in the input field
function search(){
var search = document.getElementById("search").value;
process(search);
}

// call server asynchronously
function process(in_value)
{

var server = "phoneXML.php?search="+in_value;

// only continue if xmlHttp isn't void
if (xmlHttp)
{
// try to connect to the server
try
{
// remove this line if you don't like the 'Receiving...' message
display("Receiving new message from server...")
// make asynchronous HTTP request to retrieve new message
xmlHttp.open("GET", server, true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}
catch(e)
{
displayError(e.toString());
}
}
}

//Parsing the xml file and puts the final result in "finalArray"
function handleServerResponse()
{
var contactArray = null;
var idArray = null;
var emailArray = null;
var phoneArray = null;
var firmaArray = null;

finalArray = Array();
// read the message from the server
var xmlResponse = xmlHttp.responseXML;

// catching potential errors with IE and Opera
if (!xmlResponse || !xmlResponse.documentElement)
throw("Invalid XML structure:\n" + xmlHttp.responseText);

// catching potential errors with Firefox
var rootNodeName = xmlResponse.documentElement.nodeName;

if (rootNodeName == "parsererror") throw("Invalid XML structure");
// obtain the XML's document element
xmlRoot = xmlResponse.documentElement;

contactArray = xmlRoot.getElementsByTagName("strContact");
idArray = xmlRoot.getElementsByTagName("idIntraContact");
emailArray = xmlRoot.getElementsByTagName("strEmail");
phoneArray = xmlRoot.getElementsByTagName("strPhone");
firmaArray = xmlRoot.getElementsByTagName("strFirma");

// generate HTML output
// iterate through the arrays and create an HTML structure
for (var i=0; i<idArray.length; i++){

finalArray[idArray.item(i).firstChild.data] = new Array();
finalArray[idArray.item(i).firstChild.data]["strContact"] =
contactArray.item(i).firstChild.data;
//Er der noget i den??
if(xmlRoot.getElementsByTagName("strEmail")[i].firstChild)
finalArray[idArray.item(i).firstChild.data]["strEmail"] =
emailArray.item(i).firstChild.data;
else
finalArray[idArray.item(i).firstChild.data]["strEmail"] = "";

//Er der noget i den??
if(xmlRoot.getElementsByTagName("strPhone")[i].firstChild)
finalArray[idArray.item(i).firstChild.data]["strPhone"] =
phoneArray.item(i).firstChild.data;
else
finalArray[idArray.item(i).firstChild.data]["strPhone"] = "";
finalArray[idArray.item(i).firstChild.data]["strFirma"] =
firmaArray.item(i).firstChild.data;
}

display();//This is the one who draws
}

function display()
{
//Find the table we want to append to
var selector =
document.getElementById("selector").getElementsByT agName("tbody")[0];

for(var i in finalArray){//Run through "finalArray"

var strPhone = finalArray[i]["strPhone"]

row = document.createElement("tr");//New row

cell1 = document.createElement("td");//New cell
cell1.setAttribute("id", "product");//Cell attributes
cell1.innerHTML = finalArray[i]["strFirma"];//Cell data

cell2 = document.createElement("td");
cell2.setAttribute("id", "product");
cell2.innerHTML = "<a id='cats'
href='contacts.php?action=show&id="+ i +"'>"+
finalArray[i]["strContact"] +"</a>";

cell3 = document.createElement("td");
cell3.setAttribute("id", "product");
cell3.innerHTML = strPhone;

cell4 = document.createElement("td");
cell4.setAttribute("id", "product");
cell4.innerHTML = finalArray[i]["strEmail"];

row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
selector.appendChild(row);//Add the new row to the existing table
}
}
Sep 21 '06 #1
2 2846
Memborg wrote:
Hej Everybody

I have tried do make an "instant search" in javascript, ie everytime one
hits a key the javascript sends the request to a database and returns
the result into a table.

The problem is occuring when the result i given. When one do a search
the given result is appended to the existing rowx in the table instead
of redrawing the table.
Isn't appending exactly what your display() method sets out to do?
Nowhere does it remove any table content. Find the row(s) you want to
remove and call removeChild on their parent node.

[snip]
>
function display()
{
//Find the table we want to append to
var selector =
document.getElementById("selector").getElementsByT agName("tbody")[0];

for(var i in finalArray){//Run through "finalArray"

var strPhone = finalArray[i]["strPhone"]

row = document.createElement("tr");//New row
Why don't you use inserRow() (ditto for cells)?

--
Ian Collins.
Sep 21 '06 #2
Ian Collins skrev:
Memborg wrote:
>Hej Everybody

I have tried do make an "instant search" in javascript, ie everytime one
hits a key the javascript sends the request to a database and returns
the result into a table.

The problem is occuring when the result i given. When one do a search
the given result is appended to the existing rowx in the table instead
of redrawing the table.
Isn't appending exactly what your display() method sets out to do?
Nowhere does it remove any table content. Find the row(s) you want to
remove and call removeChild on their parent node.

[snip]
>function display()
{
//Find the table we want to append to
var selector =
document.getElementById("selector").getElementsBy TagName("tbody")[0];

for(var i in finalArray){//Run through "finalArray"

var strPhone = finalArray[i]["strPhone"]

row = document.createElement("tr");//New row

Why don't you use inserRow() (ditto for cells)?
After some research I found that there is a deleteRow() function and i
work for me.

I have no idea why I don't use insertRow(). I wasn't aware of the
function i guess.
Sep 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Scott | last post by:
I have a clickable graph that resides on page 1. If user clicks a data point on the graph, the page runs again yeilding a 2nd graph that shows a more detailed graph. Problem is, I have a...
3
by: Danny | last post by:
HI How can I refresh the main project window in code in ms access 2000 For example, I create a new table, but when I minimize the form, the table is not there until I do F5 (refresh). I do...
2
by: Jeronimo Bertran | last post by:
Hi, I have a page with a very data intensive grid which needs to be automatically refreshed constantly if a change is detected. In order to not refresh the complete page so often, I created an...
17
by: Jim Little | last post by:
Hello, I'm driving myself crazy either because I'm missing something about ASP.NET, or what I'm trying to do simply can't be done. First, I am not using session variables to track state. My...
3
by: gg77 | last post by:
Hi, I don't have much knowledge of the table control. I am basically trying to use a table thats populated with data rows from a dataSet but currently things aren't working for me. below is...
7
by: Juan Romero | last post by:
Hey guys, please HELP I am going nuts with the datagrid control. I cannot get the damn control to refresh. I am using soap to get information from a web service. I have an XML writer output...
3
by: Simon Strandgaard | last post by:
Hi group, my setup: I have a GridView that extracts data from my table (a SqlDataSource). For each row, I have an 'edit' button and a 'delete' button. The 'edit' button opens a popup, through...
7
by: Lisa | last post by:
I have an Access 2000 application that uses the following function to re-link my tables when I switch from my Current back end to a Dummy back end. I also use it to refresh my links. Function...
0
by: chetu | last post by:
I have two pivot tables, but when my macro refreshes the pivot tables it refreshes all, but I want it to pick say for example; Say range "B8" of pivot table 1 is 60, I want the range "x2" of pivot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.