gpt4 book ai didi

javascript - 仅对表格内容进行 javascript html 排序

转载 作者:行者123 更新时间:2023-11-28 03:40:47 25 4
gpt4 key购买 nike

我正在做一个元素,我正在做一个地址簿。我以表格的形式显示输入的数据。 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();


}

最佳答案

如果您不必考虑数据的服务器端分页,请查看 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();

关于javascript - 仅对表格内容进行 javascript html 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44218526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com