Textfield Numeric Sorting?

J

Jared Cobb

Hi all

I'm looking for a way to sort a recordset based on a numerical value. I'm
better with examples rather than explaining, so please see the following:

http://www.jaredcobb.com/example

Hopefully that example explains it all. I've searched for tutorials on
something like this but I think it's too specialized to find anything. Any
help or a point in the right direction would be GREATLY appreciated.
Thanks!

j
 
G

George M Jempty

Jared said:
Hi all

I'm looking for a way to sort a recordset based on a numerical value. I'm
better with examples rather than explaining, so please see the following:

http://www.jaredcobb.com/example

Hopefully that example explains it all. I've searched for tutorials on
something like this but I think it's too specialized to find anything. Any
help or a point in the right direction would be GREATLY appreciated.

After looking at your page, these are my first few tips:

Change the name of the input box named "textfield" to "textfield1".
this will make it easier to parse the name of the element for it's
position in the form, if that's the approach you want to take. You
might be able to determine position using the form.elements array,
unless you put other fields in your form. In any event, "textfield1" is
more consistent giving your current naming scheme.

Speaking of a form, make sure you are indeed using a bona fide form,
with opening and closing <form> tags. this will allow you to have an
onblur event handler for each text input box that can be called like this:

onblur="reorder(this)"

"this" refers to the text element, but will also allow you to refer to
the form as "this.form", which will allow you to get at all the other
elements.

onfocus of any field I would create an array based on the existing
content of the form. onblur the first thing I would do is validate the
user's input. Having created my array onfocus it will be easy to
replace invalid input.

Hope this helps, even though its only a bunch of housekeeping BEFORE you
actually get to sorting your data.

/G
 
L

Lasse Reichstein Nielsen

Hi all

I'm looking for a way to sort a recordset based on a numerical value. I'm
better with examples rather than explaining, so please see the following:

http://www.jaredcobb.com/example

Hopefully that example explains it all. I've searched for tutorials on
something like this but I think it's too specialized to find anything. Any
help or a point in the right direction would be GREATLY appreciated.
Thanks!

I'll assume there is a form around the input elements.

Whenever you change one number, from a to b, all other numbers between
a and b must change too.

I suggest putting these two handlers on the input elements onfocus and
onchange handlers.
---
var current;

function focus(){
current = +this.value;
}
function change() {
var elems = this.form.elements;
var to = +this.value;
var min = Math.min(current,to);
var max = Math.max(current,to);
var change;
if (current < to) {
change = -1;
} else if (current > to) {
change = 1;
} else {
return; // no change
}
for (var i=0;i<elems.length;i++) {
var elemVal = +elems.value;
if (elems != this && elemVal >= min && elemVal <= max) {
elems.value = elemVal + change;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top