繁体   English   中英

如何创建检查文件扩展名的Handlebars助手

[英]How to create a Handlebars helper that check for the file extension

您好,我正在尝试检查我的文件扩展名是JPG,PNG,PDF,DOC还是Excel,以根据文件扩展名显示图标。

var info = [
  {
    IdTrnx = 1234567
    files = [{
      0:"pic.jpg",
      1:"file.pdf"
    }]
  },
  {
    IdTrnx = 7654321
    files = [{
      0:"pic.png",
      1:"file.doc"
    }]
  }
]

多数民众赞成在我的对象。

这是我当前的把手。

<table id="tb-tickets" class="table">
    <thead>
        <tr>
            <th data-field=""># Transacción</th>
            <th data-field="">Tickets</th>
        </tr>
    </thead>
    <tbody>
        {{#each this}}
            <tr>
                <td>{{IdTrnx}}</td>
                <td>
                    {{#each files}}
                        <button type="button" class="btn btn-sm btn-outline-secondary" data-toggle="modal" data-target="#fileModaltickets" data-file="{{this}}" id="getFile"><i class="fa fa-file-text-o"></i></button>
                    {{/each}}
                </td>
            </tr>
        {{/each}}
    </tbody>
</table>

Rigth现在为每个文件显示相同的图标。 如何创建车把助手以检查扩展名并将图标设置为正确的扩展名。

这是您声明助手的方式

Handlebars.registerHelper('checkExtension', function(item, options) {
    var regex = '/.*jpg|JPG|png|PNG|doc|DOC|xls|XLS|pdf|PDF$/';
    if (item.match(regex)) {
      return "checked";
    } else {
      return "";
    }
});

这是模板中的用途:

<button type="button" class="btn btn-sm btn-outline-secondary" data-toggle="modal" data-target="#fileModaltickets" data-file="{{this}}" id="getFile" {{checkExtension this}}><i class="fa fa-file-text-o"></i></button>

如果您的文件具有正确的文件扩展名,这将添加单词“ checked”

暂无
暂无

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

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