Display data from text file with jQuery

2019-09-05 08:34发布

问题:

I need to display/append the data from a file, file.txt, containing 400 lines which are in the following format:

http://www.exemple.com/img1.jpg, title1, subtitle1, description1;
http://www.exemple.com/img2.jpg, title2, subtitle2, description2;
http://www.exemple.com/img3.jpg, title3, subtitle3, description3;
http://www.exemple.com/img4.jpg, title4, subtitle4, description4;
http://www.exemple.com/img5.jpg, title5, subtitle5, description5;

I know how to append 1 line into a <div>. But, here we have 4 elements on each line. I need to turn them into 4 elements that I could use to display them.

I am using this jQuery snippet that works fine for each line with 1 element and splits at the end of the line.

$.get('file.txt', function(data) {
  //var fileDom = $(data);

  var lines = data.split("\n");

  $.each(lines, function(n, elem) {
    $('#display').append('<div><img src="' + elem + '"/></div>');
  });
});

The HTML output would be:

$('#display').append('<div class="container"><div class="left"><img src="' + src + '"/></div><div class="right">' + title + '<br/>' + subtitle + '<br/>' + description + '</div></div>');

Thanks for insights!

回答1:

You could split a second time:

$.each(lines, function(n, line) {
    // Breaks up each line into ['src', 'title', 'subtitle', 'description']
    var elements = line.split(', ');
    $('#display').append('<div><img src="' + elements[0] + '"/></div>');
});


回答2:

You can split on the ' ':

$.get('file.txt', function(data) {
    //var fileDom = $(data);

    var lines = data.split("\n");

    $.each(lines, function(line, elem) {
        var parts = line.split(', '), // this splits the line into pieces
            src = parts[0],
            title = parts[1],
            subtitle = parts[2],
            description = parts[3].slice(0, -1); // remove the ';' from the description

        // at this point, you have the pieces of your line and can do whatever
        // you want with the data

        $('#display').append('<div><img src="' + src + '"/></div>');
    });
});


回答3:

Your error resides on the way you split...

You need to do something like that, (.when(file) should be replaced with .get('file.txt'):

var file = "http://www.exemple.com/img1.jpg, title1, subtitle1, description1; http://www.exemple.com/img2.jpg, title2, subtitle2, description2; http://www.exemple.com/img3.jpg, title3, subtitle3, description3; http://www.exemple.com/img4.jpg, title4, subtitle4, description4; http://www.exemple.com/img5.jpg, title5, subtitle5, description5;";

function AppendImagesCtrl($) {
  $
//  .get('file.txt')
  .when(file) // this is a fake
  .then(file => file.split(/; ?\n? ?/))
  .then(lines => lines.map((line, i) => {
    line = line.trim();
    if(!line) { return ""; }
    
    let [
      img, title, subtitle, description
    ] = line.split(/, ?n? ?/);
    
    return `<article id="${(i + 1)}">
<h1>${title}</h1>
<img src="${img}" alt="${img}" />
</article>`;
  }))
  .then(view => $('#example').append(view))
  ;  
}

window.jQuery(document).ready(AppendImagesCtrl);
img { width: 100px; height: 50px; background: cyan; }
article { padding: 5px 10px; background: lightseagreen; margin-bottom: 5px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="example"></div>