如何将字母顺序添加到我的待办事项列表中. [英] How to add alphabetic order to my to do list.
问题描述
你好
我有一个待办事项清单.
它具有任务和优先级下拉菜单的输入.
任务应按优先级按排序顺序列出(从紧急到
如果可以的话"),那已经完成了.
而且按照字典顺序在每个优先级组中-这是我的问题.
我在
hello
I have a to do list.
it have input for the task and priority dropdown.
Tasks should be listed in a sort order according to priority ( from urgent down to
"If You Can" ), that''s already done.
And within each priority group according to lexicographical order - that''s my problem.
my code on
jsbin
上的代码
var add = document.getElementById("addButton");
var task = document.getElementById("Input");
var list = document.getElementById("list");
var body = document.querySelector("body");
var form = document.querySelector("#form");
//Add task, css and UI
add.addEventListener("click",function () {
if (task.value.length >= 6 &&
task.value.length <= 42) {
//Add task to the list
var newTask = document.createElement("li");
var x = document.getElementById("prioritySelect").selectedIndex;
newTask.dataset.priority = document.getElementsByTagName("option")[x].value;
var all = document.querySelectorAll("li");
var index = all.length;
for (var i = 0; i < all.length; i++) {
if (parseInt(newTask.dataset.priority) < parseInt(all[i].dataset.priority)) {
index = i;
break;
}
}
list.insertBefore(newTask, all[index]);
var taskName = document.createElement("h3");
newTask.appendChild(taskName);
taskName.innerHTML = task.value;
var priorSpan = document.createElement("span");
taskName.appendChild(priorSpan);
priorSpan.innerHTML = newTask.dataset.priority;
}
我尝试过的事情:
我不知道该怎么办.
我试图遍历优先级循环.
我试了又试,但解决不了.
What I have tried:
I don''t know what to do.
I tried to iterate through the priority loop.
I tried and tried and I can''t solve it.
推荐答案
您的以下代码将始终将新任务插入优先级组的最后一项:
This code of your will always insert the new task to the last item in the priority group:
for (var i = 0; i < all.length; i++) {
if (parseInt(newTask.dataset.priority) < parseInt(all[i].dataset.priority)) {
index = i;
break;
}
}
因此要在优先级组中找到其字典顺序,请添加一个循环以使之向后循环,如下所示:
So to find its lexicographical order in the priority group, add a loop to loop backwards like this:
for (var j = index - 1; j >= 0; j--) {
if (parseInt(newTask.dataset.priority) == parseInt(all[j].dataset.priority)) {
if(task.value.localeCompare(all[j].innerText)==-1) index = j;
}
}
我认为您应该使用排序数组方法..
像给数组vars一个键名...像a,b,c,d等等...然后在它们的values/上方提到它们.喜欢
i think you should use sort array method..
like give your array vars a key name... like a , b, c , d and so... then mention them just above their values/. like
var arr = [{
key: "a",
value: urgent
},
{
key: "d",
value: not so urgent
},
{
key: "c",
value: just imp
},
{
key: "b",
value: take your time
}];
现在以您想要的任何方式安排它们...
now arrange them in whatever way you want...
var order = ["c", "a", "b", "d"];
var sorted = _.sortBy(arr, function(obj){
return _.indexOf(order, obj.key);
});
编写有趣的代码...
have fun coding...
这篇关于如何将字母顺序添加到我的待办事项列表中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!