473,378 Members | 1,236 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,378 software developers and data experts.

Search & Replace row in table?

My page populates a table with a list of names and other information from a
JavaScript object. I receive changes (adds, change & delete) to that list,
convert it into a JavaScript object. I do understand how to add the new
names to the list.

I have 3 questions:

1) How can I search through the table rows to find the rows to be changed
or removed?

2) How can I resort the table, so the names continue to appear in
alphabetical order.

3) How can change or remove data in the cells, once I've found the
appropriate row?

Thanks!

Mike
Jul 23 '05 #1
2 11472
Mike wrote:
My page populates a table with a list of names and other information from a JavaScript object. I receive changes (adds,
change & delete) to that list, convert it into a JavaScript
object. I do understand how to add the new names to the list.
So you are after a tutorial on manipulation of tables?

I have 3 questions:

1) How can I search through the table rows to find the rows to be changed or removed?
Presumably each record in your object has a key? Put the key as
the id of the table row that the record is displayed in. When
adding or removing elements in your object, do the same to the
table.

If you have a row with id="row06":

<tr id="row06"> ... </tr>

a reference to the row can be found by:

var rowRef = document.getElementById('row06');

Code below includes support for document.all for IE 5 (& 4?).

2) How can I resort the table, so the names continue to appear in alphabetical order.
I would keep rows sorted by keeping the records in the object
sorted. Then whatever was done to the object, do to the table.
if the table is small, you could perhaps re-draw it each time.
But if it's more than say 1 screen, do it by rows. Presumably
you only change one row at a time, or the whole lot in one go?

To delete a table row, use:

rowRef.parentNode.removeChild(rowRef);

You can also add an onclick to the row to make it remove itself
(then replicate the removal in your object).
3) How can change or remove data in the cells, once I've

found ...
Changing data in the cells can be done very easily with
innerHTML or with marginally more effort DOM. The DOM method is
likely better since it is part of the W3C specification,
innerHTML isn't (and probably never will be).

With innerHTML, the text entered is interpreted, so if the user
enters HTML markup, it is parsed and displayed so:

<b>hi</b>

will display 'hi' in bold. The DOM method will show the
characters entered ('<b>hi</b>'). This likely makes it not
suitable for a web application unless you intend parsing the
text entered... I think DOM is better. e.g. suppose the user
enters:

<span onclick="alert('hi');">here is a lot of text</span>

Now when they click on the cell, an alert pops up with 'hi'.

Once you have a reference to the row, the cells can be accessed
as a collection:

var theCells = rowRef.cells

But that doesn't work in Safari 1.0.3 (it may be fixed in later
versions) so I've used:

var theCells = rowRef.cells;
if (!theCells[0]) theCells = rowRef.childNodes;

Safari creates the collection, but for some reason, you can't
access it. The childNodes method is equivalent in this case
(for Safari only, Firefox barfs on it).

The code below shows manipulation using both innerHTML and DOM.
It is heavily commented, hopefully it's understandable.

Have fun...

<html><head><title>table play</title>
<style type="text/css">
td {width: 150px; border: 1px solid blue;}
</style>
<script type="text/javascript">

// This method gets passed a reference to the cell to change.
function changeCellInner(x) {
var newText = prompt('Enter new text: ','');
x.innerHTML = (newText == '')?'<i>No entry</i>':newText;
}

// This method gets passed a reference to the cell to change.
function changeCellDOM(x) {
var newText = prompt('Enter new text: ','');

// Create a new text node
var textNode = document.createTextNode(newText);

// textNodes have no style attribute, so must wrap
// in something. Manipulating the TD style is OK, but creates
// logic issues so use a span to wrap the text node
var spanNode = document.createElement('span');
spanNode.appendChild(textNode);

// If no text entered at prompt, deal with it
if (newText == '') {
textNode.data = 'No entry';
spanNode.style.fontStyle = 'italic';
}
// Delete everything in the cell
while (x.childNodes.length > 0) {
x.removeChild(x.childNodes[0]);
}

// Add the span (& hence text node) to the cell
x.appendChild(spanNode);
}

// calls innerHTML method:
function changeCell0(r,c) {

if (document.getElementById) {
var rowRef = document.getElementById(r);
} else if (document.all) {
var rowRef = document.all[r];
}

var theCells = rowRef.cells;
if (!theCells[0]) theCells = rowRef.childNodes;
changeCellInner(theCells[c])
}

// calls DOM method:
function changeCell1(r,c) {

if (document.getElementById) {
var rowRef = document.getElementById(r);
} else if (document.all) {
var rowRef = document.all[r];
}

var theCells = rowRef.cells;
if (!theCells[0]) theCells = rowRef.childNodes;
changeCellDOM(theCells[c]);
}
</script>
</head>
<body>

<table>
<tr id="row0">
<td>row 0 cell 0</td>
<td>row 0 cell 1</td>
<td onclick="changeCellInner(this)">row 0 cell 2</td>
</tr>
<tr id="row1" onclick="
alert('About to delete ' + this.id);
this.parentNode.removeChild(this);">
<td>row 1 cell 0</td>
<td>row 1 cell 1</td>
<td onclick="changeCell(this)">row 1 cell 2</td>
</table>
<button onclick="
var rowRef = document.getElementById('row0');
alert('About to delete ' + rowRef.id);
rowRef.parentNode.removeChild(rowRef);
">Delete row 0</button>
<br>
<button onclick="changeCell0('row0','0');
">Change row 0 cell 0 (innerHTML)</button>
<button onclick="changeCell1('row0','1');
">Change row 0 cell 1 (DOM)</button>

</body>
</html>

--
Fred

Jul 23 '05 #2
Fred Oz wrote:
[...]
will display 'hi' in bold. The DOM method will show the
characters entered ('<b>hi</b>'). This likely makes it not
suitable for a web application unless you intend parsing the
text entered... I think DOM is better. e.g. suppose the user
enters:


This should have said:

"This likely makes innerHTML not suitable for a web
application..."

DOM is much preferred in this case, unless it is desirable for
users to be able to put markup in the cells?

--
Fred
Jul 23 '05 #3

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

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
1
by: Tomomichi Amano | last post by:
Hello. I want to make replace & search functions in my text editor. Thanks to the kind people here at the newsgroup, I was able to make the function. But I was not able to understand how to...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
6
by: DataSmash | last post by:
Hello, I need to search and replace 4 words in a text file. Below is my attempt at it, but this code appends a copy of the text file within itself 4 times. Can someone help me out. Thanks! #...
2
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
9
by: tomjones75 | last post by:
dear community, i want to search the content of all fields in one table in a access database. it already works for the content of one field in the table. please take a look at the code in...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
6
by: simon.robin.jackson | last post by:
Ok. I need to develop a macro/vba code to do the following. There are at least 300 corrections and its expected for this to happen a lot more in the future. Therefore id like a nice...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.