Date sorting with Jquery

Asked

Viewed 24 times

0

Hello, I need to sort my news so that the last post is at the top, the difficulty I’m having is where I should organize my array... This issue is different from sorting by date pq HTML is not changing.

var noticias = []; //array de noticias

function atualizarLista(noticia) {
    var lista = $('#noticias-recentes-list');
    var li = $('<li id="noticia-' + noticia.id + '">');
    li.addClass('noticia');
    var p_titulo = $('<p>');
    p_titulo.addClass('titulo');
    p_titulo.attr('onclick', 'mostrarNoticia(' + noticia.id + ')');
    p_titulo.html(noticia.titulo);
    var p_conteudo = $('<p>');
    p_conteudo.addClass('conteudo');
    p_conteudo.html(noticia.conteudo
        + '<br>'
        + '<span>------------------</span>'
        + '<br>'
        + '<button onclick="ocultarNoticia(' + noticia.id + ')">Fechar</button>' + '<br>'
        + '<button onclick="mostrarAutor(' + noticia.id + ')">Mostrar dados do autor</button>' + '<br>'
        + '<button onclick="ocultarAutor(' + noticia.id + ')">Ocultar dados do autor</button>');
    var p_autor = $('<p>');
    p_autor.addClass('autor');
    p_autor.html(
        noticia.nome
        + '<br>' +
        noticia.email
        + '<br>' +
        noticia.data_publicacao);
    li.append(p_titulo, p_conteudo, p_autor);
    p_autor.hide();
    p_conteudo.hide();
    lista.append(li);
}

function salvar(form) {
    var titulo = $('#frm-titulo').val();
    var conteudo = $('#frm-conteudo').val();
    var nome = $('#frm-nome').val();
    var email = $('#frm-email').val();
    var data_publicacao = new Date($('#frm-data').val()).toDate('dd/mm/yyyy hh:ii');
    var data_atual = new Date().toDate('dd/mm/yyyy hh:ii');
    var noticia = {
        id: noticias.length,
        titulo: titulo,
        conteudo: conteudo,
        nome: nome,
        email: email,
        data_publicacao: data_publicacao,
        data_atual: data_atual
    };
    if (data_publicacao != undefined) {
        if (data_publicacao > data_atual || data_publicacao < data_atual) {
            alert("Sua notícia foi salva, mas ainda não foi publicada! Aguarde.");
        }
        else {
            noticias.push(noticia);
            //noticias.sort(compare);  //eu pensava que era só fazer isso, mas não está dando certo
            atualizarLista(noticia);
            alert("Notícia publicada!");
        }
    }
    //form.reset();
}

function mostrarNoticia(id) {
    $('.conteudo', '#noticia-' + id).show();
}

function ocultarNoticia(id) {
    $('.conteudo', '#noticia-' + id).hide();
}

function ocultarAutor(id) {
    $('.autor', '#noticia-' + id).hide();
}

function mostrarAutor(id) {
    $('.autor', '#noticia-' + id).show();
}
function compare(a,b) {                          // metodo para ordenar
    return a.data_publicacao < b.data_publicacao;
}

People my problem is that when adding a new news, does not update the order in my html dynamically...

  • Does ordering have to be done on the client side via javascript? If so, this post might help you: https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript

  • yes need, I will try to apply this solution here

  • the only problem with this is that it doesn’t update dynamically in my html

  • yes I saw that, the problem is that n updates html, it is still where to post, n by last post order...

  • What part of the code do you use the "news" array to popular your html? I could not find.

  • right there in save, in Else I push

  • The push is just adding a "news" item in the "news" array. To update the html you have to read this array (which was ordered by the Sort function) and render the HTML again

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.