jQuery同位素按数字顺序对项目的排序不正确 [英] jQuery Isotope order by number sorts items incorrectly
问题描述
我在网站上设置了同位素订单并进行了排序,但是如果我更改了订单的编号,它将对项目进行错误排序.
I set up Isotope order and sort on my site but if I change the ordering to number it sorts items wrong.
例如,我有这样的项目:
for example I have items like this:
<div class="boxm" data-name="aaa" data-number="11944"></div>
<div class="boxm" data-name="bbb" data-number="1494"></div>
<div class="boxm" data-name="ccc" data-number="1574"></div>
<div class="boxm" data-name="ddd" data-number="1892"></div>
<div class="boxm" data-name="eee" data-number="19520"></div>
<div class="boxm" data-name="fff" data-number="2090"></div>
<div class="boxm" data-name="fgf" data-number="9010"></div>
具有这样的同位素代码:
with the Isotope code like this:
$container.isotope({
itemSelector : '.boxm',
masonry : {
columnWidth : 67,
cornerStampSelector: '.corner-stamp'
},
getSortData : {
name : function($elem){
return $elem.attr('data-name');
},
number : function($elem){
return $elem.attr('data-number');
}
}
});
$sortLinks.click(function(){
var $this = $(this);
if($this.hasClass('selected')){
return false;
}
var $isSet = $this.parents('.option-set');
$isSet.find('.selected').removeClass('selected');
$this.addClass('selected');
var options = {},
key = $isSet.attr('data-option-key'),
value = $this.attr('data-option-value');
value = value === 'false' ? false : value;
options[key] = value;
if(key === 'layoutMode' && typeof changeLayoutMode === 'function'){
changeLayoutMode($this, options)
}else {
$container.isotope(options);
}
return false;
});
如果我按数字排序,则其排序将与我在此处键入的命令相同;如果按DESC排序,则它将以2010跟随的9010开头.如果我是对的,这是因为Isotope仅监视前两个字符,但我需要正确订购.
if I press order by number it will order the same as I typed in here and if I press sort by DESC than it will start with 9010 folowed by 2090. if I'm right this is because Isotope watches only the first two character but I need to order it correctly.
我该如何解决这个问题?
How can I solve this problem?
推荐答案
可能是因为data-number属性的值不是整数值,而是字符串.试试这个:
It is probably because that the value of the data-number attribute is not considered an integer value but a string. Try this:
number : function($elem){
parseInt($elem.attr('data-number'), 10);
}
存储在字符串中的数字并非总是以逻辑方式排序,即199999由于起始数字而小于2.
Numbers stored in strings are sorted in a not always logic way, ie 199999 is less than 2 because of the initial digit.
(感谢balexandre关于添加基础的评论)
(thanks balexandre for the comment about adding the base)
这篇关于jQuery同位素按数字顺序对项目的排序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!