powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / HTML, JavaScript, VBScript, CSS [игнор отключен] [закрыт для гостей] / Sencha touch Вывести данные из вложеного JSON
2 сообщений из 2, страница 1 из 1
Sencha touch Вывести данные из вложеного JSON
    #38418090
UserQ
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Есть JSON, нужно вывести список question и внутри него список answer. На данный момент получается вывести список question с вложенными answer, но только только первый answer подпадает.

На данный момент получается вот так, только.



Мой JSON
Код: javascript
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.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
[
    {
        "id": 7,
        "answers": [
            {
                "id": 6,
                "answer": "1",
                "isCorrect": false
            },
            {
                "id": 7,
                "answer": "5",
                "isCorrect": false
            },
            {
                "id": 5,
                "answer": "3",
                "isCorrect": true
            }
        ],
        "question": "1+2=?"
    },
    {
        "id": 14,
        "answers": [
            {
                "id": 28,
                "answer": "5",
                "isCorrect": false
            },
            {
                "id": 31,
                "answer": "7",
                "isCorrect": true
            },
            {
                "id": 29,
                "answer": "2",
                "isCorrect": false
            },
            {
                "id": 30,
                "answer": "6",
                "isCorrect": false
            }
        ],
        "question": "2+5=?"
    },
    {
        "id": 9,
        "answers": [
            {
                "id": 13,
                "answer": "3",
                "isCorrect": false
            },
            {
                "id": 11,
                "answer": "5",
                "isCorrect": false
            },
            {
                "id": 14,
                "answer": "6",
                "isCorrect": true
            },
            {
                "id": 12,
                "answer": "7",
                "isCorrect": false
            }
        ],
        "question": "3+3=?"
    }
]



Views, Models, app
Код: javascript
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.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
Ext.define('Sencha.model.Question', {
    extend: 'Ext.data.Model',

    requires: ['Sencha.model.Answer'],

    config: {
        fields: [
            'question'
        ],

        proxy: {
            type: 'ajax',
            url: 'contacts.json',
            reader : {
                type : 'json'
            }
        },
         

        hasMany: {
            model: "Sencha.model.Answer",
            associationKey: 'answers'
        }
    }
});

Ext.define('Sencha.model.Answer', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
            'answer'
        ],

        belongsTo: "Sencha.model.Question"
    }
});

Ext.define('Sencha.view.Questions', {
    extend: 'Ext.List',
    xtype: 'questions',

    config: {
        title: 'Stores',
        cls: 'x-questions',

        store: 'Questions',
        itemTpl:[
            '{question}',
            '<div>',
                '<h2><b>Answers</b></h2>',
                '<tpl for="answers">',
                    '<div> - {answer}</div>',
                '</tpl>',
            '</div>'
        ].join('')
    }
});

Ext.define('Sencha.view.Answer', {
    extend: 'Ext.form.Panel',
    xtype: 'answer',

    config: {
        title: 'Answer',
        cls: 'x-questions',

        store: 'Questions',
        /*itemTpl: [
        '<div>{answer}</div>'
        ].join('')*/
        items:[
             xtype: 'radiofield',
             value: {answer}
        ]
    }
});

Ext.define("Sencha.view.Main", {
    extend: 'Ext.tab.Panel',
    requires: ['Ext.TitleBar',  'Sencha.view.Questions'],

    config: {
        tabBarPosition: 'bottom',

        items: [
            {
                title: 'Questions',
                iconCls: 'home',

                xtype: 'questions'
            }
        ]
    }
});

Ext.define('Sencha.store.Questions', {
    extend: 'Ext.data.Store',

    config: {
        model: 'Sencha.model.Question',
        autoLoad: true/*,
        //sorters: 'name',
        grouper: {
            groupFn: function(record) {
                return record.get('name')[0];
            }
        }*/
    }
});



//<debug>
Ext.Loader.setPath({
    'Ext': 'sdk/src'
});
//</debug>

Ext.application({
    name: 'Sencha',

    requires: [
        'Ext.MessageBox'
    ],

    views: ['Main'],
    stores: ['Questions'],
    models: ['Question', 'Answer'],

    launch: function() {
        // Destroy the #appLoadingIndicator element
        Ext.fly('appLoadingIndicator').destroy();

        // Initialize the main view
        Ext.Viewport.add(Ext.create('Sencha.view.Main'));
    }
});



Как заставить вывести все answer?
...
Рейтинг: 0 / 0
Sencha touch Вывести данные из вложеного JSON
    #38418268
UserQ
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Примерное решение

Код: javascript
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Ext.define('Sencha.model.Question', {
     extend: 'Ext.data.Model',  //
    requires: ['Sencha.model.Answer'],
      config: {
         fields: [
             'id', 'questionid', 'answers', 'question'
         ],
          proxy: { 
            type: 'ajax',
             url: 'contacts.json',
             reader : {
                 type : 'json'
             }
         } 
    }
 }); 



Код: javascript
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.
Ext.define('Sencha.view.Questions', {
    extend: 'Ext.List',
    xtype: 'questions',

    config: {
        title: 'Stores',
        cls: 'x-questions',

        store: 'Questions',
        itemTpl: new Ext.XTemplate(
            '<div class="question">{question}</div>',
			//'{[this.getAnswers(values.answers, values.id)]}',  // <==== Call of the method
			'{[this.getAnswers(values.answers, values.id, values)]}',
			{
				getAnswers: function (answers,id, values) {  //  <===== Method
					var returnString='';
					console.dir(values);
					Ext.each(answers, function(answer){
							 returnString += '<input type="radio" name="' + id + '" value="0">' + answer.answer + '<br>';
							 //console.log(returnString);
					 });
					return returnString;
				 }
			}
        )
    }
});
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / HTML, JavaScript, VBScript, CSS [игнор отключен] [закрыт для гостей] / Sencha touch Вывести данные из вложеного JSON
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]