469,607 Members | 2,048 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

Receive an html code to paste it in a table with Ajax


Hi

I'm new to Ajax but I've used javascript from time to time.

Now, I've a java application that will generate a page with data in a
table. I want to make a link in each row so when user click it, connect
to my server and get some more data to fill a hidden row below the
clicked one.

I guess it could be simple and could imagine that somebody could have
some code in that way and, may be, would like to share it... or just
give me a clue about how to do it.

I've been googling for this but every thing I found is about xml data
but I think it could be possible to retrieve just xhtml portions of
code with ajax/javascript...

Any help appreciated...

Thanks in advance

C

Jan 18 '07 #1
5 1413
ASM
Cacho a écrit :
Hi

I'm new to Ajax but I've used javascript from time to time.

Now, I've a java application that will generate a page with data in a
table. I want to make a link in each row so when user click it, connect
to my server and get some more data to fill a hidden row below the
clicked one.

I guess it could be simple
Not really ...
and could imagine that somebody could have
some code in that way and, may be, would like to share it... or just
give me a clue about how to do it.
To know on witch row (index of row) you are when you click.
To call via XMLHttpRequest the file to display.
To know in which cell of next row (index+1) you want to inner what was
received.
I've been googling for this but every thing I found is about xml data
but I think it could be possible to retrieve just xhtml portions of
code with ajax/javascript...
You mean you want to get a large file via XMLHttpRequest, then to insert
in next row only a little part of what was received ?

<script type="text/css">

var rangs, cible, http_request=false;

onload = function() {

// rangs = collection of rows -- cible = main target
var rangs = document.getElementById('myTable');
var cible = rangs.getElementsByTagName('TBODY')[0];
rangs = cible.getElementsByTagName('TR');

// give event onclick to rows not hidden
for(var i=0, i<rangs.length; i++) {
if(rangs[i].className=!='hide') {
rangs[i].onclick = function() {
insertDatas(this);
};
}
}
}

function rowIndex(what) {
for(var i=0, i<rangs.length; i++) {
if(rangs[i]==what) return i;
}
}

function insertDatas(what) {
var idx = rowIndex(what);
var targ = rangs[idx];
targ = targ.getElementsByTagName('TD')[0];
var url = 'myDatas/'+(idx/2)+'.txt';
XHR();
http_request.onreadystatechange = function(){ inserContents(targ);};
http_request.open('GET', url, true);
http_request.send(null);
}

function inserContents(where) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
where.innerHTML = http_request.responseText;
}
else {
alert('Problem with request.');
}
}
}

function XHR() {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!http_request) {
alert('Abandon : Impossible de créer une instance XMLHTTP');
return false;
}
return http_request;
}

</script>

<style type="text/css">
..hide { display: none;
</style>

<table id="myTable">
<tbody>
<tr><td>test 1</td></tr>
<tr class="hide'><td></td></tr>
<tr><td>test 2</td></tr>
<tr class="hide'><td></td></tr>
</tbody>
</table>

datas :
- in folder : myDatas
- files : 0.txt, 1.txt, 2.txt ...

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jan 19 '07 #2
ASM
ASM a écrit :
Cacho a écrit :
>>
I guess it could be simple

Not really ...
some errors in code given ...

code working :
http://stephane.moriaux.perso.wanado...c/insertDatas/

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jan 19 '07 #3

On 19 ene, 03:12, ASM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
ASM a écrit :
Cacho a écrit :
I guess it could be simple
Not really ...some errors in code given ...

code working :http://stephane.moriaux.perso.wanado...c/insertDatas/

Hi

Thanks for your reply.

I dont want to retrieve a file. I need to just call a servlet and paste
the xhtml code returned by it.

The process could be similar to the one you sent, right ?

Thank you

C

Jan 19 '07 #4
ASM
Cacho a écrit :
On 19 ene, 03:12, ASM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
>ASM a écrit :
code working :http://stephane.moriaux.perso.wanado...c/insertDatas/
I dont want to retrieve a file. I need to just call a servlet and paste
the xhtml code returned by it.

The process could be similar to the one you sent, right ?
servlet means something server side no?
so, I think you'i need XMLHttpRequest.

And, yes I hope it could be similar as simple given example.

Tell back if it works.

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jan 19 '07 #5
Cacho wrote:
>
I dont want to retrieve a file. I need to just call a servlet and paste
the xhtml code returned by it.
The world's worst (and most numerous) browser doesn't supported
inserting XML fetched via XMLHTTPrequest into the current DOM. You will
be better off using text (probably JSON) and building the additional DOM
elements from that.

Unless your users are known to be using a decent modern browser.

--
Ian Collins.
Jan 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Steve Franks | last post: by
1 post views Thread by rockdale | last post: by
4 posts views Thread by finecur | last post: by
6 posts views Thread by patrice.fiset | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.