Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Usuwanie elementu z tablicy powoduje dziwne zachowanie
Valantir
post
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ę
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Valantir
post
Post #2





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
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 17.10.2025 - 04:56