![]() |
![]() ![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 93 Pomógł: 7 Dołączył: 6.09.2011 Skąd: Olsztyn Ostrzeżenie: (0%) ![]() ![]() |
Witam, otóż mam pewien problem, a mianowicie. Po wywołaniu w konsoli kodu:
Kod $('body').data(); dostaję coś takiego: Kod Object {averageData: Object} averageData: Object 8: Object average: 3.599 documents: Array[2] 0: Object 1: Object length: 2 __proto__: Array[0] selector: x.fn.x.init[1] __proto__: Object 9: Object average: 3.648 documents: Array[2] 0: Object 1: Object length: 2 __proto__: Array[0] selector: x.fn.x.init[1] __proto__: Object __proto__: Object __proto__: Object Problem polega na tym, że w momencie wywołania takiej linii: Kod $('body').data('average-data')[studentId]['documents'].splice(removeIndex, 0); podstawiając za studentId = 8, a pod removeIndex np 0, usuwany jest index=0 zarówno z tablicy o indexie 8 jak i o indexie 9... Czy ktoś z Was może mi powiedzieć co robię nie tak? Z góry dziękuję -------------------- Pomogłem? Kliknij "Pomógł".
|
|
|
![]()
Post
#2
|
|
Grupa: Zarejestrowani Postów: 717 Pomógł: 120 Dołączył: 18.04.2009 Ostrzeżenie: (0%) ![]() ![]() |
prawdopodbnie zle zainicjalizowales tablice (podaj kod w ktorym tworzysz tablice)
edit: aha, albo zla kolejnosc argumentow: https://developer.mozilla.org/en-US/docs/We...ts/Array/splice drugi argument to ile elementow ma byc usunietych, a ty wpisales 0 Ten post edytował PrinceOfPersia 15.01.2014, 21:34:04 -------------------- |
|
|
![]()
Post
#3
|
|
Grupa: Zarejestrowani Postów: 93 Pomógł: 7 Dołączył: 6.09.2011 Skąd: Olsztyn Ostrzeżenie: (0%) ![]() ![]() |
Tak też myślałem, nie wiem czy to się nie odnosi do tego samego elementu i może dlatego takie "jazdy" są. Oto kod:
Kod var avgData = {};
var temp = {}; $('.average-row').each(function(index, item){ //dodanie średnich i id studentów do data body temp['average'] = $(item).data('student-average'); temp['selector'] = $(item); avgData[$(item).data('student-id')] = temp; temp = {}; }); $('body').data('average-data', avgData); Ten post edytował Valantir 15.01.2014, 21:37:26 -------------------- Pomogłem? Kliknij "Pomógł".
|
|
|
![]()
Post
#4
|
|
Grupa: Zarejestrowani Postów: 717 Pomógł: 120 Dołączył: 18.04.2009 Ostrzeżenie: (0%) ![]() ![]() |
mialem na mysli kod, w ktorym inicjalizujesz zmienna/pole documents w obiektach
-------------------- |
|
|
![]()
Post
#5
|
|
Grupa: Zarejestrowani Postów: 93 Pomógł: 7 Dołączył: 6.09.2011 Skąd: Olsztyn Ostrzeżenie: (0%) ![]() ![]() |
Kod $.each($('body').data('average-data'), function(index, item){ //dodanie dokumentów do data body var element = $('body').data('average-data')[index]; element['documents'] = preload_data; }); -------------------- Pomogłem? Kliknij "Pomógł".
|
|
|
![]()
Post
#6
|
|
Grupa: Zarejestrowani Postów: 717 Pomógł: 120 Dołączył: 18.04.2009 Ostrzeżenie: (0%) ![]() ![]() |
a jak inicjalizujesz preload_data...?
(lepiej wklej cały kod, zamiast po kawałku). -------------------- |
|
|
![]()
Post
#7
|
|
Grupa: Zarejestrowani Postów: 93 Pomógł: 7 Dołączył: 6.09.2011 Skąd: Olsztyn Ostrzeżenie: (0%) ![]() ![]() |
Kod function replaceSelects(){
$('.pretty-select.draft-documents').select2({ width: '100%', multiple: true, ajax: { url: Routing.generate('get_documents_json'), dataType: 'json', quietMillis: 100, data: function (term) { return { term: term }; }, results: function (data) { var results = []; $.each(data, function(index, item){ results.push({ id: item.id, text: item.name }); }); return { results: results }; } } }); $('.pretty-select.draft-documents').on('change', function(e){ if($(this).select2('data').length > 0){ $('a.draft-documents').show(); } if($(this).select2('data')){ var select = $('<input type="text" multiple="multiple" class="input-row"/>'); $('.input-row-container').html(select); var preload_data = $(this).select2('data'); $.each($('body').data('average-data'), function(index, item){ //dodanie dokumentów do data body var element = $('body').data('average-data')[index]; var tempArray = new Array(); tempArray = preload_data; element['documents'] = tempArray; }); $('.input-row').select2({ multiple: true, width: '100%', query: function (query){ var data = {results: []}; $.each(preload_data, function(){ data.results.push({id: this.id, text: this.text }); }); query.callback(data); } }); $('.input-row').select2('data', preload_data); $('.input-row').on('select2-removed', function(e){ var studentId = $(e.target).closest('.average-row').data('student-id'); var removeIndex = ''; $.each($('body').data('average-data')[studentId]['documents'], function(index, item){ if(item.id == e.val){ removeIndex = index; } }); if(typeof removeIndex == 'number'){ console.log(removeIndex); $('body').data('average-data')[studentId]['documents'].splice(removeIndex, 0); } }); } }); } $(document).ready(function(){ $("#wizard").steps({ headerTag: "h2", bodyTag: "section", transitionEffect: "slideLeft", onStepChanging: function (event, currentIndex, newIndex) { if(currentIndex == 0 && newIndex == 1){ var data = $('#wizard').data(); data.steps[1]['contentUrl'] = Routing.generate('student_count_average', {'step': 2, 'specialityId': $('.select-speciality').val()}) } if(currentIndex == 1 && newIndex == 2){ $.ajax({ url: Routing.generate('student_count_average'), type: "POST", data: $('.count-average-form').serialize() + '&step=' + encodeURIComponent(3) + '&specialityId=' + encodeURIComponent($('.select-speciality').val()) }).done(function(data){ $('.third-step').html(data); var avgData = {}; var temp = {}; $('.average-row').each(function(index, item){ //dodanie średnich i id studentów do data body temp['average'] = $(item).data('student-average'); temp['selector'] = $(item); avgData[$(item).data('student-id')] = temp; temp = {}; }); $('body').data('average-data', avgData); replaceSelects(); }) } return true; } }); }); Ten post edytował Valantir 15.01.2014, 22:10:10 -------------------- Pomogłem? Kliknij "Pomógł".
|
|
|
![]()
Post
#8
|
|
Grupa: Zarejestrowani Postów: 717 Pomógł: 120 Dołączył: 18.04.2009 Ostrzeżenie: (0%) ![]() ![]() |
Kod var preload_data = $(this).select2('data'); $.each($('body').data('average-data'), function(index, item){ ..... tempArray = preload_data; element['documents'] = tempArray nie wiem co chcesz zrobić, ale teraz przypisujesz tę samą zmienną preload_data do wszystkich elementów tablicy. -------------------- |
|
|
![]()
Post
#9
|
|
Grupa: Zarejestrowani Postów: 93 Pomógł: 7 Dołączył: 6.09.2011 Skąd: Olsztyn Ostrzeżenie: (0%) ![]() ![]() |
Ta tablica preload_data zawiera dokumenty. Dokumenty mają trafić do każdego rekordu pod indeksem 'documents'. Użytkownik potem będzie mógł sobie usunąć wybrany dokument. W zależności który usunie, z tego rekordu dokument będzie usuwany. To jest ok, problem pojawia się przy usuwaniu, bo jest to chyba traktowane jako jedna ta sama tablica...:/
===================== Już załatwione. Kod $.each($('body').data('average-data'), function(index, item){ //dodanie dokumentów do data body var element = $('body').data('average-data')[index]; element['documents'] = preload_data.slice(); }); Wystarczyło dodać .slice() do owej tablicy preload_data Co do tego: Cytat drugi argument to ile elementow ma byc usunietych, a ty wpisales 0 to się machnąłem wklejając tutaj, bo wcześniej miałem tam 1. Ale i tak dzięki za pomoc - leci podziękowanie:) Pozdrawiam Ten post edytował Valantir 15.01.2014, 22:44:24 -------------------- Pomogłem? Kliknij "Pomógł".
|
|
|
![]() ![]() |
![]() |
Wersja Lo-Fi | Aktualny czas: 14.08.2025 - 12:29 |