$(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 !
Rene Saarsoo
January 23, 2009, January 23, 2009 19:53, permalink
$(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>');
});
});
});
keif
January 23, 2009, January 23, 2009 20:25, permalink
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();
});
});
});
Rene Saarsoo
January 23, 2009, January 23, 2009 22:17, permalink
$(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();
});
});
Tien Dung
January 26, 2009, January 26, 2009 08:19, permalink
$(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();
});
});
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.