views:

386

answers:

3

Hi,

I'm generating a set of rows of html input text fields, which I want my endusers to be able to reorder. I dont want to use AJAX, but am okay with javascript.

I'm okay with some arrow buttons placed on the side of each row, which when pressed move the fields up and down.

My issue with AJAX is that its too heavy (50-60kb) for just this functionality. I had a look at some Yahoo code (YUI), but again, it seems overkill for one particular functionality. I found this - http://www.danvk.org/wp/dragtable/ - which is seems like the lightest code, but only allows column reordering.

I'll be working on the last option, but I'm open to other thoughts / approaches on how to do this (ie, to allow the user to reorder the rowwise fields).

Thanks, Steve!

PS: Not important to this discussion I suppose, but I'm using PHP to generate these html text fields.

A: 

You seem to be confused; AJAX is a general term referring to using Javascript to load extra content from a server via sub-requests without actually loading a new page. There are many different Javascript libraries that can handle AJAX requests.

Perhaps you're thinking of one of the more popular libraries like JQuery when you say "AJAX"?

As for your question - one thing to remember is that with proper server configuration, client browsers will general cache Javascript libraries after the first request; thus the consistent overall load time isn't as impacted by the size of such libraries - only that first page load. You can avoid even that if you use the Google-hosted copy of JQuery, which is likely to already be cached by the large majority of visitors to your site.

Amber
+1  A: 

You can write a javascript function similar to this one

function up(row) {
    var prevRow = row.previousElementSibling;
    if(prevRow != null) {
     row.parentNode.removeChild(row);
     document.body.insertBefore(row, prevRow);
    }
};

and use it in your rows like this:

<p>
    <input type="text"/>
    <a onclick="up(this.parentNode)">Up</a>
</p>

I agree with you that you don't really need to reference a Javascript framework for a very simple task like this.

Josef
Thanks Josef - that looks very promising! I'm reading more on it and trying to figure out how to make it work for a "row" of text input fields. But thanks again! :)
Steve
This doesn't appear to work in IE though...
Steve
+1  A: 

Adding to the Josef's answer...

According to W3C DOM Level 2 Core specification:

insertBefore

Inserts the node newChild before the existing child node refChild. [...] If the newChild is already in the tree, it is first removed.

Thus, there is no need to call removeChild() before calling insertChild().

Also, IE6 DOM support is very bad, so you might need to write specific code for it. Or maybe you would prefer to not waste time supporting this browser, and ask users to upgrade. Or, if you really need to support IE6, maybe using a JavaScript library (like jQuery) could be an easy solution.

Edit: This is the final JavaScript solution, based on Josef's answer:

function up(row) {
    var prevRow = row.previousSibling;
    if(prevRow) {
        row.parentNode.insertBefore(row, prevRow);
    }
}
Denilson Sá
Hi there Denilson, thanks for your inputs. It doesnt look like IE7 supports it properly either. I'm trying the following code, and it works in FF, but not in IE7 (the input fields dont move).function up(row) { var prevRow = row.previousElementSibling; if(prevRow != null) { // row.parentNode.removeChild(row); row.parentNode.insertBefore(row, prevRow); }};
Steve
Hey, I've just noticed the issue! `row.previousElementSibling` is wrong, the correct should be `row.previousSibling`. I say this based on W3C specification (look at the link from my answer) and also on MDC (Mozilla Developer Center https://developer.mozilla.org/en/DOM/Node ).
Denilson Sá
Marvelous! That solved all the problems I was having! Outstanding catch Denilson - thank you so much! This was eating my head for days!! Thanks again!!!!!
Steve