新增“数据编辑”
This commit is contained in:
		
							
								
								
									
										23
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								app.py
									
									
									
									
									
								
							@@ -370,6 +370,29 @@ def delete_entry(doc_id):
 | 
				
			|||||||
    else:
 | 
					    else:
 | 
				
			||||||
        return "删除失败", 500
 | 
					        return "删除失败", 500
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 批量删除数据路由
 | 
				
			||||||
 | 
					@app.route('/batch_delete', methods=['POST'])
 | 
				
			||||||
 | 
					def batch_delete():
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    批量删除数据
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    返回:
 | 
				
			||||||
 | 
					        重定向到所有数据页面或错误信息
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    doc_ids = request.form.getlist('doc_ids')
 | 
				
			||||||
 | 
					    if not doc_ids:
 | 
				
			||||||
 | 
					        return "没有选择要删除的文档", 400
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    success_count = 0
 | 
				
			||||||
 | 
					    for doc_id in doc_ids:
 | 
				
			||||||
 | 
					        if delete_by_id(doc_id):
 | 
				
			||||||
 | 
					            success_count += 1
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    if success_count == len(doc_ids):
 | 
				
			||||||
 | 
					        return redirect(url_for('show_all'))
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        return f"成功删除 {success_count} 条记录,失败 {len(doc_ids) - success_count} 条", 500
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 提供图片访问的路由
 | 
					# 提供图片访问的路由
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,50 +27,87 @@
 | 
				
			|||||||
        margin-bottom: 15px;
 | 
					        margin-bottom: 15px;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 表格容器 - 顶部边距调整 */
 | 
					    /* 卡片容器样式 */
 | 
				
			||||||
    .table-container {
 | 
					    .data-cards {
 | 
				
			||||||
        overflow-x: auto;
 | 
					        display: grid;
 | 
				
			||||||
        margin-top: 15px; /* 减少顶部间距 */
 | 
					        grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
 | 
				
			||||||
 | 
					        gap: 20px;
 | 
				
			||||||
 | 
					        margin-bottom: 20px;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 卡片样式 */
 | 
				
			||||||
 | 
					    .data-card {
 | 
				
			||||||
 | 
					        background-color: white;
 | 
				
			||||||
        border-radius: 8px;
 | 
					        border-radius: 8px;
 | 
				
			||||||
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
 | 
					        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
 | 
				
			||||||
 | 
					        padding: 20px;
 | 
				
			||||||
 | 
					        border: 1px solid #e0e0e0;
 | 
				
			||||||
 | 
					        transition: transform 0.3s, box-shadow 0.3s;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 表格样式 */
 | 
					    .data-card:hover {
 | 
				
			||||||
    table {
 | 
					        transform: translateY(-2px);
 | 
				
			||||||
        width: 100%;
 | 
					        box-shadow: 0 4px 12px rgba(0,0,0,0.15);
 | 
				
			||||||
        border-collapse: collapse;
 | 
					 | 
				
			||||||
        font-family: 'Segoe UI', Arial, sans-serif;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 表头样式 */
 | 
					    /* 卡片头部样式 */
 | 
				
			||||||
    thead {
 | 
					    .card-header {
 | 
				
			||||||
        background: linear-gradient(135deg, #3498db, #1a5276);
 | 
					        display: flex;
 | 
				
			||||||
        color: white;
 | 
					        justify-content: space-between;
 | 
				
			||||||
 | 
					        align-items: center;
 | 
				
			||||||
 | 
					        margin-bottom: 15px;
 | 
				
			||||||
 | 
					        padding-bottom: 15px;
 | 
				
			||||||
 | 
					        border-bottom: 1px solid #f0f0f0;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    th {
 | 
					    .card-header h3 {
 | 
				
			||||||
        padding: 16px 12px;
 | 
					        margin: 0;
 | 
				
			||||||
        text-align: left;
 | 
					        color: #333;
 | 
				
			||||||
 | 
					        font-size: 18px;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    .card-actions {
 | 
				
			||||||
 | 
					        display: flex;
 | 
				
			||||||
 | 
					        gap: 8px;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 卡片内容样式 */
 | 
				
			||||||
 | 
					    .card-content {
 | 
				
			||||||
 | 
					        margin-bottom: 15px;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    .field-item {
 | 
				
			||||||
 | 
					        display: flex;
 | 
				
			||||||
 | 
					        margin-bottom: 10px;
 | 
				
			||||||
 | 
					        line-height: 1.5;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    .field-key {
 | 
				
			||||||
        font-weight: 600;
 | 
					        font-weight: 600;
 | 
				
			||||||
 | 
					        color: #333;
 | 
				
			||||||
 | 
					        min-width: 120px;
 | 
				
			||||||
 | 
					        margin-right: 10px;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 表格行样式 */
 | 
					    .field-value {
 | 
				
			||||||
    tbody tr {
 | 
					        color: #666;
 | 
				
			||||||
        border-bottom: 1px solid #eef1f5;
 | 
					        flex: 1;
 | 
				
			||||||
        transition: background-color 0.3s;
 | 
					        word-break: break-word;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    tbody tr:nth-child(even) {
 | 
					    /* 卡片图片样式 */
 | 
				
			||||||
        background-color: #f8fafc;
 | 
					    .card-image {
 | 
				
			||||||
 | 
					        text-align: center;
 | 
				
			||||||
 | 
					        margin-top: 15px;
 | 
				
			||||||
 | 
					        padding-top: 15px;
 | 
				
			||||||
 | 
					        border-top: 1px solid #f0f0f0;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    tbody tr:hover {
 | 
					    .card-image img {
 | 
				
			||||||
        background-color: #e3f2fd;
 | 
					        max-width: 100%;
 | 
				
			||||||
    }
 | 
					        max-height: 200px;
 | 
				
			||||||
 | 
					        border-radius: 4px;
 | 
				
			||||||
    td {
 | 
					        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
 | 
				
			||||||
        padding: 14px 12px;
 | 
					 | 
				
			||||||
        color: #4a5568;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 操作按钮样式 */
 | 
					    /* 操作按钮样式 */
 | 
				
			||||||
@@ -128,6 +165,33 @@
 | 
				
			|||||||
        padding: 40px 0;
 | 
					        padding: 40px 0;
 | 
				
			||||||
        color: #a0aec0;
 | 
					        color: #a0aec0;
 | 
				
			||||||
        font-style: italic;
 | 
					        font-style: italic;
 | 
				
			||||||
 | 
					        grid-column: 1 / -1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 响应式设计 */
 | 
				
			||||||
 | 
					    @media (max-width: 768px) {
 | 
				
			||||||
 | 
					        .data-cards {
 | 
				
			||||||
 | 
					            grid-template-columns: 1fr;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        .card-header {
 | 
				
			||||||
 | 
					            flex-direction: column;
 | 
				
			||||||
 | 
					            align-items: flex-start;
 | 
				
			||||||
 | 
					            gap: 10px;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        .card-actions {
 | 
				
			||||||
 | 
					            align-self: flex-end;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        .field-item {
 | 
				
			||||||
 | 
					            flex-direction: column;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        .field-key {
 | 
				
			||||||
 | 
					            min-width: auto;
 | 
				
			||||||
 | 
					            margin-bottom: 5px;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
</style>
 | 
					</style>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -135,44 +199,155 @@
 | 
				
			|||||||
    <h2>所有已录入的奖项信息</h2>
 | 
					    <h2>所有已录入的奖项信息</h2>
 | 
				
			||||||
    <p>在此页面可以查看所有已录入的成果信息,并进行编辑和删除操作</p>
 | 
					    <p>在此页面可以查看所有已录入的成果信息,并进行编辑和删除操作</p>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <div class="table-container">
 | 
					    <!-- 批量操作区域 -->
 | 
				
			||||||
        <table>
 | 
					    <div class="batch-operations" style="margin-bottom: 20px; padding: 15px; background-color: #f8f9fa; border-radius: 8px; border: 1px solid #e0e0e0;">
 | 
				
			||||||
            <thead>
 | 
					        <div style="display: flex; align-items: center; gap: 15px;">
 | 
				
			||||||
                <tr>
 | 
					            <div style="display: flex; align-items: center; gap: 8px;">
 | 
				
			||||||
                    <th>比赛/论文名称</th>
 | 
					                <input type="checkbox" id="select-all" onchange="toggleSelectAll(this.checked)">
 | 
				
			||||||
                    <th>项目名称</th>
 | 
					                <label for="select-all" style="font-weight: 600; color: #333;">全选</label>
 | 
				
			||||||
                    <th>学生</th>
 | 
					            </div>
 | 
				
			||||||
                    <th>指导老师</th>
 | 
					            <button type="button" class="batch-delete-btn" onclick="batchDelete()" style="padding: 8px 16px; background-color: #dc3545; color: white; border: none; border-radius: 4px; cursor: pointer; font-weight: 500; transition: background-color 0.3s;">
 | 
				
			||||||
                    <th style="text-align: center;">操作</th>
 | 
					                批量删除选中项
 | 
				
			||||||
                </tr>
 | 
					            </button>
 | 
				
			||||||
            </thead>
 | 
					            <span id="selected-count" style="color: #666; font-size: 14px;">已选择 0 项</span>
 | 
				
			||||||
            <tbody>
 | 
					        </div>
 | 
				
			||||||
                {% if data %}
 | 
					    </div>
 | 
				
			||||||
                    {% for item in data %}
 | 
					
 | 
				
			||||||
                        <tr>
 | 
					    <div class="data-cards">
 | 
				
			||||||
                            <td>{{ item.id or '无' }}</td>
 | 
					        {% if data %}
 | 
				
			||||||
                            <td>{{ item.name or '无' }}</td>
 | 
					            {% for item in data %}
 | 
				
			||||||
                            <td>{% if item.students is string %}{{ item.students or '无' }}{% else %}{{ item.students|join(', ') if item.students else '无' }}{% endif %}</td>
 | 
					                <div class="data-card">
 | 
				
			||||||
                            <td>{% if item.teacher is string %}{{ item.teacher or '无' }}{% else %}{{ item.teacher|join(', ') if item.teacher else '无' }}{% endif %}</td>
 | 
					                    <div class="card-header">
 | 
				
			||||||
                            <td style="text-align: center;">
 | 
					                        <div style="display: flex; align-items: center; gap: 15px;">
 | 
				
			||||||
                                <div style="display: flex; justify-content: center; gap: 8px;">
 | 
					                            <input type="checkbox" class="doc-checkbox" value="{{ item._id }}" onchange="updateSelectedCount()">
 | 
				
			||||||
                                    <a href="{{ url_for('edit_entry', doc_id=item._id) }}" class="action-button edit-btn">编辑</a>
 | 
					                            <h3>记录 {{ loop.index }}</h3>
 | 
				
			||||||
                                    <form action="{{ url_for('delete_entry', doc_id=item._id) }}" method="POST" onsubmit="return confirm('确定要删除这条记录吗?')" style="margin: 0;">
 | 
					                        </div>
 | 
				
			||||||
                                        <button type="submit" class="action-button delete-btn">删除</button>
 | 
					                        <div class="card-actions">
 | 
				
			||||||
                                    </form>
 | 
					                            <a href="{{ url_for('edit_entry', doc_id=item._id) }}" class="action-button edit-btn">编辑</a>
 | 
				
			||||||
                                </div>
 | 
					                        </div>
 | 
				
			||||||
                            </td>
 | 
					                    </div>
 | 
				
			||||||
                        </tr>
 | 
					                    
 | 
				
			||||||
                    {% endfor %}
 | 
					                    <div class="card-content">
 | 
				
			||||||
                {% else %}
 | 
					                        {% if item.data %}
 | 
				
			||||||
                    <tr>
 | 
					                            {# 从原始数据中解析字段 #}
 | 
				
			||||||
                        <td colspan="5" class="no-data">暂无数据</td>
 | 
					                            {% set data_string = item.data %}
 | 
				
			||||||
                    </tr>
 | 
					                            {% set pairs = data_string.split('|###|') %}
 | 
				
			||||||
                {% endif %}
 | 
					                            
 | 
				
			||||||
            </tbody>
 | 
					                            {% for pair in pairs %}
 | 
				
			||||||
        </table>
 | 
					                                {% if ':' in pair %}
 | 
				
			||||||
 | 
					                                    {% set key_value = pair.split(':', 1) %}
 | 
				
			||||||
 | 
					                                    {% set field_key = key_value[0].strip() %}
 | 
				
			||||||
 | 
					                                    {% set field_value = key_value[1].strip() %}
 | 
				
			||||||
 | 
					                                    
 | 
				
			||||||
 | 
					                                    {# 处理列表格式 [item1|##|item2] #}
 | 
				
			||||||
 | 
					                                    {% if field_value.startswith('[') and field_value.endswith(']') %}
 | 
				
			||||||
 | 
					                                        {% set list_content = field_value[1:-1] %}
 | 
				
			||||||
 | 
					                                        {% set field_value = list_content.split('|##|')|join(', ') %}
 | 
				
			||||||
 | 
					                                    {% endif %}
 | 
				
			||||||
 | 
					                                    
 | 
				
			||||||
 | 
					                                    <div class="field-item">
 | 
				
			||||||
 | 
					                                        <span class="field-key">{{ field_key }}:</span>
 | 
				
			||||||
 | 
					                                        <span class="field-value">{{ field_value or '无' }}</span>
 | 
				
			||||||
 | 
					                                    </div>
 | 
				
			||||||
 | 
					                                {% endif %}
 | 
				
			||||||
 | 
					                            {% endfor %}
 | 
				
			||||||
 | 
					                        {% else %}
 | 
				
			||||||
 | 
					                            {# 如果没有data字段,显示解析后的字段 #}
 | 
				
			||||||
 | 
					                            {% for key, value in item.items() %}
 | 
				
			||||||
 | 
					                                {% if key not in ['_id', 'image'] %}
 | 
				
			||||||
 | 
					                                    <div class="field-item">
 | 
				
			||||||
 | 
					                                        <span class="field-key">{{ key }}:</span>
 | 
				
			||||||
 | 
					                                        <span class="field-value">
 | 
				
			||||||
 | 
					                                            {% if value is sequence and value is not string %}
 | 
				
			||||||
 | 
					                                                {{ value|join(', ') if value else '无' }}
 | 
				
			||||||
 | 
					                                            {% else %}
 | 
				
			||||||
 | 
					                                                {{ value or '无' }}
 | 
				
			||||||
 | 
					                                            {% endif %}
 | 
				
			||||||
 | 
					                                        </span>
 | 
				
			||||||
 | 
					                                    </div>
 | 
				
			||||||
 | 
					                                {% endif %}
 | 
				
			||||||
 | 
					                            {% endfor %}
 | 
				
			||||||
 | 
					                        {% endif %}
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                </div>
 | 
				
			||||||
 | 
					            {% endfor %}
 | 
				
			||||||
 | 
					        {% else %}
 | 
				
			||||||
 | 
					            <div class="no-data">暂无数据</div>
 | 
				
			||||||
 | 
					        {% endif %}
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <a href="{{ url_for('index') }}" class="back-btn">返回首页</a>
 | 
					    <a href="{{ url_for('index') }}" class="back-btn">返回首页</a>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<script>
 | 
				
			||||||
 | 
					// 全选/取消全选功能
 | 
				
			||||||
 | 
					function toggleSelectAll(checked) {
 | 
				
			||||||
 | 
					    const checkboxes = document.querySelectorAll('.doc-checkbox');
 | 
				
			||||||
 | 
					    checkboxes.forEach(checkbox => {
 | 
				
			||||||
 | 
					        checkbox.checked = checked;
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    updateSelectedCount();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 更新选择计数
 | 
				
			||||||
 | 
					function updateSelectedCount() {
 | 
				
			||||||
 | 
					    const checkboxes = document.querySelectorAll('.doc-checkbox');
 | 
				
			||||||
 | 
					    const selectedCount = Array.from(checkboxes).filter(cb => cb.checked).length;
 | 
				
			||||||
 | 
					    document.getElementById('selected-count').textContent = `已选择 ${selectedCount} 项`;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // 更新全选复选框状态
 | 
				
			||||||
 | 
					    const selectAllCheckbox = document.getElementById('select-all');
 | 
				
			||||||
 | 
					    if (selectedCount === 0) {
 | 
				
			||||||
 | 
					        selectAllCheckbox.checked = false;
 | 
				
			||||||
 | 
					        selectAllCheckbox.indeterminate = false;
 | 
				
			||||||
 | 
					    } else if (selectedCount === checkboxes.length) {
 | 
				
			||||||
 | 
					        selectAllCheckbox.checked = true;
 | 
				
			||||||
 | 
					        selectAllCheckbox.indeterminate = false;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        selectAllCheckbox.checked = false;
 | 
				
			||||||
 | 
					        selectAllCheckbox.indeterminate = true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 批量删除功能
 | 
				
			||||||
 | 
					function batchDelete() {
 | 
				
			||||||
 | 
					    const checkboxes = document.querySelectorAll('.doc-checkbox:checked');
 | 
				
			||||||
 | 
					    if (checkboxes.length === 0) {
 | 
				
			||||||
 | 
					        alert('请至少选择一条记录进行删除');
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    const confirmMessage = `确定要删除选中的 ${checkboxes.length} 条记录吗?此操作不可撤销。`;
 | 
				
			||||||
 | 
					    if (!confirm(confirmMessage)) {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // 收集选中的文档ID
 | 
				
			||||||
 | 
					    const docIds = Array.from(checkboxes).map(cb => cb.value);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // 创建表单并提交
 | 
				
			||||||
 | 
					    const form = document.createElement('form');
 | 
				
			||||||
 | 
					    form.method = 'POST';
 | 
				
			||||||
 | 
					    form.action = '/batch_delete';
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    docIds.forEach(docId => {
 | 
				
			||||||
 | 
					        const input = document.createElement('input');
 | 
				
			||||||
 | 
					        input.type = 'hidden';
 | 
				
			||||||
 | 
					        input.name = 'doc_ids';
 | 
				
			||||||
 | 
					        input.value = docId;
 | 
				
			||||||
 | 
					        form.appendChild(input);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    document.body.appendChild(form);
 | 
				
			||||||
 | 
					    form.submit();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 页面加载时初始化
 | 
				
			||||||
 | 
					document.addEventListener('DOMContentLoaded', function() {
 | 
				
			||||||
 | 
					    updateSelectedCount();
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
{% endblock %}
 | 
					{% endblock %}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user