ichieh commited on
Commit
7672900
·
1 Parent(s): f3937f7

perf: 優化前後端渲染效能

Browse files

1. 後端優化:
- 優化 /api/tables API,只返回必要的參與者資訊
- 使用 sorted() 來確保桌長永遠顯示在前面

2. 前端優化:
- 使用 DocumentFragment 來減少 DOM 操作
- 優化參與者列表的渲染邏輯

Files changed (2) hide show
  1. app.py +16 -5
  2. templates/index.html +11 -2
app.py CHANGED
@@ -504,11 +504,22 @@ def delete_participant(participant_id):
504
 
505
  @app.route('/api/tables')
506
  def get_tables():
507
- tables = Table.query.all()
508
- tables_data = []
509
- for table in tables:
510
- tables_data.append(table.to_dict())
511
- return jsonify(tables_data)
 
 
 
 
 
 
 
 
 
 
 
512
 
513
  @app.route('/api/register', methods=['POST'])
514
  def register():
 
504
 
505
  @app.route('/api/tables')
506
  def get_tables():
507
+ try:
508
+ tables = Table.query.order_by(Table.id).all()
509
+ return jsonify([{
510
+ 'id': table.id,
511
+ 'name': table.name,
512
+ 'capacity': table.capacity,
513
+ 'participants': [{
514
+ 'id': p.id,
515
+ 'nickname': p.nickname,
516
+ 'fb_link': p.fb_link,
517
+ 'introduction': p.introduction,
518
+ 'is_table_leader': p.is_table_leader
519
+ } for p in sorted(table.participants, key=lambda x: (not x.is_table_leader, x.created_at))]
520
+ } for table in tables])
521
+ except Exception as e:
522
+ return jsonify({'error': str(e)}), 500
523
 
524
  @app.route('/api/register', methods=['POST'])
525
  def register():
templates/index.html CHANGED
@@ -102,6 +102,9 @@
102
  const tablesContainer = document.getElementById('tables');
103
  tablesContainer.innerHTML = '';
104
 
 
 
 
105
  tables.forEach(table => {
106
  const tableElement = document.createElement('div');
107
  tableElement.className = 'table-container';
@@ -139,19 +142,25 @@
139
  const participantsList = document.createElement('div');
140
  participantsList.className = 'participants-list';
141
 
 
 
 
142
  table.participants.forEach(participant => {
143
  const participantDiv = document.createElement('div');
144
  participantDiv.className = `participant-item${participant.is_table_leader ? ' table-leader' : ''}`;
145
  participantDiv.textContent = participant.is_table_leader ? `桌長:${participant.nickname}` : participant.nickname;
146
  participantDiv.onclick = () => showParticipantInfo(participant);
147
- participantsList.appendChild(participantDiv);
148
  });
149
 
 
150
  circle.appendChild(tableHeader);
151
  circle.appendChild(participantsList);
152
  tableElement.appendChild(circle);
153
- tablesContainer.appendChild(tableElement);
154
  });
 
 
155
  } catch (error) {
156
  console.error('載入桌次資料失敗:', error);
157
  Swal.fire('錯誤', '載入桌次資料失敗', 'error');
 
102
  const tablesContainer = document.getElementById('tables');
103
  tablesContainer.innerHTML = '';
104
 
105
+ // 使用 DocumentFragment 來優化 DOM 操作
106
+ const fragment = document.createDocumentFragment();
107
+
108
  tables.forEach(table => {
109
  const tableElement = document.createElement('div');
110
  tableElement.className = 'table-container';
 
142
  const participantsList = document.createElement('div');
143
  participantsList.className = 'participants-list';
144
 
145
+ // 使用 DocumentFragment 來優化參與者列表的 DOM 操作
146
+ const participantsFragment = document.createDocumentFragment();
147
+
148
  table.participants.forEach(participant => {
149
  const participantDiv = document.createElement('div');
150
  participantDiv.className = `participant-item${participant.is_table_leader ? ' table-leader' : ''}`;
151
  participantDiv.textContent = participant.is_table_leader ? `桌長:${participant.nickname}` : participant.nickname;
152
  participantDiv.onclick = () => showParticipantInfo(participant);
153
+ participantsFragment.appendChild(participantDiv);
154
  });
155
 
156
+ participantsList.appendChild(participantsFragment);
157
  circle.appendChild(tableHeader);
158
  circle.appendChild(participantsList);
159
  tableElement.appendChild(circle);
160
+ fragment.appendChild(tableElement);
161
  });
162
+
163
+ tablesContainer.appendChild(fragment);
164
  } catch (error) {
165
  console.error('載入桌次資料失敗:', error);
166
  Swal.fire('錯誤', '載入桌次資料失敗', 'error');