473,473 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sort items like Netflix - dynamically change text boxes or select fields

Hello, I would like to be able to have the user sort a list of items
similarly to the way you sort your queue on Netflix.com. (the numbers
dont change dynamically on netflix, they must be doing something
serverside with them)

Like this: ([] represents a text or select filed)
[1] an item
[2] something else
[3] a differnt thing
[4] the other
[5] yougetthepicture

So, the user would change the 5 to a 1 if he wanted that item to be #1.
I thought that if i had script that would move everything down a
number each time the user changed one of them, that would work. The
items can stay where they are, i dont need them to change dynamically.
As long as when they are done, each item has a new number, and i can
submit the textboxes as an array to PHP.

I am populating the text boxes and the list of items with php, so the
solution would have to be able to work on any number of items.

I could deal with a serverside PHP solution if anyone knows something
that would work that way.

thanks for your time,
juglesh

Jul 23 '05 #1
7 2901
juglesh wrote:
So, the user would change the 5 to a 1 if he wanted that item to be
#1. I thought that if i had script that would move everything down a
number each time the user changed one of them, that would work. The
items can stay where they are, i dont need them to change dynamically.


Quickhack, barely tested:

function renumber(oInput) {
var oForm = oInput.form,
oElements = oForm.elements,
nElements = oElements.length,
nOrder = parseInt(oInput.value, 10) || 0,
aElements = [],
i, n, oCurElem;
oInput.value = nOrder;
for (i=0; i<nElements; i++) {
oCurElem = oElements[i];
if (oCurElem.type == "text") {
n = parseInt(oCurElem.value, 10) || 0;
if (n >= nOrder && oCurElem != oInput) {
n++;
}
aElements[aElements.length] = { element: oCurElem, order: n };
}
}
aElements.sort(function(a, b) {
return a.order - b.order > 0? 1 : b.order - a.order == 0? 0 : -1;
});
for (i=0; i<aElements.length; i++) {
aElements[i].element.value = i + 1;
}
}
[...]
<form><p>
<input type="text" name="item0" value="1"
onchange="renumber(this)"> an item<br>
<input type="text" name="item1" value="2"
onchange="renumber(this)"> something else<br>
<input type="text" name="item2" value="3"
onchange="renumber(this)"> a differnt thing<br>
<input type="text" name="item3" value="4"
onchange="renumber(this)"> the other<br>
<input type="text" name="item4" value="5"
onchange="renumber(this)"> yougetthepicture<br>
</p></form>

ciao, dhgm
Jul 23 '05 #2
sweet! works if you move an item down the list, but not up. I may be
able to use it as is if it cant be fixed. Would you(or anyone) mind
commenting the code?

Is there a js manual that is as good as the php.net manual?

Thank you very much for your time,
juglesh

Jul 23 '05 #3
juglesh wrote:
works if you move an item down the list, but not up.


As I told, barely tested ... Here's another try, and again,
testing will be yours :-) Maybe it can be done easier, maybe
even much easier.

function renumber(oInput) {
var oFormElems, nFormElems, bUp, aElements, i, nCurOrder, oCurElem,
nNewOrder = parseInt(oInput.value, 10) || 0;
nOldOrder = parseInt(oInput.oldvalue, 10) || 0;
if ((bUp = nNewOrder - nOldOrder)) {
oFormElems = oInput.form.elements;
nFormElems = oFormElems.length,
aElements = [];
for (i=0; i<nFormElems; i++) {
oCurElem = oFormElems[i];
if (oCurElem.type == "text") {
nCurOrder = parseInt(oCurElem.value, 10) || 0;
if (oCurElem != oInput) {
if (bUp < 0 && nCurOrder >= nNewOrder) {
nCurOrder++;
}
else if (bUp > 0 && nCurOrder <= nNewOrder) {
nCurOrder--;
}
}
aElements[aElements.length] = { element: oCurElem, order: nCurOrder };
}
}
aElements.sort(function(a, b) {
return a.order - b.order > 0? 1 : b.order - a.order == 0? 0 : -1;
});
for (i=0; i<aElements.length; i++) {
aElements[i].element.value = i + 1;
}
}
}
[...]
<form><p>
<input type="text" name="item0" value="1"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> an item<br>
<input type="text" name="item1" value="2"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> something else<br>
<input type="text" name="item2" value="3"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> a differnt thing<br>
<input type="text" name="item3" value="4"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> the other<br>
<input type="text" name="item4" value="5"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> yougetthepicture<br>
</p></form>

ciao, dhgm
Jul 23 '05 #4
juglesh wrote:
works if you move an item down the list, but not up.


As I told, barely tested ... Here's another try, and again,
testing will be yours :-) Maybe it can be done easier, maybe
even much easier.

function renumber(oInput) {
var oFormElems, nFormElems, bUp, aElements, i, nCurOrder, oCurElem,
nNewOrder = parseInt(oInput.value, 10) || 0,
nOldOrder = parseInt(oInput.oldvalue, 10) || 0;
if ((bUp = nNewOrder - nOldOrder)) {
oFormElems = oInput.form.elements;
nFormElems = oFormElems.length,
aElements = [];
for (i=0; i<nFormElems; i++) {
oCurElem = oFormElems[i];
if (oCurElem.type == "text") {
nCurOrder = parseInt(oCurElem.value, 10) || 0;
if (oCurElem != oInput) {
if (bUp < 0 && nCurOrder >= nNewOrder) {
nCurOrder++;
}
else if (bUp > 0 && nCurOrder <= nNewOrder) {
nCurOrder--;
}
}
aElements[aElements.length] = { element: oCurElem, order: nCurOrder };
}
}
aElements.sort(function(a, b) {
return a.order - b.order > 0? 1 : b.order - a.order == 0? 0 : -1;
});
for (i=0; i<aElements.length; i++) {
aElements[i].element.value = i + 1;
}
}
}
[...]
<form><p>
<input type="text" name="item0" value="1"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> an item<br>
<input type="text" name="item1" value="2"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> something else<br>
<input type="text" name="item2" value="3"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> a differnt thing<br>
<input type="text" name="item3" value="4"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> the other<br>
<input type="text" name="item4" value="5"
onfocus="this.oldvalue=this.value"
onchange="renumber(this)"> yougetthepicture<br>
</p></form>

ciao, dhgm
Jul 23 '05 #5
thank you very much, works great!

Jul 23 '05 #6
works great, thanks again! I'm pretty amazed youre able to just write
that stuff off the top of your head with no testing or anything.

just to let anyone know, i'm using name="order[]" instead of
name="item1"etc. cuz i needed that for the code that takes this form.
works fine though.

Jul 23 '05 #7
juglesh wrote:
Is there a js manual that is as good as the php.net manual?


Depends on what you mean by "good". Generally: No, since most of the
examples will be using host objects which are not part of the core
language but of the used AOMs/DOMs. See the FAQ.
PointedEars
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: juglesh | last post by:
hi, all, thanks for reading. i have a form in which i want drop down boxes to dynamically change some hidden fields: http://cynthialoganjewelry.com/test4.htm <form name=test method="post" > ...
4
by: Chris Sharman | last post by:
I've got 3 'alternative' boxes, only one of which I want displayed, according to the value of an earlier select (so I'm using the <htmlelement>.style property from javascript). One is a div...
4
by: JohnSouth104 | last post by:
Hi I want to warn the user that changing the selected item in a pull-down will clear some data he has already entered in a file download box. I've tried using confirm() to return true or false...
15
by: Paul T. Rong | last post by:
Hi everybody, This time a very very difficult case: I have a table "tblStudent", there are 50 students, I also made a form "frmStudent" based on "tblStudent", now if I don't want to show all...
3
by: rquinnan | last post by:
Good Evening all, I would greatly appreciate any assistance on this Access 2003 quandary I'm in. And I do apologize if this has been answered somewhere else, I didn't see one that addressed my...
3
by: Frustrated Developer via DotNetMonster.com | last post by:
I have posted a couple times on here already and found the user community to be very helpful. I took on a project before I realized how difficult a time I'm having working with a database....
1
by: Daniel Gormley | last post by:
What I have is a form that is dynamically generated based on which database table its calling. Therefore, the number of category.name.count can be different. So I have this form generated and...
2
by: Sethos | last post by:
I am sure that this has been covered, hashed, and rehashed, but a search on the group did not produce the answer, so forgive me if this seems like a "newbie" type question... Besically, I have a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.