简体   繁体   English

如何在Django for循环中显示/隐藏特定元素

[英]How to ng-show/hide specific elements inside a django for loop

Hi am working on a Django powered website with some AngularJS. 嗨,我正在使用AngularJS在Django支持的网站上工作。 I have django template as follows 我有Django模板如下

<div ng-app="MyApp" ng-controller="MyCtrl">
    <script src="{{ STATIC_URL }}js/bootstrap.min.js"></script>
    <div class="well ">
        {% for k, m in items %}
            <table ng-show="show" class="table table-bordered tbl" ng-cloak>
              <tr align="left">
                <span>
                    <button ng-click="show=!show" class="btn btn-default drop">
              ..............

and angularjs 和angularjs

{% block extra_js %}
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var app = angular.module('MyApp', []);

app.controller('MyCtrl', function($scope) {
    $scope.show = false;
});
</script>
{% endblock extra_js %}

What am trying achieve here is whenever the user click on the button inside the the table hides or vice versa. 在这里试图实现的是,只要用户单击表隐藏中的按钮,反之亦然。 But as per the code when the user click the button all the tables (remember there will be multiple tables since its inside a for loop) are hiding instead of that particular table. 但是按照代码,当用户单击按钮时,所有表(请记住将会存在多个表,因为它位于for循环中)而不是该特定表被隐藏。 Is there anyway to hide only that particular table? 无论如何,是否只隐藏该特定表? instead of all the tables? 而不是所有表格? Thanks in advance. 提前致谢。

It's been a while but if I remember correctly you'll want to add a class to your table to identify it as the one you're wanting to hide. 已经有一段时间了,但是如果我没记错的话,您想在表中添加一个类,以将其标识为您要隐藏的类。 Then within there you have the button. 然后在其中有按钮。 On click you do something to the effect of element.closest grabbing the parent table of the button you clicked. 单击时,您可以执行一些操作来达到element.closest的效果,从而抓住您单击的按钮的父表。 From there you can call .toggle() If nothing else, Django keeps state of table ids using the field_name_1..2..3 so you could assign the class to the button and grab the number attached to it and use that as the identifier but that's kind of sloppy. 从那里可以调用.toggle()如果没有其他问题,Django使用field_name_1..2..3保持表ID的状态,因此您可以将类分配给按钮并获取按钮附带的数字并将其用作标识符但这有点草率。

The recommended way of doing something like this is not to use Django templating, but to pass in your variables as JSON objects and let Angular do all the templating. 推荐的类似方法不是使用Django模板,而是将变量作为JSON对象传入,并让Angular完成所有模板。 You can do that by sending them over Ajax or in the simplest case by just passing them in as Django template context and then adding them to the window object in an inline script at the top of your page. 您可以通过在Ajax上发送它们或在最简单的情况下将它们作为Django模板上下文传递,然后将它们添加到页面顶部的内联脚本中的window对象中来实现。 You can then access them from window in your Angular controller. 然后,您可以从Angular控制器的window中访问它们。 Both of these solutions will result in much neater code, and the second one will be no more effort to implement than what you have. 这两种解决方案都将使代码更加整洁,而第二种解决方案将比您所拥有的更多。

If you really want to continue down the path you're on, you could try the following. 如果您真的想继续前进,可以尝试以下方法。 Your javascript variable show could become an Object, with your Django object ids as keys (not sure exactly what the Django objects are you're iterating through in your {% for k, m in items %} loop, but you could use any unique id they have.) 您的javascript变量show可能成为对象,以Django对象ID作为键(不确定在{% for k, m in items %}循环中到底要迭代的Django对象是什么,但是您可以使用任何唯一的他们拥有的ID。)

So it would look something like this: 所以看起来像这样:

{% for k, m in items %}
    <table ng-show="show[{{ k }}]" class="table table-bordered tbl" ng-cloak>
        <tr align="left">
            <span>
                <button ng-click="show[{{ k }}] =! show[{{ k }}]" class="btn btn-default drop">

and

app.controller('MyCtrl', function($scope) {
   $scope.show = {};
});

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

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