DeepLearning101 commited on
Commit
5d4fbce
·
verified ·
1 Parent(s): 212391b

去外層留內層終極版(精準切割)

Browse files

針對「紅框」所在的層級(外層):強制截斷 (overflow: hidden),不准它變寬,不准它捲動。
針對「綠框」所在的層級(內層):強制開啟 (overflow-x: auto),只有這裡可以動。

Files changed (1) hide show
  1. app.py +86 -60
app.py CHANGED
@@ -131,89 +131,115 @@ def check_login(user, password):
131
  error_msg: "<span style='color: red'>❌ 帳號或密碼錯誤</span>"
132
  }
133
 
134
- # --- 🟢 [JavaScript] 精準控制捲軸 ---
135
- # 這段 JS 負責:
136
- # 1. 找到表格本體 (table)
137
- # 2. 找到包著表格的「那一層」div (parent),強制它顯示捲軸
138
- # 3. 找到更外層的所有 div (grandparents),強制它們「不准」顯示捲軸
139
- force_scroll_js = """
140
  function() {
141
- const fixScroll = () => {
142
- // 1. 找到表格元件
143
- const comp = document.querySelector('#booking_table');
144
- if (!comp) return;
145
 
146
- // 2. 找到裡面的 table
147
- const table = comp.querySelector('table');
148
- if (!table) return;
149
-
150
- // 強制表格寬度 (確保內容撐開)
151
- table.style.width = '1500px';
152
- table.style.minWidth = '1500px';
153
- table.style.tableLayout = 'fixed';
 
 
 
 
 
 
 
154
 
155
- // 3. 找到直接包著 table 的那一層 div (這是我們要保留捲軸的地方)
156
- const innerWrapper = table.parentElement;
157
- if (innerWrapper) {
158
- innerWrapper.style.overflowX = 'auto'; // 開啟捲軸
159
- innerWrapper.style.width = '100%';
160
- innerWrapper.style.maxWidth = '100vw';
161
- innerWrapper.style.display = 'block';
162
- }
163
 
164
- // 4. 往上找所有祖先容器,全部殺死捲軸
165
- // 這是解決「錯誤捲軸」或「雙重捲軸」的關鍵
166
- let parent = innerWrapper.parentElement;
167
- while (parent && parent.id !== 'booking_table') {
168
- parent.style.overflowX = 'hidden'; // 隱藏外層捲軸
169
- parent.style.width = '100%';
170
- parent = parent.parentElement;
171
  }
172
-
173
- // 確保最外層也沒有捲軸
174
- if(comp) comp.style.overflowX = 'hidden';
175
  };
176
 
177
- // 執行邏輯
178
- fixScroll();
179
- setInterval(fixScroll, 500); // 防止 Gradio 重繪後失效
 
180
  }
181
  """
182
 
183
  # --- 🔥 [CSS] 定寬 + 換行 + 隱藏全域捲軸 ---
184
  custom_css = """
185
- /* 1. 全域設定:防止網頁被撐開 */
186
  body, .gradio-container {
187
- overflow-x: hidden !important;
188
  max-width: 100vw !important;
189
  }
190
 
191
- /* 2. 表格內容設定 */
192
- /* 這裡只負責欄位的樣式,捲軸交給 JS 控制 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  #booking_table th, #booking_table td {
194
- white-space: normal !important; /* 允許換行 */
195
- word-break: break-all !important; /* 強制換行 */
196
  overflow-wrap: break-word !important;
197
-
198
  vertical-align: top !important;
199
- box-sizing: border-box !important;
200
  padding: 8px 5px !important;
201
  border: 1px solid #444 !important;
202
  font-size: 13px !important;
203
  line-height: 1.4 !important;
204
  }
205
 
206
- /* 3. 🔥 個別欄位寬度 (同時設定 width, min-width, max-width 確保不動如山) */
207
- #booking_table th:nth-child(1), #booking_table td:nth-child(1) { width: 60px !important; min-width: 60px !important; max-width: 60px !important; }
208
- #booking_table th:nth-child(2), #booking_table td:nth-child(2) { width: 170px !important; min-width: 170px !important; max-width: 170px !important; }
209
- #booking_table th:nth-child(3), #booking_table td:nth-child(3) { width: 80px !important; min-width: 80px !important; max-width: 80px !important; }
210
- #booking_table th:nth-child(4), #booking_table td:nth-child(4) { width: 120px !important; min-width: 120px !important; max-width: 120px !important; }
211
- #booking_table th:nth-child(5), #booking_table td:nth-child(5) { width: 120px !important; min-width: 120px !important; max-width: 120px !important; }
212
- #booking_table th:nth-child(6), #booking_table td:nth-child(6) { width: 250px !important; min-width: 250px !important; max-width: 250px !important; }
213
- #booking_table th:nth-child(7), #booking_table td:nth-child(7) { width: 50px !important; min-width: 50px !important; max-width: 50px !important; }
214
- #booking_table th:nth-child(8), #booking_table td:nth-child(8) { width: 180px !important; min-width: 180px !important; max-width: 180px !important; }
215
- #booking_table th:nth-child(9), #booking_table td:nth-child(9) { width: 120px !important; min-width: 120px !important; max-width: 120px !important; }
216
- #booking_table th:nth-child(10), #booking_table td:nth-child(10) { width: 320px !important; min-width: 320px !important; max-width: 320px !important; }
217
  """
218
 
219
  # --- 介面開始 ---
@@ -251,8 +277,8 @@ with gr.Blocks(title="Admin", css=custom_css) as demo:
251
  outputs=[login_row, admin_row, error_msg]
252
  )
253
 
254
- # 🔥🔥🔥 啟動 JS 修復程式 🔥🔥🔥
255
- demo.load(None, js=force_scroll_js)
256
 
257
  if __name__ == "__main__":
258
  demo.launch()
 
131
  error_msg: "<span style='color: red'>❌ 帳號或密碼錯誤</span>"
132
  }
133
 
134
+ # --- 🟢 [JS 核心邏輯]:層層檢查,殺掉紅框,保留綠框 ---
135
+ fix_scroll_js = """
 
 
 
 
136
  function() {
137
+ const applyFix = () => {
138
+ const root = document.querySelector('#booking_table');
139
+ if (!root) return;
 
140
 
141
+ // 1. 殺掉外層捲軸 (Red Scrollbar)
142
+ // 找到所有包在表格外面的 div,只要不是直接包著 table 的,通通 hidden
143
+ const allDivs = root.querySelectorAll('div');
144
+ allDivs.forEach(div => {
145
+ // 如果這個 div 裡面還有一個 table-wrap,或者它本身就是外層包裝
146
+ // 而且它不是 table 的直接父層
147
+ const hasInnerTable = div.querySelector('table');
148
+ if (hasInnerTable && window.getComputedStyle(div).overflowX === 'auto') {
149
+ div.style.overflowX = 'hidden';
150
+ div.style.maxWidth = '100%';
151
+ }
152
+ });
153
+
154
+ // 確保最外層也是 hidden
155
+ root.style.overflowX = 'hidden';
156
 
157
+ // 2. 保留內層捲軸 (Green Scrollbar)
158
+ const table = root.querySelector('table');
159
+ if (table) {
160
+ // 強制設定表格寬度,確保內容撐開
161
+ table.style.width = '1500px';
162
+ table.style.minWidth = '1500px';
163
+ table.style.tableLayout = 'fixed';
 
164
 
165
+ // 找到直接父層 (Green Scrollbar 所在位置)
166
+ const parent = table.parentElement;
167
+ if (parent) {
168
+ parent.style.overflowX = 'auto'; // 開啟
169
+ parent.style.maxWidth = '100vw'; // 限制寬度
170
+ parent.style.display = 'block';
171
+ }
172
  }
 
 
 
173
  };
174
 
175
+ // 啟動時執行
176
+ applyFix();
177
+ // 循環執行以對抗 Gradio 的動態渲染
178
+ setInterval(applyFix, 500);
179
  }
180
  """
181
 
182
  # --- 🔥 [CSS] 定寬 + 換行 + 隱藏全域捲軸 ---
183
  custom_css = """
184
+ /* 1. 全域與元件外層:殺死紅框捲軸 */
185
  body, .gradio-container {
186
+ overflow-x: hidden !important; /* 殺死瀏覽器捲軸 */
187
  max-width: 100vw !important;
188
  }
189
 
190
+ #booking_table {
191
+ overflow: hidden !important; /* 殺死元件捲軸 */
192
+ max-width: 100% !important;
193
+ border: none !important;
194
+ padding: 0 !important;
195
+ }
196
+
197
+ /* 2. 中間層:確保沒有任何中間人偷偷加捲軸 */
198
+ #booking_table .wrap,
199
+ #booking_table .svelte-12cmxck {
200
+ overflow-x: hidden !important;
201
+ max-width: 100% !important;
202
+ }
203
+
204
+ /* 3. 內層 (綠框位置):唯一允許捲動的地方 */
205
+ #booking_table .table-wrap,
206
+ #booking_table tbody {
207
+ overflow-x: auto !important;
208
+ overflow-y: hidden !important;
209
+ max-width: 100vw !important; /* 確保不超過螢幕 */
210
+ border: 1px solid #444 !important;
211
+ }
212
+
213
+ /* 4. 表格本體:撐開它! */
214
+ #booking_table table {
215
+ table-layout: fixed !important;
216
+ width: 1500px !important; /* 總寬度 */
217
+ min-width: 1500px !important;
218
+ }
219
+
220
+ /* 5. 欄位內容:自動換行 */
221
  #booking_table th, #booking_table td {
222
+ white-space: normal !important;
223
+ word-break: break-all !important;
224
  overflow-wrap: break-word !important;
 
225
  vertical-align: top !important;
 
226
  padding: 8px 5px !important;
227
  border: 1px solid #444 !important;
228
  font-size: 13px !important;
229
  line-height: 1.4 !important;
230
  }
231
 
232
+ /* 6. 個別欄位寬度 */
233
+ #booking_table th:nth-child(1), #booking_table td:nth-child(1) { width: 60px !important; }
234
+ #booking_table th:nth-child(2), #booking_table td:nth-child(2) { width: 170px !important; }
235
+ #booking_table th:nth-child(3), #booking_table td:nth-child(3) { width: 80px !important; }
236
+ #booking_table th:nth-child(4), #booking_table td:nth-child(4) { width: 120px !important; }
237
+ #booking_table th:nth-child(5), #booking_table td:nth-child(5) { width: 120px !important; }
238
+ #booking_table th:nth-child(6), #booking_table td:nth-child(6) { width: 250px !important; }
239
+ #booking_table th:nth-child(7), #booking_table td:nth-child(7) { width: 50px !important; }
240
+ #booking_table th:nth-child(8), #booking_table td:nth-child(8) { width: 180px !important; }
241
+ #booking_table th:nth-child(9), #booking_table td:nth-child(9) { width: 120px !important; }
242
+ #booking_table th:nth-child(10), #booking_table td:nth-child(10) { width: 320px !important; }
243
  """
244
 
245
  # --- 介面開始 ---
 
277
  outputs=[login_row, admin_row, error_msg]
278
  )
279
 
280
+ # 🔥🔥🔥 執行 JS 強制修正 🔥🔥🔥
281
+ demo.load(None, js=fix_scroll_js)
282
 
283
  if __name__ == "__main__":
284
  demo.launch()