简体   繁体   English

在没有JQuery的情况下,将创建的节点从JavaScript添加到HTML表中并将其排序?

[英]Add and sort created nodes form JavaScript into a HTML table without JQuery?

first of all please notice, I'm new here and I'm not a program expert. 首先请注意,我是新来的,我不是程序专家。

What I want: I made a stopwatch in JavaScript/HTML/CSS and it's working. 我想要的是:我用JavaScript / HTML / CSS做了一个秒表,它正在运行。 Now I want to save the name which can be written in a textfield (HTML) and the time after I hit the save button into the table in the HTML file. 现在我想保存可以用文本字段(HTML)写的名称以及我在HTML文件中的表中点击保存按钮后的时间。 But I would like to have sorted it by time. 但我想按时间排序。

My Problem: I can save the the names and times into the table. 我的问题:我可以将名称和时间保存到表格中。 But I can't sort it directly after smashing the safe button. 但是在粉碎安全按钮后我无法直接对它进行排序。 I'm not an expert and I never used JQuery. 我不是专家,我从未使用过JQuery。 So, is there another way to sort this table immediately without any Plug-Ins? 那么,是否有另一种方法可以在没有任何插件的情况下立即对此表进行排序?

Below you see the function which saves the time and the name into the table and after that set the time to 0. 下面您将看到将时间和名称保存到表中的功能,然后将时间设置为0。

I also added the code the fiddle: https://jsfiddle.net/dtq6o52h/ 我还添加了代码小提琴: https//jsfiddle.net/dtq6o52h/

But please notice, that there is one file missing in fiddle (the main.js) 但请注意,小提琴中缺少一个文件(main.js)

Unfortunately i don't know how to add a second file into js fiddle. 不幸的是我不知道如何将第二个文件添加到js小提琴中。 The code for the main.js file you also can see below. main.js文件的代码也可以在下面看到。

Thank you, regards 谢谢,问候

McMauser McMauser

Function from stopwatch.js 来自stopwatch.js的功能

this.reset = function() {
  var playername = document.getElementById("name").value;

  var table = document.getElementsByTagName('table')[0];

  //
  var newRow = table.insertRow(1);

  var cel1 = newRow.insertCell(0)
  var cel2 = newRow.insertCell(1);
  var cel3 = newRow.insertCell(2);

  cel1.innerHTML = id;
  cel2.innerHTML = playername;
  cel3.innerHTML = timeFormatter(time);


  id++;
  time = 0;
  update();
  console.log(id);
};
this.isOn = false;
}

main.js main.js

var timer = document.getElementById('timer');
var toggleBtn = document.getElementById('toggle');
var resetBtn = document.getElementById('reset');
var watch = new Stopwatch(timer);

function start() {
  toggleBtn.textContent = 'Stop';
  watch.start();
}

function stop() {
  toggleBtn.textContent = 'Start';
  watch.stop();
}

toggleBtn.addEventListener('click', function() {
  watch.isOn ? stop() : start();
});

resetBtn.addEventListener('click', function() {
  watch.reset();
});

So, is there another way to sort this table immediately without any Plug-Ins? 那么,是否有另一种方法可以在没有任何插件的情况下立即对此表进行排序?

Yes! 是! Click on headers to sort data: 单击标题以对数据进行排序:

 (function(w) { w.data = [ { column1: "CCC", column2: "111", column3: "EEE" }, { column1: "AAA", column2: "BBB", column3: "CCC" } ]; w.sortData = function(columnName) { w.data = w.data.sort((a, b) => ((a[columnName] < b[columnName]) ? -1 : ((a[columnName] > b[columnName]) ? 1 : 0))); render(); } w.render = function() { var trs = data.map(a => `<tr><td>${a.column1}</td><td>${a.column2}</td><td>${a.column3}</td></tr>`).join(''); document.querySelector("#myTable tbody").innerHTML = trs; } })(window); 
 <table id="myTable"> <thead> <tr> <th onclick="sortData('column1');">Header 1</th> <th onclick="sortData('column2');">Header 2</th> <th onclick="sortData('column3');">Header 3</th> </tr> </thead> <tbody></tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM