2013-07-25 16 views
5

Sto cercando di creare una funzione che provvederà punti in una tabella come come mostrato nell'immagine:Disporre array elementi con javascript e jquery

enter image description here

codice ho bisogno di modificare è:

var rowsCount = 7; 
var heightTable = 700; 
var rowHeight = 100; 

//an array with divs with elements, every element has a top and left position 
var arrayOfDivs = [({topPosition : 99, leftPosition: 100}),({topPosition : 150, leftPosition: 400}),({topPosition : 578, leftPosition: 10})]; 

//so here create a function that will create arrays = rowCount, so here we will need 7 arrays and arrange elements from arrayOfDivs into new arrays 


function arrangeInNewArrays (rowsCont,heightTable,rowHeight) { 
    var j=0; 
    for i=0 { 
     if (arrayOfDivs.topPosition[0] > rowHeight*j < rowHeight){ 
       var array+j = array+j+arrayOfDivs[i]; 
     }else { 
       i++,j++ 
     } 
    } 

    print arrayofDivs[i]; 
} 

Qualche suggerimento? Come posso organizzare punti per riga ...

Ti prego, aiutami a farlo ///

CODICE: http://jsfiddle.net/sYq9S/9/

+0

organizzare i punti da una tabella per righe nelle matrici –

+0

http://jsfiddle.net/sYq9S/9/ –

+0

qualcuno ha messo una taglia su questa domanda prima di rispondere: D questa non verrà data risposta presto, ho lavorato con qualcosa di molto simile un paio di mesi fa – pythonian29033

risposta

0

penso che si può usare qualcosa di simile:

var ROW_HEIGHT = 100; //assume 100px, but you can change this 
var rowsCount = 7; 

var rows = []; 
for(var i = 0; i < rowsCount; i++) { 
    rows[i] = []; 
} 

for(var i = 0; i < arrayOfDivs.length; i++) { 
    var position = arrayOfDivs[i]; 

    //The assumption is that the table starts at position 0. If not, you will 
    //have to subtract the table's top position from topPosition 
    var rowNumber = Math.floor(position.topPosition/ROW_HEIGHT); 
    rows[rowNumber].push(position); 
} 

Questo finirà per raggruppare div in file, in base al loro topPosition. Quindi tutto ciò che con 0 <= topPosition < 100 sarà in row[0] (prima riga), qualsiasi cosa con 100 <= topPosition < 200 sarà in row[1] (seconda riga) e così via.

È quindi possibile scorrere su questi e posizionarli in base allo leftPosition.

Questo non ti darà necessariamente le righe 7 perché dipende dallo topPosition di ogni elemento. Quindi se non hai nulla che abbia un topPosition tale che 600 <= topPosition < 600 allora non ci sarà nulla nella settima fila.

Se si desidera ordinare leftPosition all'interno di ciascuna delle righe, si può fare questo:

for(var i = 0; i < rowsCount; i++) { 
    rows[i].sort(function(a, b) { 
     return a.leftPosition - b.leftPosition; 
    }); 
} 

Here is the fiddle.

+0

si prega di mostrare in un jsfiddle, non vedo quale funzione restituire qui. GRAZIE –

+0

@MarkWest La funzione restituisce 'rows'. –

+0

quindi questa funzione creerà 7 nuovi array con gli elementi interni con la riga superiore e sinistra [1] ({topPosition: top, leftPosition: left} {etc ...}, ... ... ... Come organizzare dopo di che ogni nuova riga di matrice creata [i] per posizione sinistra all'interno dell'array? –

Problemi correlati