【问题标题】:Hide the Table td incase the JSON size is one隐藏表 td 以防 JSON 大小为 1
【发布时间】:2018-06-12 11:42:01
【问题描述】:

我正在从 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(一),我如何隐藏 Remove td (last td)

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

【问题讨论】:

    标签: jquery ember.js


    【解决方案1】:

    可以通过一个简单的计算属性来实现。流程在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】:

      您可以像这样使用 ifeq ember 助手:

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

      或者你可以在你的控制器中有一个计算属性来隐藏/显示删除按钮,如下所示:

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

      希望对你有帮助!

      【讨论】:

      • 谢谢,第一个选项无法正常工作,期待 'CLOSE'、'CLOSE_UNESCAPED'、'STRING'、'INTEGER'、'BOOLEAN'、'ID'、'DATA'、'SEP' ,在 Parser.parseError (handlebars-v1.1.2.js:777) 处得到“无效”
      • 这更像是一个把手解析错误,而不是一个逻辑错误。您应该检查是否以正确的方式编译模板。检查这个handlebarsjs.com/installation.html
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2013-11-02
      • 2016-03-12
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多