去外層留內層終極版(精準切割)
Browse files針對「紅框」所在的層級(外層):強制截斷 (overflow: hidden),不准它變寬,不准它捲動。
針對「綠框」所在的層級(內層):強制開啟 (overflow-x: auto),只有這裡可以動。
app.py
CHANGED
|
@@ -131,89 +131,115 @@ def check_login(user, password):
|
|
| 131 |
error_msg: "<span style='color: red'>❌ 帳號或密碼錯誤</span>"
|
| 132 |
}
|
| 133 |
|
| 134 |
-
# --- 🟢 [
|
| 135 |
-
|
| 136 |
-
# 1. 找到表格本體 (table)
|
| 137 |
-
# 2. 找到包著表格的「那一層」div (parent),強制它顯示捲軸
|
| 138 |
-
# 3. 找到更外層的所有 div (grandparents),強制它們「不准」顯示捲軸
|
| 139 |
-
force_scroll_js = """
|
| 140 |
function() {
|
| 141 |
-
const
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
if (!comp) return;
|
| 145 |
|
| 146 |
-
//
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
//
|
| 156 |
-
const
|
| 157 |
-
if (
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
}
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
}
|
| 172 |
-
|
| 173 |
-
// 確保最外層也沒有捲軸
|
| 174 |
-
if(comp) comp.style.overflowX = 'hidden';
|
| 175 |
};
|
| 176 |
|
| 177 |
-
//
|
| 178 |
-
|
| 179 |
-
|
|
|
|
| 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 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
/*
|
| 207 |
-
#booking_table th:nth-child(1), #booking_table td:nth-child(1) { width: 60px !important;
|
| 208 |
-
#booking_table th:nth-child(2), #booking_table td:nth-child(2) { width: 170px !important;
|
| 209 |
-
#booking_table th:nth-child(3), #booking_table td:nth-child(3) { width: 80px !important;
|
| 210 |
-
#booking_table th:nth-child(4), #booking_table td:nth-child(4) { width: 120px !important;
|
| 211 |
-
#booking_table th:nth-child(5), #booking_table td:nth-child(5) { width: 120px !important;
|
| 212 |
-
#booking_table th:nth-child(6), #booking_table td:nth-child(6) { width: 250px !important;
|
| 213 |
-
#booking_table th:nth-child(7), #booking_table td:nth-child(7) { width: 50px !important;
|
| 214 |
-
#booking_table th:nth-child(8), #booking_table td:nth-child(8) { width: 180px !important;
|
| 215 |
-
#booking_table th:nth-child(9), #booking_table td:nth-child(9) { width: 120px !important;
|
| 216 |
-
#booking_table th:nth-child(10), #booking_table td:nth-child(10) { 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 |
-
# 🔥🔥🔥
|
| 255 |
-
demo.load(None, 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()
|