Javascript 为jQuery传递一个细枝数组

Javascript 为jQuery传递一个细枝数组,javascript,php,jquery,twig,Javascript,Php,Jquery,Twig,我的树枝上有一个块,它从控制器接收的变量中写入一个表 {% block foot_informations %} {% if ads is not empty %} <div class="panel-foot-information row"> <table id="ads"> <thead> <tr>

我的树枝上有一个块,它从控制器接收的变量中写入一个表

{% block foot_informations %}
    {% if ads is not empty %}
        <div class="panel-foot-information row">
            <table id="ads">
                <thead>
                <tr>
                    <th>Departure</th>
                    <th>Destination</th>
                </tr>
                </thead>
                <tbody>
                {% for ad in ads %}
                    <tr class="ad-tr">
                        <td>{{ ad.departure }}</td>
                        <td>{{ ad.packageType }}</td>
                        <td>{{ ad.transportation }}</td>
                        {# <td>{{ ad.date }}</td> #}
                        <td><a href="{{ path('ad_select', { 'id': ad.id }) }}" class="tr-link">select</a></td>
                        <td class="hidden"><input type="hidden" id="idLat" value="{{ ad.departureLatitude }}"/></td>
                        <td class="hidden"><input type="hidden" id="idLong" value="{{ ad.departureLongitude }}"/></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}
{% endblock %}

首先,我建议你不要把两种策略混在一起

  • 正在服务器端生成视图,这显然是您使用细枝模板所做的

  • 正在传递原始数据(使用AJAX或类似您的示例,将json_编码的数组解析为JS对象),然后使用JS DOM操作生成表

  • 但这是我对这部分的看法

    如果选择选项1,则可以在
    $中添加/删除类。对于要隐藏/显示的表行,每个
    类都会进行类似于过滤器的回调。
    然后在样式表中写下类似的内容

    tr.filtered {
      display: none;
    }
    
    备选方案:按如下方式扩展表体:

    <tbody>
      {% for ad in ads %}
        <tr data-ad-id="{{ ad.id }}" class="ad-tr">
          <td>{{ ad.departure }}</td>
          {# all the other td's #}
        </tr>
      {% endfor %}
    </tbody>
    
    编辑:

    如果您的基本模板中有一个
    javascripts
    块,您可以这样做,将ads数组公开给全局JS作用域(正如您在问题中所说,关于您所看到的内容):

    {%block javascripts%}
    {{parent()}}
    var ads={{ads|json_encode()}};
    {%endblock%}
    
    但文章中的此类广告:
    $。每个(广告,函数(索引,广告)
    来自何处?要执行
    警报(ad.packageType)
    似乎未定义。我认为您必须调查广告的内容。我不知道它是如何组成的,以及如何或是否设置了此对象的某些属性。只需检查生成的html即可(包括脚本块)使用浏览器读取ads json字符串的内容。或者转到您的或其他控制台,等等。控制台并键入
    ads
    ,这应显示ads对象的内容。另一个问题可能是您希望在细枝模板中进行json编码的对象是延迟加载的。这意味着,它必须在中实现可序列化的接口为了得到json格式的表达。你会发现到处都有很多关于这方面的资源。我是如何设法不使用jQuery数组的,这也决定了另一种方法,非常感谢你的帮助。
    <tbody>
      {% for ad in ads %}
        <tr data-ad-id="{{ ad.id }}" class="ad-tr">
          <td>{{ ad.departure }}</td>
          {# all the other td's #}
        </tr>
      {% endfor %}
    </tbody>
    
    $('#my_button').click(function() {
      //alert(ads);
      $.each(ads, function(index, ad) {
    
        if (ad.packageType == 'some_package_type') {
          $('table#ads tr[data-ad-id=' + ad.id + ']').hide();
        }
      });
      // rewrite table
      // Perhaps there is no need for anymore
    });
    
    {% block javascripts %}
      {{ parent() }}
      <script>
        var ads = {{ ads|json_encode() }};
      </script>
    {% endblock %}