Subject: Selecting Data from a table
Posted By: nkrust Post Date: 1/10/2007 7:18:47 AM
Hi,
Is there any way to select data from a table using Javascript..? I understand that it will be a huge code so even if someone gives pointers to some online resource, its very much welcome too ..

------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
Reply By: joefawcett Reply Date: 1/11/2007 12:41:46 AM
Who can say, your question is too vague?
You can use JavaScript to open a connection to a table in a database using ADO, if the database supports OleDb. Or do you you mean accessing data from an HTML table in a web page?

--

Joe (Microsoft MVP - XML)
Reply By: surendran Reply Date: 1/11/2007 12:45:32 AM
hi nkrust,
You can connect with javascript but it is not feasible.
You better use ajax for reading data from database but server site languages should use for database connection,

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
Reply By: joefawcett Reply Date: 1/11/2007 1:21:41 AM
quote:
Originally posted by surendran


You can connect with javascript but it is not feasible.

surendran


I disagree, pre-ASP.NET I always used JavaScript for ASP.
If you mean that connecting via client-side code is impractical then you're right but that's entirely different.

--

Joe (Microsoft MVP - XML)
Reply By: nkrust Reply Date: 1/11/2007 3:47:30 AM
Actually I'm selecting multiple rows of a table using javascript, (I know that a listbox is the proper way to do it but with ListBox u cant customise the look). The Problem that i'm facing is that i cant get the values out of the selected fields. The code that i'm using to select(or rather highlight) the rows is:

function lockRow() {
    var tbodies = document.getElementsByTagName("tbody");
    for (var j=0; j<tbodies.length; j++) {
        var rows = tbodies[j].getElementsByTagName("tr");
        var cell = tbodies[j].getElementsByTagName("td");
        for (var i=0; i<rows.length; i++) {
            rows[i].oldClassName = rows[i].className
            rows[i].onclick = function() {
                if (this.className.indexOf("selected") != -1) {
                    this.className = this.oldClassName;
                } else {
                var text = setDataType(cell.item[i].innerText);
                    addClass(this,"selected");
                }
            }
        }
    }
}

The line in Red is where i'm trying to get the value of the selected cell, unfortunately addition of this line actually fails the code(i mean the rows don't get selected) ...

------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
Reply By: joefawcett Reply Date: 1/11/2007 3:56:41 AM
I would just use:
cell[i].innerText
. I think item is a method so it would be:
cell.item(i)

If I rememeber correctly innerText is IE only so innerHTML maybe better.
But do you mean you can't get the text or that the row doesn't get selected? I'm still not sure what the problem is.

--

Joe (Microsoft MVP - XML)
Reply By: nkrust Reply Date: 1/11/2007 4:15:09 AM
After i added the line coded in red, the rows just stopped getting selected.
If instead of cell.item[i] i use cell.item("0") or ("1") the 1st or the 2nd row value is copied to the var text.
So i suppose there's some mistake in that line, or may b its just not possible to do it... I hope i was able to make it clear to u ..

------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
Reply By: joefawcett Reply Date: 1/11/2007 4:44:27 AM
But you are storing the oldClassName outside of the onclick function so that might be the reason. As you didn't show setDataType, which seems oddly named, I can't tell.
Do you just want to change the className  when the td is clicked? If so it might be easier just to use the onclick of the table itself and then analyse the event object to see which td was clicked rather than having an onclick for all cells.

--

Joe (Microsoft MVP - XML)

Go to topic 54697

Return to index page 67
Return to index page 66
Return to index page 65
Return to index page 64
Return to index page 63
Return to index page 62
Return to index page 61
Return to index page 60
Return to index page 59
Return to index page 58