0

i'm trying to list my image preuploader sort by name but got some issues.i have dozen image with name img001 - img100 but when i input that the result show the files sort by image size. here is my code

<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>

can someone show me how to get it done ?

3 Answers 3

1

To achieve this you can sort() the table rows after you append a new one, based on the value of the input contained within the row. Try this:

$('#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>

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, glad to help
0

Instead of displaying the images as you read them, you could instead put them all i an array with the same for loop. Then sort the new array with a sort function. And then you just run through the array and display them. Works for me!

Comments

0

You can sort using javascripts Array build in function using a custom sort function with localeCompare. Inserting the following right before the for-loop should do what you want.

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

Providing that this.files is an array of files having a name attribute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.