0
function Controles(contro, nomtab, numtab, action, nomcla, tipdat, lista, datos) {

    $(document).on('click', '.'+contro+' #IZQTOD', function(event) {
        $.getJSON(action+'&rows='+rows+'&page=1', function(datos) {
            var nuevafila;
            $.each(datos+tipdat, function(index, data) {
                nuevafila = nuevafila + "<tr class='Fila-Grid-"+nomcla+"' id='" + numtab + (index + 1) + "'>";
                nuevafila = nuevafila + "<td class='Columna1'>" + (index + 1) + "</td>";
                var list = lista.split("-");
                for (var j = 1; j < list.length; j++) {
                    nuevafila = nuevafila + "<td class='Borde-'>" + data+list[j] + "</td>"; 
                }
                nuevafila = nuevafila + "</tr>";
            });
            $('#'+nomtab+' tr:eq(1)').after(nuevafila);
        });
    });
}

I want to run this piece of code as a function of javascript in order to reuse code. The part that does not work for me is the part of each:    $. each (+ tipdat data, function (index, data) {

Where "datos" is an object with variables (set and get) (codcli, name, apepat) I mean to call codcli I do:

   $. each (datos.codcli, function (index, data) {
}

But this way is static. I want to do through dynamic parameters.

So the question is how to pass parameters to successfully achieve? Or is that you can not do? There will always be static?

in the code above what I want to do is, but obviously does not work:

tipdat=".codcli"
   $. each (datos+tipdat, function (index, data) {
}
3
  • For this example, could you translate to english? This makes no sence if you don't understand the language :) Commented Mar 25, 2014 at 17:29
  • please clean up the last to code examples, they are not easy to follow. thanks. :) Commented Mar 25, 2014 at 17:36
  • Please consider reading about dynamic-programming , it's not what you're doing here at all. Commented Mar 25, 2014 at 17:37

1 Answer 1

1

I think you're looking for bracket notation.

var tipdat = "codcli";
$.each(datos[tipdat], function (index, data) {
    //...
});

Is the same as:

$.each(datos.codcli, ...

If your string has multiple properties, I would do something like this:

var tipdat = "codcli.cod";

var objToIterate = datos;
var parts = tipdate.split('.');

for(var i = 0; i< parts.length; i++) {
    objToIterate = objToIterate[parts[i]];   
}

$.each(objToIterate, function (index, data) {
    //...
});
Sign up to request clarification or add additional context in comments.

5 Comments

I think this is what I seek. Tomorrow at work I try and give you your point.
that works good. last question: Whats up if var tipdat = "codcli" is tipdat = "codcli.cod" ?
I was referring to: $.each(datos.codcli.cod, ... is the same to: $.each(datos[parts[0]][parts[1]], ... ?
What I posted should give you same result as $.each(datos.codcli.cod, ...
i have problems with the sintaxis: i opened other question. help me there plz. stackoverflow.com/questions/22720296/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.