Good practice when loading a div with database data, using jQuery

1

What is the best way to build a div with jQuery Example:

$(document).ready(function(){
$.ajax({
    type:'post',        
    dataType: 'json',   
    url: 'listAllForum',
    success: function(dados){
        for(var i = 0; dados.length > i; i++){

            $('#post-forum').append('<strong>Usuario Fulano de Tal</strong> - Data: 00/00 <br>' + 
                    dados[i].idForum + ' - '+ dados[i].message + '<br> <br>');
        }
    }
});

});

For example: I want each message to have a different color for easy reading. The user name puts the tags to give a highlight, but is it okay to do this within the ajax for? If I want to put a link to redirect to a @RequestMapping, where would I place the href?

    
asked by anonymous 12.04.2017 / 22:01

1 answer

0

As you do not use a template-teach, better way would be to add some color classes in your CSS, and after the ajax request you return in your json object the right color for each post.

$ (document) .ready (function () { $ .ajax ({     type: 'post',
    dataType: 'json',
    url: 'listAllForum',     success: function {         for (var i = 0; data.length> i; i ++) {

        //Get cores
        var varCor = dados.cor;
        var contentPosts =  $('#post-forum');         

        contentPosts.append('<div class="' +  varCor + '">Usuario Fulano de Tal</strong> - Data: 00/00 <br>' + 
                dados[i].idForum + ' - '+ dados[i].message + '</div>');
    }
}

});

    
12.04.2017 / 22:28