This time I will bring you a detailed explanation of the use cases of pjax in the laravel framework. What are theprecautionsfor using pjax in the laravel framework. The following is a practical case, let's take a look.
Regarding what pjax is, readers are asked to Baidu.
Preparation:
1. Download the jquery.pjax.js file and click download
2. Download NProgress
3. Put the required files into the project. and referenced in the layout file. (Under the framework public directory)
Start:
Here I am using the adminLTE backend template. For specific usage, please refer to Using AdminLTE in Laravel5.*
Installing pjaxcomposer Package(laravel middleware):
$ composer require spatie/laravel-pjax
Add code to the kernel.php file:
// app/Http/Kernel.php ... protected $middleware = [ ... \Spatie\Pjax\Middleware\FilterIfPjax::class, ];
Configure pjax to complete page interaction: (ps: The author uses pjax to load pages throughout the site, Therefore, pjax is configured in the global js file, and readers can configure it separately as needed)
$.pjax.defaults.timeout = 5000; $.pjax.defaults.maxCacheLength = 0; //配置可点击的标签使用pjax $(document).pjax('a:not(a[target="_blank"])', { container: '#pjax-container'//配置pjax刷新容器 }); NProgress.configure({parent: '#pjax-container'}); //超时执行函数 $(document).on('pjax:timeout', function (event) { event.preventDefault(); });
At this point, laravel combined with pjax has been completed.
Attachment:
The author’s layout (layout.blade.php):
AdminLTE @include('admin::partials.header') @include('admin::partials.sidebar')
@include('admin::partials.control') @include('admin::partials.footer')
@yield('content')
Global js file (admin-base.js):
toastr.options = { closeButton: true, progressBar: true, showMethod: 'slideDown', positionClass: 'toast-top-right', timeOut: 4000 }; $.pjax.defaults.timeout = 5000; $.pjax.defaults.maxCacheLength = 0; $(document).pjax('a:not(a[target="_blank"])', { container: '#pjax-container' }); NProgress.configure({parent: '#pjax-container'}); $(document).on('pjax:timeout', function (event) { event.preventDefault(); }); $(document).on('submit', 'form[pjax-container]', function (event) { $.pjax.submit(event, '#pjax-container') }); $(document).on("pjax:popstate", function () { $(document).one("pjax:end", function (event) { $(event.target).find("script[data-exec-on-popstate]").each(function () { $.globalEval(this.text || this.textContent || this.innerHTML || ''); }); }); }); $(document).on('pjax:send', function (xhr) { if (xhr.relatedTarget && xhr.relatedTarget.tagName && xhr.relatedTarget.tagName.toLowerCase() === 'form') { $submit_btn = $('form[pjax-container] :submit'); if ($submit_btn) { $submit_btn.button('loading') } } NProgress.start(); }); $(document).on('pjax:complete', function (xhr) { if (xhr.relatedTarget && xhr.relatedTarget.tagName && xhr.relatedTarget.tagName.toLowerCase() === 'form') { $submit_btn = $('form[pjax-container] :submit'); if ($submit_btn) { $submit_btn.button('reset') } } NProgress.done(); }); $(function () { $('.sidebar-menu li:not(.treeview) > a').on('click', function () { var $parent = $(this).parent().addClass('active'); $parent.siblings('.treeview.active').find('> a').trigger('click'); $parent.siblings().removeClass('active').find('li').removeClass('active'); }); $('[data-toggle="popover"]').popover(); //整页刷新时,菜单显示 var selector = $('.sidebar-menu').find('a[href="/'+ selectedMenu +'"]'); selector.parent().addClass('active'); selector.parents('ul.treeview-menu').css('display', 'block'); selector.parents('li.treeview').addClass('menu-open'); //datatables删除按钮 $('#pjax-container').on('click', '.row-delete', function () { var del_url = $(this).data('url'); swal({ title: "确定删除此项?", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "确 定", closeOnConfirm: false, cancelButtonText: "取 消" }, function(){ $.ajax({ method: 'post', url: del_url, data: { _method:'delete', _token:csrf_token, }, success: function (data) { if (typeof data === 'object') { if (data.status) { swal(data.message, '', 'success'); $.pjax.reload('#pjax-container'); } else { swal(data.message, '', 'error'); } } } }); }); }); });
I believe I have read it You have mastered the method in the case of this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the steps for combining React with TypeScript and Mobx
Detailed explanation of the steps for using React-router v4
The above is the detailed content of Detailed explanation of pjax use cases in laravel framework. For more information, please follow other related articles on the PHP Chinese website!