使用js将下列的工资表保存到变量salary里,工时表保存到hours里。1.网页上显示一个下拉列表,选择对应的姓名,下面动态显示工资表和工时表,及对应的工资和工时小计。2.统计员工时薪最高的2位员工姓名、总薪资、总工时和平均时薪,并将结果以大方美观样式输出到html页面里
时间: 2024-02-18 12:03:56 浏览: 172
首先,我们需要将工资表和工时表保存到变量中。假设工资表和工时表的数据如下:
```
var salary = {
"Alice": {
"Jan": 2000,
"Feb": 2500,
"Mar": 3000
},
"Bob": {
"Jan": 1500,
"Feb": 1800,
"Mar": 2000
},
"Charlie": {
"Jan": 3000,
"Feb": 3500,
"Mar": 4000
}
};
var hours = {
"Alice": {
"Jan": 80,
"Feb": 90,
"Mar": 100
},
"Bob": {
"Jan": 70,
"Feb": 80,
"Mar": 90
},
"Charlie": {
"Jan": 100,
"Feb": 110,
"Mar": 120
}
};
```
接下来,我们可以创建一个下拉列表,让用户选择对应的姓名。当用户选择一个姓名时,我们可以动态显示该员工的工资表和工时表,并计算对应的工资和工时小计。代码如下:
```
// 获取下拉列表和显示表格的元素
var selectElement = document.getElementById("select-employee");
var salaryTable = document.getElementById("salary-table");
var hoursTable = document.getElementById("hours-table");
var salaryTotal = document.getElementById("salary-total");
var hoursTotal = document.getElementById("hours-total");
// 更新显示表格和小计
function updateTables(employee) {
// 清空表格和小计
salaryTable.innerHTML = "";
hoursTable.innerHTML = "";
salaryTotal.innerHTML = "";
hoursTotal.innerHTML = "";
// 获取该员工的工资表和工时表
var employeeSalary = salary[employee];
var employeeHours = hours[employee];
// 显示工资表和工时表
for (var month in employeeSalary) {
// 创建新的表格行
var salaryRow = document.createElement("tr");
var hoursRow = document.createElement("tr");
// 添加月份列
var monthCell = document.createElement("td");
monthCell.innerHTML = month;
salaryRow.appendChild(monthCell);
hoursRow.appendChild(monthCell.cloneNode(true));
// 添加工资列
var salaryCell = document.createElement("td");
salaryCell.innerHTML = employeeSalary[month];
salaryRow.appendChild(salaryCell);
// 添加工时列
var hoursCell = document.createElement("td");
hoursCell.innerHTML = employeeHours[month];
hoursRow.appendChild(hoursCell);
// 添加行到表格
salaryTable.appendChild(salaryRow);
hoursTable.appendChild(hoursRow);
}
// 计算并显示小计
var salarySubtotal = Object.values(employeeSalary).reduce((a, b) => a + b);
var hoursSubtotal = Object.values(employeeHours).reduce((a, b) => a + b);
salaryTotal.innerHTML = salarySubtotal;
hoursTotal.innerHTML = hoursSubtotal;
}
// 监听下拉列表的选择事件
selectElement.addEventListener("change", function() {
// 获取选择的员工
var employee = selectElement.value;
// 更新显示表格和小计
updateTables(employee);
});
```
最后,我们需要统计员工时薪最高的2位员工姓名、总薪资、总工时和平均时薪,并将结果以大方美观样式输出到html页面里。代码如下:
```
// 统计员工时薪并排序
var employees = Object.keys(salary);
var hourlyRates = employees.map(function(employee) {
var employeeSalary = salary[employee];
var employeeHours = hours[employee];
var totalSalary = Object.values(employeeSalary).reduce((a, b) => a + b);
var totalHours = Object.values(employeeHours).reduce((a, b) => a + b);
var hourlyRate = totalSalary / totalHours;
return {
"name": employee,
"hourlyRate": hourlyRate
};
}).sort(function(a, b) {
return b.hourlyRate - a.hourlyRate;
});
// 获取统计结果和输出元素
var topEmployees = employees.slice(0, 2);
var totalSalary = Object.values(salary).reduce((a, b) => a.concat(Object.values(b))).reduce((a, b) => a + b);
var totalHours = Object.values(hours).reduce((a, b) => a.concat(Object.values(b))).reduce((a, b) => a + b);
var averageHourlyRate = totalSalary / totalHours;
var outputElement = document.getElementById("output");
// 构建输出HTML
var html = "<h2>员工时薪最高的2位员工</h2>";
html += "<ol>";
for (var i = 0; i < topEmployees.length; i++) {
html += "<li>" + topEmployees[i] + "</li>";
}
html += "</ol>";
html += "<h2>总薪资</h2>";
html += "<p>" + totalSalary + "</p>";
html += "<h2>总工时</h2>";
html += "<p>" + totalHours + "</p>";
html += "<h2>平均时薪</h2>";
html += "<p>" + averageHourlyRate + "</p>";
// 输出到页面
outputElement.innerHTML = html;
```
以上代码仅供参考,具体实现可以根据具体需求进行修改和调整。
阅读全文