代码之家  ›  专栏  ›  技术社区  ›  Pawan

如果json大小为1,则隐藏表td

  •  -2
  • Pawan  · 技术社区  · 7 年前

    我正在用json形成一个表,如下所示

    <script type="text/x-handlebars" data-template-name="index">
       <table class="table table-striped table-condensed">
        <thead>
            <tr>
                <th>Participant</th>
    
            </tr>
        </thead>
        <tbody id="ParticipantList">
            {{#each person in model}}
                <tr>
                    <td> {{person.name}}</td>
                        <td> {{person.city}}</td>
                <td><img src="image.jpg" alt="Remove"></td>
                </tr>
            {{/each}}
        </tbody>
       </table>
    </script>
    

    如果json的大小是1(1),我如何隐藏remove td(last td)

    http://jsfiddle.net/6Evrq/1805/

    2 回复  |  直到 7 年前
        1
  •  1
  •   Vignesh Raja    7 年前

    它可以通过一个简单的计算属性来实现。该过程在 IndexController .

    Working Fiddle

    模板:

    <script type="text/x-handlebars">
       {{outlet}}
    </script>
    <script type="text/x-handlebars" data-template-name="index">
       <table class="table table-striped table-condensed">
        <thead>
            <tr>
                <th>Participant</th>
            </tr>
        </thead>
        <tbody id="ParticipantList">
            {{#each person in model}}
                <tr>
                    <td> {{person.name}}</td>
                    <td> {{person.city}}</td>
                    {{#if ismorethanone}}
                         <td><img src="image.jpg" alt="Remove" {{action "removeUser" person.name}}></td>
                    {{/if}}
                </tr>
            {{/each}}
        </tbody>
       </table>
    </script>
    

    JS部分:

    App = Ember.Application.create();
    
    App.Router.map(function () {
        // put your routes here
    });
    
    App.IndexRoute = Ember.Route.extend({
        model: function() {
            return [ {"name": "John", "city": "New York"},
                    {"name": "SAAA","city": "California"},
                    {"name": "Vignesh","city": "India"}]
        }
    });
    
    App.IndexController = Ember.Controller.extend({
        ismorethanone : function(){
                return this.get("model").length>1;
        }.property("model.length"),
    
        actions :
        {
            removeUser:function(name){
                var model = this.get("model").filter(function(obj){
                     return obj.name!=name;
                });
                this.set("model",model);
            }
        }
    });
    
        2
  •  0
  •   Tiago Conceiçao    7 年前

    你可以用 if eq 像这样的灰烬助手:

     {{#if (eq model.length 1)}}
            <td><img src="image.jpg" alt="Remove"></td>
     {{/if}}
    

    或者,您的控制器中可以有一个computed属性,该属性隐藏/显示“删除”按钮,如下所示:

    //YourController.js
    hideRemoveBtn: Ember.computed.equal('person.length', 1)
    
    //html 
     {{#unless hideRemoveBtn}}
            <td><img src="image.jpg" alt="Remove"></td>
     {{/unless}}
    

    我希望这能有帮助!