3

Hi I am new in this I try sort array on click event, and update my html at the same time, the issue is I have an array and I format in html element to show it

function contactM(contactList) {
    return  $.each(contactList, function(index, val){

 $('#list-contact').append("<li id="+ val.chatid +" class='list group'>"+
"<div class='userAvatar' style='background: #fff url(https:\/\/swinglifestyle.com/s1sp1cture5a/"+val.members[0].picture+ "') 50%; background-size: cover;'>"+
"<div class='status sm'></div>"+
  "</div>"+
  "<div class='userInfo'>"+
  "<div class='msg'>"+val.members[0].name +"</div>"+
 "<div class='msg'>" +val.message+"</div>"+
showNotMsg(val.message)+
                                        "<div class='date'>"+formatDate(val.newest_message) +"</div>"+
  "</div>"+unreadMsgLestThem(val.unread_cnt)
 +"</li>");
});    

}

this print a list of chat, now I using some like this. This is my click event I call inside the function is control the view for pass the sort array

   $('#by-name').on('click',function() {
       contactList.sort(SortByName);

       contactM(contactList);
   });

for example my view before sort and click

<div>B</div>
<div>A</div>

after click and sort

<div>B</div>
<div>A</div>
<div>A</div>
<div>B</div>

the problem is in the view show me the is sort correctly but also is duplicate some one can help me please ?

2
  • You're appending, but not removing the duplicate values. You need to search your $('#list-contact') for the duplicate or previously appended values, remove them, then append the sorted values. Commented Oct 30, 2018 at 18:27
  • Can you post you contactList array as well? Commented Oct 30, 2018 at 18:27

1 Answer 1

2

You're adding the items of an array called contactList to #list-contact, after which you press a button and it sorts items in the array, however this does not change the order in which they are displayed on screen.

Then you click on a button and are executing the function which added the elements in the first place, however you're never clearing the parent element of the previous (unsorted) items.

jQuery has the following function to do this, which you can add when the button is clicked:

$("#list-contact").empty();

Remove all child nodes of the set of matched elements from the DOM.


An example of how something like that would work in your code can be found below:

contactList = ["d", "a", "c", "b"]

function contactM(contactList) {
  return  $.each(contactList, function(index, val){
    $('#list-contact').append("<p>" + val + "</p>");
  }); 
}

$("#sortbutton").click(function() {
  contactList.sort();
  $("#list-contact").empty();
  contactM(contactList);
});

contactM(contactList);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="list-contact"></div>
<button id="sortbutton">Sort</button>

Sign up to request clarification or add additional context in comments.

1 Comment

Solve thanks so mush guys the last example is work well for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.