Javascript / AJAX / "D"HTML Help needed

Status
Not open for further replies.
All --

I'm in the process of converting a Java Swing-based application into a web-based application.

The Java app is relatively simple. A JTable grid which shows rows and columns of the catalog data, a Filter pane which allows searching by values of various fields in the catalog data and, finally, a Detail pane which allows you to see all the details about the currently selected grid item.

I had implemented an 'instant' search where the grid updated on every keypress (instead of entering values and hitting an apply button.) The customer loves it and wants it in the web version.

In the web version, for combo-box/drop-down lists and checkboxes, I implemented an onchange event handler to simply/naively submit the whole form and update the whole page. However, this same trick doesn't work (well) for onkey* events as the cursor position gets reset on the submit.

So, I guess I'm going to need some AJAX-y stuff.

My problem is that I never quite 'got' dynamic HTML and I really don't know where to start.

Can I have the backend simply send a DIV with the grid content and have the callback function replace the current DIV with the replacement DIV? Do I need to delve into JSON to make this work?

Thanks for any help!

Brad
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Can I have the backend simply send a DIV with the grid content and have the callback function replace the current DIV with the replacement DIV?
Yes, but doing so is fragile and a pain in the ass. If you're already writing a dynamic system it's usually going to be more straightforward to handle rendering client-side.

Do I need to delve into JSON to make this work?
If you want to not hate dealing with it later you probably will. JSON is the most common for most applications, XML isn't far behind. You can look at Dust templates or something for an example of clean separation of concerns.
 
Blacken00100":1wdofefu said:
Can I have the backend simply send a DIV with the grid content and have the callback function replace the current DIV with the replacement DIV?
Yes, but doing so is fragile and a pain in the ass. If you're already writing a dynamic system it's usually going to be more straightforward to handle rendering client-side.

Could you elaborate as to 'pain in the ass'? As I'm relatively noob to AJAX and client-side rendering, I don't know what the pitfalls of doing it as I suggested would be. How is it fragile?

Do I need to delve into JSON to make this work?
If you want to not hate dealing with it later you probably will. JSON is the most common for most applications, XML isn't far behind. You can look at Dust templates or something for an example of clean separation of concerns.

Hrm. Well, I have access to jQuery ... and maybe some other Javscript libraries; I don't think dust is one of them. I'm working in a constrained environment and anything I use must be already available on the system or on an approved list. I'll check to see what I have.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Refactoring becomes difficult because you're spreading around your display source and how it gets injected into your page. You are forced to keep more mental state because of a lack of consistency regarding where your display logic is living. Doing it server-side means there's no less than two locations for any piece of display logic. Meanwhile, a basic REST-ish backend lets you pull down data from the server and do whatever you want client-side.

Dust is an example of how to do it, not the be-all end-all. It's not a hard thing to build, though reinventing the wheel is for suckers.
 
Feanor.Yarr":3rf2xaso said:
What you are trying to do is fairly simple. You just have to create an AJAX call (see here for instruction: http://www.w3schools.com/ajax/ajax_xmlh ... create.asp) and then use document.getElementById(divname).innerHTML=AJAXresponse to change the contents of a div. innerHTML is much easier to use than going with DOM calls and usually faster.

I, of course, did this already, and it does work (and the customer is quite stoked about the result) But, like Blacken said almost immediately after my OP, it is a bit cumbersome and hard to manage ... also, the amount of data over the wire is a lot bigger than it would need to be.

I'm looking really hard at rendering the grid via a JSON-emitting web request and a client-side Javascript 'grid builder.'
 
Status
Not open for further replies.