我正在制作一个我正在制作地址簿的项目。我以表格的形式显示输入的数据。 reference image of output我想对通过HTML表单字段输入的数据进行排序。以下是我的代码。知道如何做到这一点。提前谢谢。
window.onload = function(){
// Buttons
var quickAddBtn = document.getElementById('QuickAdd');
var quickAddFormDiv = document.querySelector('.quickaddForm')
var cancelBtn = document.getElementById('Cancel');
var AddBtn = document.getElementById('Add');
// Form Fields
var lastname = document.getElementById('lastname');
var firstname = document.getElementById('firstname');
var email = document.getElementById('email');
var speaciality = document.getElementById('speaciality');
var practicename = document.getElementById('practicename');
// Divs etc.
var addBookDiv = document.querySelector('.addbook2');
quickAddFormDiv.style.display = "block";
localStorage.clear();
AddBtn.addEventListener("click", addToBook);
addBookDiv.addEventListener("click", removeEntry);
// Storage Array
var addressBook = [];
//localStorage['addbook'] = '[{"lastname":"Sachin B","practicename":"sachin@frameboxx.in","firstname":"93828292","email":"something","speaciality":"Chandigarh"}]';
function jsonStructure(lastname,firstname,email,speaciality,practicename){
this.lastname = lastname;
this.lastname += ", "+firstname;
//this.firstname = firstname;
this.email = email;
this.speaciality = speaciality;
this.practicename = practicename;
}
function addToBook(){
var isNull = lastname.value!='' && firstname.value!='' && email.value!='' && speaciality.value!='' && practicename.value!='';
if(isNull){
// format the input into a valid JSON structure
var obj = new jsonStructure(lastname.value,firstname.value,email.value,speaciality.value,practicename.value);
console.log(obj);
addressBook.push(obj);
localStorage['addbook2'] = JSON.stringify(addressBook);
console.log(localStorage['addbook2']);
//quickAddFormDiv.style.display = "none";
clearForm();
showaddressBook();
}
}
function removeEntry(e){
// Remove an entry from the addressBook
if(e.target.classList.contains('delbutton')){
var remID = e.target.getAttribute('data-id');
addressBook.splice(remID,1);
localStorage['addbook2'] = JSON.stringify(addressBook);
showaddressBook();
}
}
function clearForm(){
var formFields = document.querySelectorAll('.formFields');
for(var i in formFields){
formFields[i].value = '';
}
}
function showaddressBook(){
if(localStorage['addbook2'] === undefined){
localStorage['addbook2'] = '';
} else {
addressBook = JSON.parse(localStorage['addbook2']);
// Loop over the array addressBook and insert into the page
var str = '<table>'+'<div id="heading">'+'<th>'+'<tr>'+'<td id="hName">Name</td>'+'<td id="hEmail">Email</td>'+
'<td id="hPracticeName">Practice Name</td>'+
'<td id="hSpecialty">Specialty</td>'+
'<td id="hDel"></td>'+'</tr>'+'</div>'+'</th>';
addBookDiv.innerHTML = '';
for(var n in addressBook){
str += '<tr>';
str += '<td><div class="lastname"><p>' + addressBook[n].lastname + '</p></div></td>';
str += '<td class="email"><p>' + addressBook[n].email + '</p></td>';
str += '<td class="practicename"><p>' + addressBook[n].practicename + '</p></td>';
str += '<td class="specialty"><p>' + addressBook[n].speaciality + '</p></td>';
str += '<td class="del"><a href="#" class="delbutton" data-id="' + n + '">Delete</a></td>';
str += '</tr>';
//addBookDiv.innerHTML += str;
document.getElementsByClassName("addbook2")[0].innerHTML = str;
console.log(str);
}
}
}
showaddressBook();
}
答案 0 :(得分:0)
如果您不必考虑数据的服务器端分页,请查看jquery tablesorter。演示权here。
只需添加jquery作为依赖项并在表上初始化tablesorter插件,如下所示:
// in the <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="path/to/your/tablesorter.js"></script>
// right after calling showaddressBook()
$("table").tablesorter();