互動視窗、模態,就是點按鈕時會彈出或滑出的一個小視窗。
下面先來看一個後台頁面。
這個頁面中,文章內容那個欄位裡面的文章內容
按鈕,可以啟動互動視窗(modal)。
下面來看一下按他的效果。
這個是按按鈕啟動的互動視窗, 他是來自bootstrap5的code。 不過這個code如果只是單純貼上,然後只加入你要用的程式碼,會出現問題。 你會發現他只讀取到第一筆資料的內容, 不管你點哪一篇文章的按鈕,出現的都是第一筆資料的內容。
(點開看文章內容說了什麼)
@foreach ($newsuse as $item)
<!-- 這個按鈕用來啟動互動視窗(modal) -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
文章內容
</button>
<!-- 被啟動的互動視窗(Modal) -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<!-- 下面是php的變數,可以把文章的內容帶進來網頁 -->
{{$item->content}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">關閉</button>
<button type="button" class="btn btn-primary">儲存(這裡沒用這個功能)</button>
</div>
</div>
</div>
</div>
@endforeach
互動視窗(modal)的啟動機制是什麼呢?
它是由上面程式碼的第3行那個按鈕,裡面有一個data-bs-target="#exampleModal"
對應到程式碼第8行的div,它的id="exampleModal"
,才能實現開啟。
如果我把按鈕的data-bs-target="#exampleModal"
裡面的#exampleModal
改成#aaaa
,
變成data-bs-target="#aaaa"
,因為沒有對應那個div的id,互動視窗(modal)就開不起來了。
為了實現每篇文章的按鈕,點了都可以看到內篇文章的內文,調整後的程式碼如下。
@foreach ($newsuse as $item)
<!-- 這個按鈕用來啟動互動視窗(modal) -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#xx{{$item->id}}">
文章內容
</button>
<!-- 被啟動的互動視窗(Modal) -->
<div class="modal fade" id="xx{{$item->id}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<!-- 下面是php的變數,可以把文章的內容帶進來網頁 -->
{{$item->content}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">關閉</button>
<button type="button" class="btn btn-primary">儲存(這裡沒用這個功能)</button>
</div>
</div>
</div>
</div>
@endforeach
看得出來改了哪裡嗎?
按鈕裡面的data-bs-target="#xx{{$item->id}}"
,
以及互動視窗中對應的div裡id="xx{{$item->id}}"
,
然後要注意,id的前面不能是數字,所以才特地加xx的,
這個xx隨便你打,只要兩邊都一樣就可以,你也可以打ooo或rrrr。
總之,這樣就成功了。
laravel
laravel.8
bootstrap
modal
後台
互動視窗
這是當頁的網址 當要連結的a標籤連結為 這個開頭沒有斜線"/" 則網址扣除當分頁累加($item與新連結視為同階分支) 如果有斜線"/" 則導向該網址
Mar 7, 2022在前端送資料到後端的時候 很多格式是有可能被串改的 因此在使用獲得的資料前 要先做驗證的動作 Laravel就有提供驗證的功能 https://laravel.com/docs/8.x/validation 直接舉例 將接收到的$request直接送入驗證
Mar 3, 2022Laravel 版本7~8.X 00.專案基本知識與指令 01.在哪一頁登入,就回到哪一頁 02.好用的Blade語法實例1 用@auth/@endauth實現部分頁面權限 03.好用的Blade語法實例1 居然遇到報錯 04.好用的Blade語法實例2 只有發布者才能編輯與刪除他發布的文章
Feb 17, 2022為了讓共筆者可以更好的新增及查找問題,以這個目錄為主要依據。 舉凡是: :::success 1.單純想分享自己搞懂的新東西 2.覺得這個技巧需要會,但就是不會 3.想學會某個外掛/套件 4.不懂這個程式碼在寫什麼
Feb 9, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up