473,396 Members | 1,766 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,396 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 11476
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.