简体   繁体   English

使用jQuery或Angular动态添加或删除表行?

[英]Dynamically add or remove table rows with jQuery or Angular?

This is the code I have. 这是我的代码。 I'm using Symfony/Twig to pass the variables and translation strings in (if anyone was unsure what the {{ , }} , {% trans %} etc was for). 我正在使用Symfony / Twig传递变量和翻译字符串(如果有人不确定{{}}{% trans %}等的用途)。

Please see the line where I have the glyphicon glyphicon-camera - what I want is for the user to be able to click this, and a new row appears directly below containing the contents of row.getPhoto() - the icon will only appear if row.getPhoto() is not null, so therefore clicking it will always mean there is content to show. 请查看我有glyphicon glyphicon-camera -我想要的是用户可以单击它,然后在下面直接显示新行,其中包含row.getPhoto()的内容-该图标仅在以下情况出现row.getPhoto()不为null,因此单击它始终意味着要显示的内容。

Likewise, clicking the photo icon again will make the row disappear. 同样,再次单击照片图标将使该行消失。

How can I do this? 我怎样才能做到这一点? I'm not sure if I should use jQuery or Angular (I am using both in other places in the project, so both are easily available for me). 我不确定是否应该使用jQuery或Angular(我在项目的其他地方都使用了jQuery,所以我都可以轻松使用它们)。 Any comments welcome, thank you. 欢迎任何意见,谢谢。

    <table class="table">
    <tr>
        <th width="10%">{% trans %} header.item {% endtrans %}</th>
        <th width="60%">{% trans %} header.action {% endtrans %}</th>
        <th width="10%">{% trans %} header.option1 {% endtrans %}</th>
        <th width="10%">{% trans %} header.option2 {% endtrans %}</th>
        <th width="10%">{% trans %} header.option3 {% endtrans %}</th>
    </tr>

    {% for row in showRows(allItems) %}
        <tr>
            <td>
                {{ row.getItem() }}
            </td>

            <td>
                {{ row.getAction() }} {% if row.getPhoto() is not null %} <span class="pull-right show-hide-photo glyphicon glyphicon-camera"></span>{% endif %}
            </td>

            <td>
                {% if row.getOption1() %}<span class="glyphicon glyphicon-ok"></span>{% endif %}
            </td>

            <td>
                {% if row.getOption2() %}<span class="glyphicon glyphicon-ok"></span>{% endif %}
            </td>

            <td>
                {% if row.getOption3() %}<span class="glyphicon glyphicon-ok"></span>{% endif %}
            </td>
        </tr>
    {% endfor %}
    </table>

Only jQuery I have right now is this, to make the icon appear like a link when hovered over: 我现在只有jQuery,它是将鼠标悬停在该图标上时看起来像一个链接:

    // Photo button
    $('.show-hide-photo').css('cursor', 'pointer');

You can always pass symphony2 variables on javascript code, so you can have a scirpt with something along the lines of: 您始终可以在javascript代码上传递symphony2变量,因此可以对以下内容进行脚本编写:

<scirpt>
$(.glyphicon).click(function(){
     $(.Some-other-class).toggle()
});
</script>

You can have the .Some-other-class div element or td element starting as hidden and with variables in it (like you did in your static html code). 您可以将.Some-other-class div元素或td元素以隐藏的开头并包含变量(就像在静态html代码中一样)。

You don't need the css pointer in the js use it in a css class to have it when the page load not when the user click. 您不需要js中的css指针在css类中使用它来在页面加载时(而不是在用户单击时)使用它。

And for your click you can do something like this if you photo is a link then : 如果您的照片是链接,则可以单击以执行以下操作:

first use in your twig a <img src="{{row.photo}}" alt="" style="display:none;"/> whenever you want to put the images. 每当您要放置图像时,请先在树枝中使用<img src="{{row.photo}}" alt="" style="display:none;"/>

then inside your js in the click photo button 然后在您的js中点击照片按钮

$('.show-hide-photo').on('click', function(){

   $(this).closest('tr').find('img').toggle();

});

$(this) is your show-hide-photo button , closest('tr') will look for the tag which contain your button then find('img') will go find the tag inside that (row) in that case you wont need to bother with ids to select right row etc.. $(this)是您的show-hide-photo按钮, closest('tr')将查找包含按钮的标签,然后find('img')将在该(行)中查找标签,在这种情况下,您不会需要打扰以选择右行等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM