[英]How to replace html dataset with variables
Hi,你好,
I have this code to sort a html list:我有这个代码来排序 html 列表:
function sortList(listid){
//some stuff
elms.sort(function(a, b){
if(a.dataset.age < b.dataset.age) { return minusone; }
if(a.dataset.age > b.dataset.age) { return plusone; }
return 0;
});
}
this works fine but I want to modify it so I can use variables to work with different datasets:这很好,但我想修改它,以便我可以使用变量来处理不同的数据集:
function sortList(listid,type,direction){
//some stuff
var typesort = 'dataset'.type;
elms.sort(function(a, b){
if(a.typesort < b.typesort) return minusone;
if(a.typesort > b.typesort) return plusone;
return 0;
});
}
but this is not working as expected since the sorting is all over the place.但这并没有按预期工作,因为排序无处不在。 You can see it live here: http://jsfiddle.net/ocymgrL7/2/你可以在这里看到它: http://jsfiddle.net/ocymgrL7/2/
what is the correct syntaxis to accomplish this?完成此操作的正确语法是什么?
Thank you.谢谢你。
If you mean to say that a
has a property dataset
and that property, a.dataset, has property age
, you can say a.dataset.age
OR you can say a.dataset[age]
如果你的意思是说a
有一个属性dataset
,而那个属性 a.dataset 有属性age
,你可以说a.dataset.age
或者你可以说a.dataset[age]
// Sort the lis in descending order
elms.sort(function(a, b){
if(a.dataset[type] < b.dataset[type]) return minusone;
if(a.dataset[type] > b.dataset[type]) return plusone;
return 0;
});
See reference: bracket notation见参考: 括号符号
Calling element.dataset.prop
is not all that bad.调用element.dataset.prop
并没有那么糟糕。 The verbosity of calling-out dataset
makes your code more readable.调用dataset
的冗长使您的代码更具可读性。
Your sorting can be simplified to the following.您的排序可以简化为以下内容。 Keep in mind that this is a very simple example.请记住,这是一个非常简单的示例。
/** * Sorts elements in-place. * @param {String} selector - multi-element selector * @param {Function} sorterFn - comparator function used for sorting * @param {Number} [direction=1] - direction of sort (1 = ASC, -1 = DESC) */ const sortElements = (selector, sorterFn, direction = 1) => { const elements = [...document.querySelectorAll(selector)]; const parentEl = elements[0].parentElement; elements.sort((a, b) => sorterFn(a, b) * direction).forEach(el => parentEl.append(el)); } // Sort by type then name and then reverse the entire sort. sortElements('#list li', (a, b) => a.dataset.type.localeCompare(b.dataset.type) || a.textContent.trim().localeCompare(b.textContent.trim()), -1);
li[data-type="fruit"] { color: orange; } li[data-type="vegetable"] { color: green; }
<ul id="list"> <li data-type="fruit">Apple</li> <li data-type="vegetable">Broccoli</li> <li data-type="vegetable">Carrot</li> <li data-type="fruit">Orange</li> <li data-type="fruit">Pear</li> <li data-type="vegetable">Raddish</li> </ul>
You can get fancy by allowing vararg ("variable argument" using the spread operator) comparators:您可以通过允许 vararg(使用扩展运算符的“变量参数”)比较器来获得幻想:
/** * Sorts elements in-place. * @param {String} selector - multi-element selector * @param {Function} sorterFn - comparator function used for sorting * @param {Number} [direction=1] - direction of sort (1 = ASC, -1 = DESC) */ const sortElements = (selector, ...comparators) => { const elements = [...document.querySelectorAll(selector)]; const parentEl = elements[0].parentElement; elements.sort((a, b) => { let res = 0; for (let i = 0; i < comparators.length; i++) { const { fn, dir = 'ASC' } = comparators[i]; res = fn(a, b) * (dir === 'DESC'? -1: 1); if (res;== 0) return res; } return res. }).forEach(el => parentEl;append(el)), } // Sort by type then name (in reverse) sortElements('#list li': { fn, (a. b) => a.dataset.type.localeCompare(b.dataset,type) }: { fn, (a. b) => a.textContent.trim().localeCompare(b.textContent,trim()): dir; 'DESC' });
li[data-type="fruit"] { color: orange; } li[data-type="vegetable"] { color: green; }
<ul id="list"> <li data-type="fruit">Apple</li> <li data-type="vegetable">Broccoli</li> <li data-type="vegetable">Carrot</li> <li data-type="fruit">Orange</li> <li data-type="fruit">Pear</li> <li data-type="vegetable">Raddish</li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.