Fork me on GitHub

JS with array parameters  Bottom

  • Hi all.

    I'm trying create a module. In this module, i have an array (ex. [0]-> company1, [1]-> company2...), and i want send this array to a function in javascript.
    So, in the template file, add:

    Code

    <!--[pnpageaddvar name="javascript" value="modules/nameModule/pnjavascript/file.js"]-->

    Later

    Code

    <select name="Tipo" id="Tipo" onchange="Programador_cargaCmb(<!--[ $comp ]-->,<!--[ $categ ]-->)">
    <option value="empresa">Empresa</option>
    <option value="categoria">Categoría</option>
    </select>

    $comp and $categ are arrays.

    File JS:

    Code

    function Programador_cargaCmb(empresas, categorias)
    {
    var sel;
    sel = document.frmFilter.Tipo[document.frmFilter.Tipo.selectedIndex].value;
       
    if (sel == "empresa"){
    document.frmFilter.cmb.length = empresas.length;
    document.frmFilter.txt.value = empresas.length;
    for (i=0;i<empresas.length;i++){
    document.frmFilter.cmb.options[i].value = empresas[i];
    document.frmFilter.cmb.options[i].text = empresas[i];          
    }   
    }
    ...
    ...
    }


    Well, the problem is that "empresas" and "categorias", not is retrieved successfully. What can i do? What is the problem in the call function(template)?

    Thanks and sorry for my english poor.
  • Is not possible? Understand me? icon_confused
  • I guess the arrays that you are trying to use are not formatted as JavaScript arrays. You might need to loop trough them in Smarty (section of foreach) and build the arrays in JavaScript format. What is in $comp and $categ?
  • You can't directly pass PHP array to javasctipt.

    If $comp and $categ are one level array - the simplest way would be to implode PHP array in Smarty tpls and then split them in js code.

    It could look like this:
    Smarty - imploding arrays:

    Code

    <select name="Tipo" id="Tipo" onchange="Programador_cargaCmb(<!--[ $comp|@implode:'/' ]-->,<!--[ $categ|@implode:'/' ]-->)">


    Now you have simple strings, separated by slash (or any other separator you choose).

    Javascript - splitting strings to arrays:

    Code

    function Programador_cargaCmb(empresasStr, categoriasStr)
    {
        empresas= empresasStr.split('/');
        categorias= categoriasStr.split('/');


    Check what sing would be the best separator (make sure it's not contain in array values).

    --
    Polish Zikula Team
    Bianor Works - my Zikula works on CoZi
  • The Jusuff's solution is very valid!!! Now it works!!!

    Only 2 points:
    1. $comp and $categ are arrays. Ex:
    Array (36)
    0 => "Adobe"
    1 => "Autodesk"
    ...
    So

    Code

    <!--[ $comp|@implode:'/' ]-->
    it's not work. But is not a problem (i changed program's logic).

    2. The code:

    Code

    onchange="Programador_cargaCmb(<!--[ $comp|@implode:'/' ]-->,<!--[ $categ|@implode:'/' ]-->)">

    If i want pass a parameter string, use:

    Code

    onchange="Programador_cargaCmb('<!--[ $comp ]-->','<!--[ $categ ]-->')">

    Thanks you, its a solution that it work.



    edited by: krator, Mar 19, 2009 - 04:00 PM
  • Good to know that works.
    But it's strange, that implode modifier does not work icon_rolleyes

    --
    Polish Zikula Team
    Bianor Works - my Zikula works on CoZi

This list is based on users active over the last 60 minutes.