fix: 修复编辑配置时已选内容未回显的问题
Coze-Commit-Type: user Coze-User-ID: 3722323274763196 Coze-Conversation-ID: 9894087
This commit is contained in:
34
app.py
34
app.py
@@ -344,6 +344,40 @@ def get_entities():
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@app.route('/api/entities/by_ids', methods=['POST'])
|
||||
def get_entities_by_ids():
|
||||
"""根据ID列表查询实体信息(用于编辑回显)"""
|
||||
try:
|
||||
data = request.json
|
||||
entity_type = data.get('type', 'company')
|
||||
ids = data.get('ids', [])
|
||||
|
||||
if not ids:
|
||||
return jsonify({'success': True, 'data': []})
|
||||
|
||||
placeholders = ', '.join(['%s'] * len(ids))
|
||||
|
||||
if entity_type == 'company':
|
||||
entities = execute_query(
|
||||
f'SELECT id AS company_id, company_name FROM t_company WHERE id IN ({placeholders})',
|
||||
tuple(ids)
|
||||
)
|
||||
elif entity_type == 'station':
|
||||
entities = execute_query(
|
||||
f'SELECT id AS station_id, station_name FROM t_station WHERE id IN ({placeholders})',
|
||||
tuple(ids)
|
||||
)
|
||||
else:
|
||||
entities = execute_query(
|
||||
f'SELECT id AS user_id, user_name FROM t_user WHERE id IN ({placeholders})',
|
||||
tuple(ids)
|
||||
)
|
||||
|
||||
return jsonify({'success': True, 'data': entities})
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
# ==================== 字段列表API ====================
|
||||
|
||||
@app.route('/api/fields', methods=['GET'])
|
||||
|
||||
@@ -849,7 +849,21 @@
|
||||
select.innerHTML = '<option value="">加载中...</option>';
|
||||
|
||||
try {
|
||||
// 先加载第一页数据
|
||||
// 先查询已选择的实体信息
|
||||
let selectedEntities = [];
|
||||
if (selectedValues.length > 0 && selectedValues[0] !== '') {
|
||||
const response = await fetch('/api/entities/by_ids', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ type: entityType, ids: selectedValues })
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
selectedEntities = result.data;
|
||||
}
|
||||
}
|
||||
|
||||
// 加载第一页数据
|
||||
const response = await fetch(`/api/entities?type=${entityType}&page=1&page_size=${entityPageSize}`);
|
||||
const result = await response.json();
|
||||
|
||||
@@ -858,7 +872,15 @@
|
||||
const total = result.total || 0;
|
||||
const totalPages = Math.ceil(total / entityPageSize);
|
||||
|
||||
select.innerHTML = entities.map(e => {
|
||||
// 构建已选择实体的ID集合
|
||||
const selectedIdSet = new Set(selectedValues);
|
||||
|
||||
// 合并已选择的实体和第一页数据(去重)
|
||||
const mergedEntities = [];
|
||||
const addedIds = new Set();
|
||||
|
||||
// 先添加已选择的实体(确保它们显示在顶部)
|
||||
for (const e of selectedEntities) {
|
||||
let value = '';
|
||||
let name = '';
|
||||
let typeLabel = '';
|
||||
@@ -875,9 +897,41 @@
|
||||
name = e.station_name;
|
||||
}
|
||||
|
||||
const isSelected = selectedValues.includes(String(value));
|
||||
return `<option value="${value}" data-name="${name}" ${isSelected ? 'selected' : ''}>${name} ${typeLabel}</option>`;
|
||||
}).join('');
|
||||
if (!addedIds.has(String(value))) {
|
||||
mergedEntities.push({ value, name, typeLabel, isSelected: true });
|
||||
addedIds.add(String(value));
|
||||
}
|
||||
}
|
||||
|
||||
// 再添加第一页数据(跳过已添加的)
|
||||
for (const e of entities) {
|
||||
let value = '';
|
||||
let name = '';
|
||||
let typeLabel = '';
|
||||
|
||||
if (splitType === 'company_id') {
|
||||
value = e.company_id;
|
||||
name = e.company_name;
|
||||
} else if (splitType === 'user_id') {
|
||||
value = e.user_id;
|
||||
name = e.user_name;
|
||||
typeLabel = e.order_type === 3 ? '(企业用户)' : '(普通用户)';
|
||||
} else if (splitType === 'station_id') {
|
||||
value = e.station_id;
|
||||
name = e.station_name;
|
||||
}
|
||||
|
||||
if (!addedIds.has(String(value))) {
|
||||
const isSelected = selectedIdSet.has(String(value));
|
||||
mergedEntities.push({ value, name, typeLabel, isSelected });
|
||||
addedIds.add(String(value));
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染选项
|
||||
select.innerHTML = mergedEntities.map(item =>
|
||||
`<option value="${item.value}" data-name="${item.name}" ${item.isSelected ? 'selected' : ''}>${item.name} ${item.typeLabel}</option>`
|
||||
).join('');
|
||||
|
||||
// 更新分页UI
|
||||
currentEntityType = entityType;
|
||||
|
||||
Reference in New Issue
Block a user