【问题标题】:Javascript Filereader sort by nameJavascript Filereader 按名称排序
【发布时间】:2018-11-02 08:01:41
【问题描述】:

我正在尝试按名称列出我的图像预上传器排序,但遇到了一些问题。我有十几个名称为 img001 - img100 的图像,但是当我输入结果显示文件按图像大小排序时。这是我的代码

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <title>generate nama ikan</title>
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            <div class="container">
              <div class="row">
                <div class="col-md-12">
                  <h1>Nama Ikan</h1>
                  <input id="ingambar" type="file" multiple />
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <div class="content">
                      <br />
                        <table class="table">
                          <thead>
                            <th>gambar</th><th>nama</th>
                          </thead>
                          <tbody></tbody>
                        </table>
                  </div>
                </div>
              </div>
            </div>
        </div>
    </body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script>
    $('#ingambar').change(function () {
        for (var i=0, len = this.files.length; i < len; i++) {
            (function (j, self) {
                var reader = new FileReader()
                reader.onload = function (e) {
                    $('tbody').append('<tr><td><img width="100px" src="' + e.target.result + '"></td><input class="form-control" type="text" value="' + self.files[j].name.slice(0, -4) + '" name="namaikan[]" disabled/></td></tr>')
                }
                reader.readAsDataURL(self.files[j])
            })(i, this);
        }
    });
    </script>
</html>

有人可以告诉我如何完成它吗?

【问题讨论】:

    标签: javascript jquery arrays sorting input


    【解决方案1】:

    要实现这一点,您可以在追加新行后sort() 表行,基于行中包含的input 的值。试试这个:

    $('#ingambar').change(function() {
      for (var i = 0, len = this.files.length; i < len; i++) {
        (function(j, self) {
          var reader = new FileReader()
          reader.onload = function(e) {
            $('tbody').append('<tr><td><img width="100px" src="' + e.target.result + '"></td><td><input class="form-control" type="text" value="' + self.files[j].name.slice(0, -4) + '" name="namaikan[]" disabled/></td></tr>');
            sortFiles();
          }
          reader.readAsDataURL(self.files[j])
        })(i, this);
      }
    });
    
    function sortFiles() {
      $('.table tbody tr').sort(function(a, b) {
        var $a = $(a).find('input'),
          $b = $(b).find('input');
        return $a.val().localeCompare($b.val());
      }).appendTo('.table tbody');
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    
    <div class="flex-center position-ref full-height">
      <div class="container">
        <div class="row">
          <div class="col-md-12">
            <h1>Nama Ikan</h1>
            <input id="ingambar" type="file" multiple />
          </div>
        </div>
        <div class="row">
          <div class="col-md-12">
            <div class="content">
              <br />
              <table class="table">
                <thead>
                  <th>gambar</th>
                  <th>nama</th>
                </thead>
                <tbody></tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

    • 没问题,很高兴为您提供帮助
    【解决方案2】:

    您可以在阅读时不显示图像,而是将它们全部放在一个具有相同 for 循环的数组中。然后使用排序函数对新数组进行排序。然后你只需遍历数组并显示它们。为我工作!

    【讨论】:

      【解决方案3】:

      您可以使用带有 localeCompare 的自定义排序函数使用 javascripts 数组内置函数进行排序。在 for 循环之前插入以下内容应该可以满足您的要求。

      this.files.sort((fileA, fileB) => fileA.name.localeCompare(fileB.name));
      

      假设this.files 是一个具有name 属性的文件数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-04
        • 1970-01-01
        • 1970-01-01
        • 2013-07-21
        • 2018-03-26
        • 1970-01-01
        • 1970-01-01
        • 2014-09-19
        相关资源
        最近更新 更多