191 lines
8.6 KiB
JavaScript
191 lines
8.6 KiB
JavaScript
// 格式化时间戳为日期字符串
|
|
function formatDate(timestamp) {
|
|
if (!timestamp) return '-';
|
|
const date = new Date(timestamp);
|
|
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
|
}
|
|
|
|
// 渲染问卷列表
|
|
function renderQuestionnaireList() {
|
|
// 获取问卷列表数据
|
|
const questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
|
|
const tableBody = document.querySelector('table');
|
|
|
|
// 保留表头
|
|
const tableHeader = tableBody.rows[0];
|
|
tableBody.innerHTML = '';
|
|
tableBody.appendChild(tableHeader);
|
|
|
|
// 渲染每个问卷
|
|
questionnaireList.forEach(questionnaire => {
|
|
const tr = document.createElement('tr');
|
|
tr.innerHTML = `
|
|
<td>
|
|
<div class="item-title">
|
|
<input type="checkbox" data-id="${questionnaire.id}">
|
|
<span>${questionnaire.title}</span>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<div class="data-row">
|
|
<span class="data-item">${formatDate(questionnaire.createTime)}</span>
|
|
<span class="data-item">${formatDate(questionnaire.publishTime)}</span>
|
|
<span class="data-item">${formatDate(questionnaire.data.endTime)}</span>
|
|
<span class="data-item">${questionnaire.status}</span>
|
|
<div class="data-item">
|
|
<button onclick="publishQuestionnaire('${questionnaire.id}')"
|
|
${questionnaire.status !== '未发布' ? 'disabled style="opacity: 0.5; cursor: not-allowed;"' : ''}>
|
|
发布问卷
|
|
</button>
|
|
<button onclick="editQuestionnaire('${questionnaire.id}')"
|
|
${questionnaire.status === '已发布' ? 'disabled style="opacity: 0.5; cursor: not-allowed;"' : ''}>
|
|
编辑问卷
|
|
</button>
|
|
<button onclick="deleteQuestionnaire('${questionnaire.id}')">
|
|
删除问卷
|
|
</button>
|
|
<button onclick="viewQuestionnaire('${questionnaire.id}')">
|
|
查看问卷
|
|
</button>
|
|
<button onclick="fillQuestionnaire('${questionnaire.id}')"
|
|
${questionnaire.status !== '已发布' || new Date().getTime() > new Date(questionnaire.data.endTime).getTime() ?
|
|
'disabled style="opacity: 0.5; cursor: not-allowed;"' : ''}>
|
|
填写问卷
|
|
</button>
|
|
<button onclick="analyzeQuestionnaire('${questionnaire.id}')"
|
|
${questionnaire.status !== '已发布' ? 'disabled style="opacity: 0.5; cursor: not-allowed;"' : ''}>
|
|
分析问卷
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
`;
|
|
tableBody.appendChild(tr);
|
|
});
|
|
}
|
|
|
|
// 删除问卷
|
|
function deleteQuestionnaire(id) {
|
|
if (!confirm('确定要删除这份问卷吗?')) return;
|
|
|
|
let questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
|
|
questionnaireList = questionnaireList.filter(q => q.id !== id);
|
|
localStorage.setItem('questionnaireList', JSON.stringify(questionnaireList));
|
|
renderQuestionnaireList();
|
|
}
|
|
|
|
// 批量删除问卷
|
|
function batchDeleteQuestionnaires() {
|
|
const checkedBoxes = document.querySelectorAll('input[type="checkbox"]:checked');
|
|
if (checkedBoxes.length === 0) {
|
|
alert('请先选择要删除的问卷!');
|
|
return;
|
|
}
|
|
|
|
if (!confirm(`确定要删除选中的 ${checkedBoxes.length} 份问卷吗?`)) return;
|
|
|
|
let questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
|
|
const idsToDelete = Array.from(checkedBoxes).map(cb => cb.dataset.id);
|
|
questionnaireList = questionnaireList.filter(q => !idsToDelete.includes(q.id));
|
|
localStorage.setItem('questionnaireList', JSON.stringify(questionnaireList));
|
|
renderQuestionnaireList();
|
|
}
|
|
|
|
// 全选/取消全选
|
|
function toggleSelectAll() {
|
|
const selectAllCheckbox = document.querySelector('.ctrl-all input[type="checkbox"]');
|
|
const allCheckboxes = document.querySelectorAll('table input[type="checkbox"]');
|
|
allCheckboxes.forEach(cb => cb.checked = selectAllCheckbox.checked);
|
|
}
|
|
|
|
// 发布问卷
|
|
function publishQuestionnaire(id) {
|
|
let questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
|
|
const index = questionnaireList.findIndex(q => q.id === id);
|
|
if (index !== -1) {
|
|
const now = new Date().getTime();
|
|
questionnaireList[index].publishTime = now;
|
|
questionnaireList[index].status = '已发布';
|
|
questionnaireList[index].data.publishTime = now;
|
|
questionnaireList[index].data.status = '已发布';
|
|
localStorage.setItem('questionnaireList', JSON.stringify(questionnaireList));
|
|
renderQuestionnaireList();
|
|
}
|
|
}
|
|
|
|
// 新建问卷
|
|
function createNewQuestionnaire() {
|
|
window.location.href = 'createQuestion.html';
|
|
}
|
|
|
|
window.onload = function() {
|
|
renderQuestionnaireList();
|
|
document.querySelector('.ctrl-all input[type="checkbox"]')
|
|
.addEventListener('change', toggleSelectAll);
|
|
document.querySelector('.ctrl-all button')
|
|
.addEventListener('click', batchDeleteQuestionnaires);
|
|
document.querySelector('.grid-item-add')
|
|
.addEventListener('click', createNewQuestionnaire);
|
|
}
|
|
|
|
// 编辑问卷
|
|
function editQuestionnaire(id) {
|
|
window.location.href = `editQuestion.html?id=${id}`;
|
|
}
|
|
|
|
// 查看问卷
|
|
function viewQuestionnaire(id) {
|
|
window.location.href = `viewQuestion.html?id=${id}`;
|
|
}
|
|
|
|
// 填写问卷
|
|
function fillQuestionnaire(id) {
|
|
// 检查问卷状态
|
|
const questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
|
|
const questionnaire = questionnaireList.find(q => q.id === id);
|
|
|
|
if (!questionnaire) {
|
|
alert('问卷不存在!');
|
|
return;
|
|
}
|
|
|
|
if (questionnaire.status !== '已发布') {
|
|
alert('该问卷尚未发布,无法填写!');
|
|
return;
|
|
}
|
|
|
|
const endTime = new Date(questionnaire.data.endTime).getTime();
|
|
const now = new Date().getTime();
|
|
if (now > endTime) {
|
|
alert('该问卷已过期!');
|
|
return;
|
|
}
|
|
|
|
// 跳转到填写问卷页面
|
|
window.location.href = `fillQuestion.html?id=${id}`;
|
|
}
|
|
|
|
// 分析问卷
|
|
function analyzeQuestionnaire(id) {
|
|
const questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
|
|
const questionnaire = questionnaireList.find(q => q.id === id);
|
|
|
|
if (!questionnaire) {
|
|
alert('问卷不存在!');
|
|
return;
|
|
}
|
|
|
|
if (questionnaire.status !== '已发布') {
|
|
alert('未发布的问卷无法查看分析结果!');
|
|
return;
|
|
}
|
|
const answers = JSON.parse(localStorage.getItem('questionnaireAnswers') || '[]');
|
|
const hasAnswers = answers.some(a => a.questionnaireId === id);
|
|
|
|
if (!hasAnswers) {
|
|
alert('暂无人填写该问卷,无法查看分析结果!');
|
|
return;
|
|
}
|
|
|
|
window.location.href = `analyzeQuestion.html?id=${id}`;
|
|
} |