我有一个员工表格,大约有1000-2000行。在表格中,我展示了员工的姓名和部门。当我获取表格数据时,我还会获取员工的其他详细信息(如出生日期、位置、角色等)。因此,用户可以选择编辑员工详细信息。当他点击编辑时,我需要在弹出窗口中显示其他员工详细信息。由于我已经将所有数据存储在JavaScript中,所以我可以搜索特定ID并显示所有详细信息。代码如下:
function getUserDetails(employeeId){
//i store all the employeedetails in a variable employeeInformation while getting //data for the grid.
for(var i=0;i<employeeInformation.length;i++){
if(employeeInformation[i].employeeID==employeeId){
//display employee details.
}
}
}
第二种解决方案是将员工ID传递给数据库,获取员工的所有信息。代码如下:
function getUserDetails(employeeId){
//make an ajax call to the controller which will call a procedure in the database
// to get the employee details
//then display employee details
}
那么,在处理1000-2000个记录时,你认为哪种解决方案是最优的?我不想通过在页面中存储大量数据来使JavaScript变得臃肿。
更新:
所以我的一个朋友提出了一个简单的解决方案。我为500行(平均值)存储4列,因此我不认为网页会非常缓慢。 在将行加载到表格中时,在编辑链接下,我将data-rowId作为属性赋予数据,以便于检索数据。 假设我将所有员工信息存储在名为employeeInfo的变量中。 当有人点击编辑链接时,$(this).attr('data-rowId')将给出rowId,而employeeInfo[$(this).attr('data-rowId')]应该给出关于员工的所有信息。 不需要存储员工ID并循环遍历员工表以查找匹配的员工ID,行号(rowid)就足够了。这非常简单,但是没有让我想起它。