473,545 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Broad Question

I'm looking for possible ways of doing this with javascript so this
question is pretty broad but I have a string like this coming from a
database.

strDivOrder = "Div3,Div5,Div1 ,Div2"

The string could contain values from Div1 to Div16 in any order. I
want to arrange 16 div sections on a page based on which values are in
strDivOrder. In this example, Div3 would be displayed first on the
page, Div5 would be second etc.

For example,

<div id="Div1">
'This should be position 3 on the page
</div>

<div id="Div2">
'This should be position 4 on the page
</div>

<div id="Div3">
'This should be position 1 on the page
</div>

<div id="Div4">
'This should not be displayed at all
</div>

<div id="Div5">
'This should be position 2 on the page
</div>

<div id="Div6">
'This should not be displayed at all
</div>

<div id="Div7">
'This should not be displayed at all
</div>

Feb 7 '06 #1
6 1242
<ev*******@gmai l.com> schreef
I'm looking for possible ways of doing this with javascript so this
question is pretty broad but I have a string like this coming from a
database.

strDivOrder = "Div3,Div5,Div1 ,Div2"

The string could contain values from Div1 to Div16 in any order. I
want to arrange 16 div sections on a page based on which values are in
strDivOrder. In this example, Div3 would be displayed first on the
page, Div5 would be second etc.


First choice to make is, should the actual DOMnodes be re arranged in the
order dictated by the string, or is it enough to show them as such on the
screen? If the latter, it should be sufficient to make them all 'position'
'absolute', and then set each 'top' to a multiple of the element's id index
in the string, plus any initial offset. Any div not occuring in the sring is
set to 'display' 'none' et presto.

//let's assume all div's start with 'display' 'none'
var a = strDivOrder.spl it( ',' ), i = a.length, o, s;
while(i--) {
if( (o=document.get ElementById( a[i] )) ) {
s = o.style;
s.display = 'block';
s.postion = 'absolute';
s.top = i * 30 + 'px'; // assume they are 28 high or so
}
}

untested.
hth
ivo
http://www.ariel.shakespearians.com/
Feb 7 '06 #2
Very nice, thank. Bit I would actually want the div areas displayed on
the page in the same order as in the strDivOrder string

Feb 7 '06 #3
Very nice, thanks. But I actually need the div nodes displayed in the
same order as specified in strDivOrder.

Feb 7 '06 #4


Well, just use 2 array sets, one for the served string off the db,
and one for the respective style values/schemes:

strDivOrder = "Div3,Div5,Div1 ,Div2"
myArr=strDivOrd er.split(/div\d+/i); // will return an array off the
string whose delimiter is such regexp
from there, give all positioning schemes you need, and do like
oStyles=new Array(); // and set your schemes for all myArr
members accordingly
for (i=0;i<myArr.le ngth;i++) {

document.getEle mentById(myArr[i]).style.YOURPRO PERTY=oStyles[i][0];

document.getEle mentById(myArr[i]).style.YOURPRO PERTY=oStyles[i][1];
...
}
Danny
Feb 7 '06 #5
ev*******@gmail .com wrote:
Very nice, thanks.
What is?

But I actually need the div nodes displayed in the
same order as specified in strDivOrder.


Always handy to have a go yourself first.

The various methods for inserting nodes like appendChild or insertBefore
if given an argument of an existing node will move it (that is, remove
it from the document tree and insert it where you specify).

<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-952280727>
Trivial example:

<script type="text/javascript">
function doOrder(id, idList)
{
var idArray = idList.split(', ');
var box = document.getEle mentById(id);
for (var i=0, len=idArray.len gth; i<len; ++i){
box.appendChild (document.getEl ementById(idArr ay[i]))
}
}
</script>

<input type="button" value="Order 4 2 3 1"
onclick="doOrde r('container', 'div-04,div-02,div-03,div-01');">
<input type="button" value="Order 1 4 2 3"
onclick="doOrde r('container', 'div-01,div-04,div-02,div-03');">
<input type="button" value="Order 1 2 3 4"
onclick="doOrde r('container', 'div-01,div-02,div-03,div-04');">
<div id="container" >
<div id="div-01">div 01</div>
<div id="div-02">div 02</div>
<div id="div-03">div 03</div>
<div id="div-04">div 04</div>
</div>


--
Rob
Feb 7 '06 #6
JRS: In article <11************ ********@g47g20 00cwa.googlegro ups.com>,
dated Tue, 7 Feb 2006 11:29:31 remote, seen in
news:comp.lang. javascript, ev*******@gmail .com <ev*******@gmai l.com>
posted :
I'm looking for possible ways of doing this with javascript so this
question is pretty broad but I have a string like this coming from a
database.

strDivOrder = "Div3,Div5,Div1 ,Div2"

The string could contain values from Div1 to Div16 in any order. I
want to arrange 16 div sections on a page based on which values are in
strDivOrder. In this example, Div3 would be displayed first on the
page, Div5 would be second etc.


Unless it is classwork, I don't see any real need to do that.

Create divisions D01 to D16 in that order.

Create an object of the form {Div1:"D3", Div2:"D4", ...}; maintain this
object to represent the intended arrangement.

When accessing Div2, for example, use lookup in the Object to determine
that D4 must be used.

Details may need adjustment.
Alternatively, 1 + strDivOrder.ind exOf("Div1")/5 will give the
number of the div to be used as Div1, if all the names are the same
length.

In general, when creating names containing digits representing numbers,
it is often wise to pad all numbers to a given length with leading
zeroes.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 8 '06 #7

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

Similar topics

1
1225
by: Donald Braman | last post by:
I am wondering what the most efficient way to handle a rather small (about 1000 records) set of data would be. In this data set, each record tracks an agent over time. Agents move around on a grid and interact when they end up on top of or next to one another. Each agent has a number of attributes that determine what these interactions are...
11
1742
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I understand that you declare your intention to use a namespace within a page through the "Inherits" attribute. I know that using "Inherits" isn't...
3
1083
by: eric.burgin | last post by:
I have not heavily used interfaces in the past so I am looking for advice. An interface may not be the best solution but it was the first idea I had and was working at first. Here is the situation, I have been asked to create an application that will provide a "script" of things to say, yes/no questions to ask, information to look up etc....
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1028
muto222
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.