Я использую django шаблоны и имею JSON данные для рендеринга на нем. Но проблема в том, что первая строка пуста, когда более 1 объекта создано с использованием итерации. и последний объект имеет данные (td's) одной дополнительной строки.
Код здесь -
Передан JSON объект похож на этот:
{"Academics": {"IB": {"IB_1st": {
"id": 12, "group": 2, "group__group_group": "Academics-CBSE/IB-upto5th", "group__name": "German",
"category__type": "Academics", "board__name": "IB", "tuition_class__name": "1st", "stream__name": null,
"subject__name": "German", "streams": [null], "subjects": {"12": "German"}
},
"IB_Pre-Primary": {
"id": 2339, "group": 2, "group__group_group": "Academics-CBSE/IB-upto5th", "group__name": "German",
"category__type": "Academics", "board__name": "IB", "tuition_class__name": "Pre-Primary",
"stream__name": null, "subject__name": "German", "streams": [null], "subjects": {"2339": "German"}
}
},
"CBSE": {"CBSE_2nd": {
"id": 13, "group": 2, "group__group_group": "Academics-CBSE/IB-upto5th", "group__name": "German",
"category__type": "Academics", "board__name": "CBSE", "tuition_class__name": "2nd", "stream__name": null,
"subject__name": "German", "streams": [null], "subjects": {"13": "German"}},
"CBSE_3rd": {
"id": 15, "group": 2, "group__group_group": "Academics-CBSE/IB-upto5th", "group__name": "German",
"category__type": "Academics", "board__name": "CBSE", "tuition_class__name": "3rd", "stream__name": null,
"subject__name": "German", "streams": [null], "subjects": {"15": "German"}
}
}
}}
Создание элемента используя JQUERY в django шаблонах:
function myFun(group_list_record) {
var group_dict_record = group_list_record[0];
var table = $('<table>');
var content = '<thead><tr><th>CATEGORY</th> <th>BOARD</th> <th>TUITION CLASS </th> <th>STREAM</th> <th>SUBJECT</th></tr></thead>';
table_body = $('<tbody>');
$(table).append(content);
for (var category_key in group_dict_record) {
var category = category_key;
var category_dict = group_dict_record[category_key];
if (group_dict_record.hasOwnProperty(category_key)) {
console.log(category_key + " -> " + group_dict_record[category_key]);
for (var board_key in category_dict) {
console.log(" "+board_key + " -> " + category_dict[board_key]);
var board = board_key;
var board_dict = category_dict[board_key];
for (var board_tuition_key in board_dict) {
console.log(" "+board_tuition_key + " -> " + board_dict[board_tuition_key]);
var table_row = $('<tr>').addClass(board_tuition_key); // .attr('id', board_tuition_key);
var board_tuition = board_tuition_key;
var board_tuition_dict = board_dict[board_tuition_key];
for (var records_key in board_tuition_dict) {
console.log(records_key);
if (records_key == 'category__type')
{
var category_column = $('<td>');
category_column.text(board_tuition_dict.category__type);
}
if (records_key == 'board__name')
{
var board_column = $('<td>');
board_column.text(board_tuition_dict.board__name);
}
if (records_key == 'tuition_class__name')
{
var tuition_class_column = $('<td>');
tuition_class_column.text(board_tuition_dict.tuition_class__name);
}
if (records_key == 'streams')
{
var stream_column = $('<td>');
stream_column.text(board_tuition_dict.streams);
}
if (records_key == 'subjects')
{
var subjects_column = $('<td>');
for (sub in board_tuition_dict.subjects)
{
var subject_name = board_tuition_dict.subjects[sub];
var delete_tag = $('<div>').addClass('delete-group-sepc').text(subject_name);
console.log(" Subjects:"+sub + " -> " + board_tuition_dict.subjects[sub]);
subjects_column.append(delete_tag);
delete_tag.attr('data-id', sub);
}
}
table_row.append(category_column);
table_row.append(board_column);
table_row.append(tuition_class_column);
table_row.append(stream_column);
table_row.append(subjects_column);
}
table_body.append(table_row).attr('id', board_tuition_key);
}
}
}
}
table.append(table_body);
$('#ajax_response').append(table);
// $('#ajax_response').children().replaceWith(table);
}
, предоставляя изображение экрана вывода: проверьте это здесь
обратите внимание на пустое изображение.