Bonjour, Je viens vers vous car j'ai un petit soucis avec la création d'un objet Json.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 
function enregistrer() {
 
var json
var dom = document.getElementById('dropzone').children
 
//if (dom[0].className == 'full'){
 
var DOM = []
 
    if (dom[0].className == 'full'){
    objet = {
        id : dom[0].firstChild.id, 
        class : dom[0].firstChild.className ,
        place : dom[0].firstChild.getAttribute('place') ,
        name : dom[0].firstChild.getAttribute('name') ,
        value : dom[0].firstChild.getAttribute('value') ,
        draggable : dom[0].firstChild.getAttribute('draggable'), 
        ondragstart : dom[0].firstChild.getAttribute('ondragstart')    
    }
    DOM.push(objet)
    }
    if (dom[1].className == 'full'){
    objet = {
        id : dom[1].firstChild.id, 
        class : dom[1].firstChild.className ,
        place : dom[1].firstChild.getAttribute('place') ,
        name : dom[1].firstChild.getAttribute('name') ,
        value : dom[1].firstChild.getAttribute('value') ,
        draggable : dom[1].firstChild.getAttribute('draggable'), 
        ondragstart : dom[1].firstChild.getAttribute('ondragstart')
    }
    DOM.push(objet)
    }
 
    if (dom[2].className == 'full'){
    objet = {
        id : dom[2].firstChild.id, 
        class : dom[2].firstChild.className ,
        place : dom[2].firstChild.getAttribute('place') ,
        name : dom[2].firstChild.getAttribute('name') ,
        value : dom[2].firstChild.getAttribute('value') ,
        draggable : dom[2].firstChild.getAttribute('draggable'), 
        ondragstart : dom[2].firstChild.getAttribute('ondragstart')
    }
    DOM.push(objet)
    }
 
json = JSON.stringify(DOM)
 
 
$.post("./php/sauvegarde.php",
        {
                result : json
 
        })
    }
Cela fonctionne, le problème c'est que ça va jusqu’à dom[29] du coup c'est un peu lourd et je suis sur que je peux factoriser ça mais je ne sais pas comment. J'ai essayé un truc comme ça

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
for (var i = 0 ; i < 29 ; i++)
if (dom[i].className == 'full'){
    objet = {
        id : dom[i].firstChild.id, 
        class : dom[i].firstChild.className ,
        place : dom[i].firstChild.getAttribute('place') ,
        name : dom[i].firstChild.getAttribute('name') ,
        value : dom[i].firstChild.getAttribute('value') ,
        draggable : dom[i].firstChild.getAttribute('draggable'), 
        ondragstart : dom[i].firstChild.getAttribute('ondragstart')    
    }
    DOM.push(objet)
mais forcement ça ne marche pas du coup je ne vois pas trop