使用laravel 做一个展示大屏项目,后台的数据变化时,如何让前端的页面自动刷新.我想到的办法是使用laravel livewire组件. 具体代码如下:
http/livewire/show.php(控制器代码)
use App\Zdrw; use App\Djgz; use App\Shgg; use App\Sjjb; class Show extends Component { public $zdrws; public $djgzs; public $shggs; public $sjjbs; public function mount() { $this->zdrws = Zdrw::all(); $this->djgzs = Djgz::all(); $this->shggs = Shgg::all(); $this->sjjbs = Sjjb::all(); } public function render() { return view('livewire.show'); } }
前端页面
views/home.blade.php
//这里按照文档添加了wie:poll <div wire:poll> <livewire:show /> </div>
views/livewire/show.blade.php
<table id="table" data-toggle="table" data-pagination="false" data-page-size="8" data-search="false"> <thead> <tr> <th data-sortable="true">ID</th> <th data-sortable="true">任务名称</th> <th data-sortable="true">牵头科室</th> <th data-sortable="true">责任人</th> <th data-sortable="true">完成时限</th> <th data-sortable="true">工作进度</th> </tr> </thead> <tbody wire:model="zdrws"> @foreach($zdrws as $zdrw) <tr> <td>{{$zdrw->id}}</td> <td>{{$zdrw->name}}</td> <td>{{$zdrw->department}}</td> <td>{{$zdrw->person}}</td> <td>{{$zdrw->finish}}</td> <td>{{$zdrw->status}}</td> </tr> @endforeach </tbody> </table>
后台数据变动时,删除\修改\添加, 前端数据都不自动变化,我哪里出问题了,应该怎么解决?请各位高手赐教,非常感谢
一般大屏数据都会是异步加载数据,可以配置自动刷新间隔,比如5秒?
你这种就是在页面加载(mount)时,去绘制(render)一次页面。
livewire
没用过,大概查了一下,好像只是可以自动生成CRUD对应的界面?
你应该需要改造一下这个页面。
做数据实时刷新一般两种思路:
- 前端定时拉取:①短连接 ②长轮询
- 后端主动推送:①WebSocket ②HTTP2
