802c73427a78116c398cfb6765be9c08

Using jQuery, I'm trying to parse a text file from a carriage return split, pipe split format into an unordered list.
This looks kind of ugly, and I'm not fully immersed in jQuery so I figure there's a better way.

I tried using $('li')[i].text(term[0]); but it said it's not a function, and doing $('li').text(term[0]); rewrote the text on every LI to the last term found.

$(document).ready(function(){
	// get the text file to be parsed
	$.get('test-data.txt', function(textData){
		// split on carriage return
		var text = textData.split('\n');
		$(text).each(function(i){
			// we want the "top" (i.e. first) fifteen terms in the file
			if(i < 15)
			{
				// split the new entry on pipe ('|')
				var term = text[i].split('|');

				// create a new list item for each entry
				$('#tagcloud').append('<li></li>');

				// this will grab the current li and append the term
				$('li').each(function(j){
					if(i == j) $(this).text(term[0]);
				});
			}
		});
	});
});

Refactorings

No refactoring yet !

543d0222ea3269453527c07141d48e4b

Rene Saarsoo

January 23, 2009, January 23, 2009 19:53, permalink

No rating. Login to rate!
$(document).ready(function(){
  var MAX_NR_OF_LINES = 15;
  
  // get the text file to be parsed
  $.get('test-data.txt', function(textData){
    // get first n lines (no more than the maximum allowed)
    var lines = textData.split('\n').slice(0, MAX_NR_OF_LINES-1);
    $.each(lines, function(i, line){
      // split the line on pipe ('|') and take the first term
      var term = line.split('|')[0];

      // add new list item containing this term
      $('#tagcloud').append('<li></li>');
      $('#tagcloud li:last-child').text(term);

      // If the text in this file is safe, then you could instead use:
      // $('#tagcloud').append('<li>'+term+'</li>');

      // Or when you have an html-escaping function
      // There doesn't seem to be one offered by jQuery
      // $('#tagcloud').append('<li>'+escape_html(term)+'</li>');
    });
  });
});
802c73427a78116c398cfb6765be9c08

keif

January 23, 2009, January 23, 2009 20:25, permalink

No rating. Login to rate!

Awesome - thanks! I'm making this into a tagcloud, this is my (current) result:

$(document).ready(function(){
	// how many results do we want?
	var resultsMax = 15;

	// get the text file to be parsed
	$.get('test-data.txt', function(textData){

		// get the first n lines < lineMax
		var lines = textData.split('\n').slice(0, resultsMax);
		$.each(lines, function(i, line){
			// split the line on pipe ('|')
			// keeping it as a single term to squeeze a little more performance juice
			var term = line.split('|');

			// create a new list item for each entry
			$('#tagcloud').append('<li value="'+(15-i)+'"></li>');
			$('#tagcloud li:last-child').html('<a href="'+term[3]+'" title="'+term[0]+'">'+term[0]+'</a>'); // this is what I was looking for! Thanks.

			// start tagcloud, I can't find a "onComplete" function anywhere
			if(i == resultsMax-1) $('#tagcloud').tagcloud();
		});
	});
});
543d0222ea3269453527c07141d48e4b

Rene Saarsoo

January 23, 2009, January 23, 2009 22:17, permalink

1 rating. Login to rate!
$(document).ready(function(){
  var resultsMax = 15;

  $.get('test-data.txt', function(textData){
    var lines = textData.split('\n').slice(0, resultsMax);
    
    $.each(lines, function(i, line){
      var term = line.split('|');
      
      // I would go for some extra variables.
      // Array indexes are quite meaningless.
      var title = term[0];
      var url = term[3];

      // This way it should be easier to understand,
      // and more effective too, 
      // and avoids using the magic number 15.
      $('#tagcloud').append(
        '<li value="'+(resultsMax-i)+'">' +
        '<a href="'+url+'" title="'+title+'">'+title+'</a>' +
        '</li>');
    });
    
    // No need for any onComplete function,
    // just start it after the $.each() loop ends
    $('#tagcloud').tagcloud();
  });
});
5071c5b861341c0dcfcf6ac86327701f

Tien Dung

January 26, 2009, January 26, 2009 08:19, permalink

1 rating. Login to rate!
$(document).ready(function(){
  var resultsMax = 15;

  $.get('test-data.txt', function(textData){
    var lines = textData.split('\n').slice(0, resultsMax);
    
    $.each(lines, function(i, line){
      var terms = line.split('|');
      
      $('#tagcloud').append(
        '<li value="{index}"><a href="{url}" title="{title}">{title}</a></li>'.replace(/{(.+?)}/ig, function(raw, key) {
          return { 
            index : resultsMax - i, 
            url   : terms[3], 
            title : terms[0]
          }[ key ] || raw;
      }));      
    });
    
    $('#tagcloud').tagcloud();
  });
});
2a6571da26602a67be14ea8c5ab82349

dsa

April 19, 2010, April 19, 2010 12:08, permalink

No rating. Login to rate!

gfdgd

Your refactoring





Format Copy from initial code

or Cancel