javve / list.js

The perfect library for adding search, sort, filters and flexibility to tables, lists and various HTML elements. Built to be invisible and work on existing HTML.
https://listjs.com
MIT License
11.2k stars 896 forks source link

Sort based on the text content of item's children excluding their html #303

Closed ahgz7 closed 8 years ago

ahgz7 commented 9 years ago

Hey all,

Really great work. I've noticed that while sorting, if the item being sorted has nested html elements, their html are considered in the sorting. So I recommend adding .replace(/<[^>]*>/g, "") in here: https://github.com/javve/list.js/blob/master/src/sort.js#L10

I've looked around in the issues for the same problem, but I didn't find any. Excuse me if I missed it.

Thank you

egardner commented 9 years ago

I've been dealing with this exact problem and trying to figure out how to work around it. It would be great if there was an option or flag that users could specify during the script initialization which would add this functionality if for some reason it doesn't make sense to enable by default (though I'm struggling to think of a case where you would want this behavior). @ahgz7 are you just adding the .replace call at the end of the line?

return naturalSort(itemA.values()[options.valueName], itemB.values()[options.valueName], options).replace(/<[^>]*>/g, "");
egardner commented 9 years ago

Actually, the line in question should look like this:

return naturalSort(itemA.values()[options.valueName].replace(/<[^>]*>/g, ""), itemB.values()[options.valueName].replace(/<[^>]*>/g, ""), options);

There are probably better ways to write this but I don't know them. It would be great to add this in as a new feature.

Thanks for posting, this is really helpful!

javve commented 8 years ago

I'm aware of this problem and the solution I suggest is to add a hidden column with just the value you want to sort without html.

<li>
  <span class="name"><h1>Jonny</h1></span>
  <span class="name-plain">Jonny</span>
</li>
.name-plain { display:none; }

...and in the next version that'll be released soon, you can use a data-attribute instead.