473,698 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11522
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.getEle mentById('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.parentNo de.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.childNod es;

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><ti tle>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.create TextNode(newTex t);

// 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.create Element('span') ;
spanNode.append Child(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.l ength > 0) {
x.removeChild(x .childNodes[0]);
}

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

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

if (document.getEl ementById) {
var rowRef = document.getEle mentById(r);
} else if (document.all) {
var rowRef = document.all[r];
}

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

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

if (document.getEl ementById) {
var rowRef = document.getEle mentById(r);
} else if (document.all) {
var rowRef = document.all[r];
}

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

<table>
<tr id="row0">
<td>row 0 cell 0</td>
<td>row 0 cell 1</td>
<td onclick="change CellInner(this) ">row 0 cell 2</td>
</tr>
<tr id="row1" onclick="
alert('About to delete ' + this.id);
this.parentNode .removeChild(th is);">
<td>row 1 cell 0</td>
<td>row 1 cell 1</td>
<td onclick="change Cell(this)">row 1 cell 2</td>
</table>
<button onclick="
var rowRef = document.getEle mentById('row0' );
alert('About to delete ' + rowRef.id);
rowRef.parentNo de.removeChild( rowRef);
">Delete row 0</button>
<br>
<button onclick="change Cell0('row0','0 ');
">Change row 0 cell 0 (innerHTML)</button>
<button onclick="change Cell1('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
8723
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, returned the hyperlinked page title, filename, and the body txt (30 preceding and following words) in context with the search word highlighted. Excellent.! See it working at: http://www.ipt.co.za Just search for "firearm"
5
2632
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* replace){ char* result; size_t l = strlen(source), r = strlen(replace), i; int number_of_replaces = 0; for(i = 0; i < l; i++){ if(source == search)
1
1670
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 REPLACE the next word (the nearest word from the cursor; not REPLACE ALL, but replace only one word) and SEARCH the next word. COuld some one help me? Thanks in advance.
3
8256
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;}") Thanks, Craig
6
2671
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! # Search & Replace file = open("text.txt", "r") text = file.read()
2
5094
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, the string at the beginning explains what it does: '''A script for MS Word which does the following: 1) Assigns all Hebrew italic characters "Italic" character style. 2) Assigns all Hebrew bold characters "Bold" character style. 2) Assign all...
9
2246
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 the resultpage: <%
1
7546
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 having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is quick and adaptive. I generally use a frames page for the search, in this way the search is maintained...
6
2230
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 button that does this all for me. In my head the method should go something like this:
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.