javascript - Sorting large number of DOM elements -
i'm pulling large number of nodes sharepoint list , i'll try sort them html table in orderly fashion. way need need split these z:rows 5 different tables. example should self explanatory: https://jsfiddle.net/jse21z9d/1/
$(document).ready(function(){ $('#execb').click(function(){ $('.myul li').each(function(){ var litext = $(this).text().tolowercase(); var newlitext = litext.replace(/[{()}]/g, ""); var textarray = newlitext.split(/[,]/); console.log(textarray); $(textarray).each(function(index){ switch(true){ case (textarray[index].indexof('pol') != -1): $('#poldiv').append(litext+"<br>"); break; case (textarray[index].indexof('uk') != -1) || (textarray[index].indexof('ire') != -1): $('#ukdiv').append(litext+"<br>"); break; case (textarray[index].indexof('ger') != -1): $('#gerdiv').append(litext+"<br>"); break; case (textarray[index].indexof('san') != -1): $('#sandiv').append(litext+"<br>"); break; } }); }); }); });
so that's got far, , i'm wondering maybe there better way think code wrote might slow entire load lot if talking 1000+ z:rows(li in case) go through?
i modified own code following:
- less appends: iterations generate string instead of multiple appends less actions done on dom.
- less searches: though search id easy search. still better execute once, , append generated string.
source: https://jsfiddle.net/mamtkjw4/
$(document).ready(function(){ $('#execb').click(function(){ var polstr = ""; var ukstr = ""; var gerstr = ""; var sanstr = ""; $('.myul li').each(function(){ var litext = $(this).text().tolowercase(); var newlitext = litext.replace(/[{()}]/g, ""); var textarray = newlitext.split(/[,]/); console.log(textarray); $(textarray).each(function(index){ switch(true){ case (textarray[index].indexof('pol') != -1): polstr = polstr + litext+"<br>"; break; case (textarray[index].indexof('uk') != -1) || (textarray[index].indexof('ire') != -1): ukstr = ukstr + litext+"<br>"; break; case (textarray[index].indexof('ger') != -1): gerstr = gerstr + litext+"<br>"; break; case (textarray[index].indexof('san') != -1): sanstr = sanstr + litext+"<br>"; break; } }); }); if (polstr) { $('#poldiv').append(polstr+"<br>"); } if (ukstr) { $('#ukdiv').append(ukstr+"<br>"); } if (gerstr) { $('#gerdiv').append(gerstr+"<br>"); } if (sanstr) { $('#sandiv').append(sanstr+"<br>"); } }); });
.holders{ background: gray; width: 100px; height: 200px; margin-left: 15px; margin-bottom: 15px; float: left; }
<button id="execb">execb</button> <ul class="myul"> <li>(uk/ire, pol)</li> <li>(uk/ire)</li> <li>(pol)</li> <li>(san)</li> <li>(ger, pol)</li> <li>(ger)</li> <li>(san, uk/ire)</li> </ul> <div id="gerdiv" class="holders"><p>german div:</p><ul></ul></div> <div id="ukdiv" class="holders"><p>uk/ire div:</p><ul></ul></div> <div id="poldiv" class="holders"><p>pol div:</p><ul></ul></div> <div id="sandiv" class="holders"><p>san div:</p><ul></ul></div>
Comments
Post a Comment