main
HuangHai 3 weeks ago
parent c670be0f8f
commit 4fd3e6a1a5

@ -73,6 +73,10 @@
} }
} }
function findParentTitle(node) {
const parent = allNodes.find(n => n.id === node.parent_id);
return parent ? parent.title : '';
}
function findNodeById(nodes, id) { function findNodeById(nodes, id) {
for (var i = 0; i < nodes.length; i++) { for (var i = 0; i < nodes.length; i++) {
@ -123,7 +127,7 @@
// 初始化数据 // 初始化数据
// Move this declaration outside the AJAX call // Move this declaration outside the AJAX call
var allNodes = []; var allNodes = [];
// Add flattenTree function definition before the AJAX call // Add flattenTree function definition before the AJAX call
function flattenTree(nodes) { function flattenTree(nodes) {
var result = []; var result = [];
@ -135,11 +139,11 @@
}); });
return result; return result;
} }
$.ajax({ $.ajax({
url: "/api/tree-data", url: "/api/tree-data",
type: "GET", type: "GET",
success: function(res) { success: function (res) {
if (res.code === 0) { if (res.code === 0) {
treeData = res.data; treeData = res.data;
allNodes = flattenTree(treeData); allNodes = flattenTree(treeData);
@ -147,89 +151,108 @@
} }
} }
}); });
// 修改prerequisiteUpdate和relatedUpdate函数 // 修改prerequisiteUpdate和relatedUpdate函数
function prerequisiteUpdate(nodeId) { function prerequisiteUpdate(nodeId) {
const node = findNodeById(treeData, nodeId); const currentNodeId = nodeId; // Define the variable from function parameter
if (!node) {
console.error('Node not found:', nodeId);
return;
}
// 创建模态框容器
const modal = document.createElement('div'); const modal = document.createElement('div');
modal.style.position = 'fixed'; modal.style.cssText = `
modal.style.top = '50%'; position: fixed;
modal.style.left = '50%'; top: 50%;
modal.style.transform = 'translate(-50%, -50%)'; left: 50%;
modal.style.width = '500px'; transform: translate(-50%, -50%);
modal.style.padding = '20px'; background-color: rgba(255, 255, 255, 0.95); // 修改背景为不透明
modal.style.backgroundColor = 'white'; padding: 20px;
modal.style.border = '1px solid #ccc'; border-radius: 5px;
modal.style.borderRadius = '5px'; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
modal.style.boxShadow = '0 0 10px rgba(0,0,0,0.2)'; z-index: 1000;
modal.style.zIndex = '1000'; width: 500px;
max-width: 90%;
modal.style.width = '600px';
modal.style.padding = '20px';
modal.style.backgroundColor = 'white';
modal.style.border = '1px solid #ccc';
modal.style.borderRadius = '5px';
modal.style.boxShadow = '0 0 10px rgba(0,0,0,0.2)';
modal.style.zIndex = '1000';
`;
// 创建标题和关闭按钮 // 创建标题和关闭按钮
const header = document.createElement('div'); const header = document.createElement('div');
header.style.display = 'flex'; header.style.display = 'flex';
header.style.justifyContent = 'space-between'; header.style.justifyContent = 'space-between';
header.style.alignItems = 'center'; header.style.alignItems = 'center';
header.style.marginBottom = '15px'; header.style.marginBottom = '15px';
const title = document.createElement('h3'); const title = document.createElement('h3');
title.textContent = '选择先修知识'; title.textContent = '选择先修知识';
title.style.margin = '0'; title.style.margin = '0';
const closeBtn = document.createElement('button'); const closeBtn = document.createElement('button');
closeBtn.innerHTML = '&times;'; closeBtn.innerHTML = '&times;';
closeBtn.style.background = 'none'; closeBtn.style.background = 'none';
closeBtn.style.border = 'none'; closeBtn.style.border = 'none';
closeBtn.style.fontSize = '20px'; closeBtn.style.fontSize = '20px';
closeBtn.style.cursor = 'pointer'; closeBtn.style.cursor = 'pointer';
closeBtn.onclick = function() { closeBtn.onclick = function () {
document.body.removeChild(modal); document.body.removeChild(modal);
}; };
header.appendChild(title); header.appendChild(title);
header.appendChild(closeBtn); header.appendChild(closeBtn);
// 创建多选下拉框 // 创建多选下拉框
const select = document.createElement('select'); // 替换select元素为checkbox列表
select.multiple = true; const container = document.createElement('div');
select.style.width = '100%'; container.style.cssText = `
select.style.height = '200px'; max-height: 300px;
select.style.margin = '10px 0'; overflow-y: auto;
select.style.padding = '5px'; margin-bottom: 10px;
`;
// 添加选项
allNodes.forEach(n => { // 为每个节点创建checkbox
if (n.id !== nodeId) { allNodes.forEach(node => {
const option = document.createElement('option'); if (node.id !== currentNodeId) {
option.value = n.id; const div = document.createElement('div');
option.text = n.title; const checkbox = document.createElement('input');
select.appendChild(option); checkbox.type = 'checkbox';
checkbox.value = node.id;
checkbox.id = `node_${node.id}`;
const label = document.createElement('label');
label.htmlFor = `node_${node.id}`;
label.textContent = node.isParent ? node.title : `【${findParentTitle(node)}】${node.title}`;
div.appendChild(checkbox);
div.appendChild(label);
container.appendChild(div);
} }
}); });
// 创建确定按钮 // Create confirm button before using it
const button = document.createElement('button'); const confirmBtn = document.createElement('button');
button.textContent = '确定'; confirmBtn.textContent = '确定';
button.style.padding = '8px 16px'; confirmBtn.style.marginTop = '10px';
button.style.backgroundColor = '#4CAF50'; confirmBtn.style.padding = '5px 15px';
button.style.color = 'white'; confirmBtn.style.backgroundColor = '#4CAF50';
button.style.border = 'none'; confirmBtn.style.color = 'white';
button.style.borderRadius = '4px'; confirmBtn.style.border = 'none';
button.style.cursor = 'pointer'; confirmBtn.style.borderRadius = '4px';
button.onclick = function() { confirmBtn.style.cursor = 'pointer';
document.body.removeChild(modal);
// 在确定按钮中获取选中的节点
confirmBtn.onclick = function () {
const selectedNodes = Array.from(container.querySelectorAll('input[type="checkbox"]:checked'))
.map(checkbox => checkbox.value);
// 这里添加保存选中节点的逻辑
modal.remove();
}; };
// 添加到模态框 // 添加到模态框
modal.appendChild(header); modal.appendChild(header);
modal.appendChild(select); modal.appendChild(container);
modal.appendChild(button); modal.appendChild(confirmBtn);
// 添加到页面 // 添加到页面
document.body.appendChild(modal); document.body.appendChild(modal);
} }

Loading…
Cancel
Save