Overlap of data to cycle through for

3

I have the following table:

Thistableisdynamicandworkslikeform,thatis,theuserclickingNovocreatesanewrowwiththedynamicfields.WhenyouclickSalvarDadosalllinescreatedbytheuseraresaved.Sofarsogood.ButwhatI'mdoingisgivingtheuserthepossibilitytoinsertfiles,forthatIhaveainputtype="file" also dynamic for each line (this is the hidden ), where it will load the data to controller from a ajaxSubmit (I'm working on ASP MVC 4).

My problem is now: When doing ajaxSubmit , javascript is overlapping the id's dynamic data I have for input file . Here is the function:

function submitDocCertISCC(numID, DivNrFicha) {
$("#formSaveFile" + numID + "_" + DivNrFicha + "").ajaxSubmit({
    type: "POST",
    url: $("#formSaveFile").attr("action"),
    clearForm: true,
    data: { numID: numID, DivNrFicha: DivNrFicha }
});
}

I call the function up when running through a for loop where I'll save each row of table at a time, and it works fine. Now comes the save part of the file and only writes the file from the last line. As it seems to me, it goes to the next step of the for cycle without first finishing the ajaxSubmit .

How can I circumvent this and make it not jump in the loop without finishing this function?

EDIT

Cycle For :

for (var i = 1; i <= window.numAutoDeclaracaoISCC; i++) {

$.getJSON("/Terceiros/saveAutoDeclar", { // Gravar dados da linha
                DivNrFicha: DivNrFicha, dtaEmissao: dtaEmissao, NumAutoDeclar: NumAutoDeclar, DtValid: DtValid, EmitidoPor: EmitidoPor,
                Anexo: Anexo, DtEntregaTec: DtEntregaTec, DtRecepcao: DtRecepcao
            },
            function (result) {
                submitDocCertISCC(i, DivNrFicha); // Fazer submit do form com o ficheiro

            });
        }
    
asked by anonymous 25.02.2014 / 15:28

2 answers

1

The problem is exactly what you suspected: since the ajax call is asynchronous, its callbacks are being called after the end of the for cycle, when the i value will be window.numAutoDeclaracaoISCC . This is because the scope of i is only one, of the context where that code is (in JavaScript, there is no block scope, only function).

A simple solution is to add an intermediate function to capture ( close over ) each value of i :

for (var i = 1; i <= window.numAutoDeclaracaoISCC; i++) {
    // assume que DivNrFicha é definido aqui
    $.getJSON("/Terceiros/saveAutoDeclar", { // Gravar dados da linha
            DivNrFicha: DivNrFicha,
            dtaEmissao: dtaEmissao,
            NumAutoDeclar: NumAutoDeclar,
            DtValid: DtValid,
            EmitidoPor: EmitidoPor,
            Anexo: Anexo,
            DtEntregaTec: DtEntregaTec,
            DtRecepcao: DtRecepcao
        },
        (function(i, DivNrFicha) {
            return function (result) {
                submitDocCertISCC(i, DivNrFicha); // Fazer submit do form com o ficheiro
            }
        }(i, DivNrFicha));
    });
}
    
25.02.2014 / 15:54
0

Use {async: true} in the ajaxSubmit () call, so the system will wait for the end ( 200 OK ) of each request.

$("#formSaveFile" + numID + "_" + DivNrFicha + "").ajaxSubmit({
    type: "POST",
    async: true,
    url: $("#formSaveFile").attr("action"),
    clearForm: true,
    data: { numID: numID, DivNrFicha: DivNrFicha }
});
    
25.02.2014 / 15:34