【问题标题】:Laravel - Pagination doesn't work wellLaravel - 分页效果不佳
【发布时间】:2016-11-21 15:17:26
【问题描述】:

我有一个带有表格和分页的视图,但是当我使用 de pagination 时,视图是重复的。

我的控制器功能:

public function index()
{
    $items = Items::where('active', '=', 1)->paginate(5);
    if (Request::ajax())
    {
        return View::make('app/items.index')->with('items', $items)->render();
    }
    else
    {
        return view('app/items.index', array('items' => $items));    
    }

}

我的 jquery:

function pagination()
    {
        $(document).on("click", ".pagination a", function(e)
        {
            e.preventDefault();
            alert($(this).attr('href'));
            $.ajax({
                url: $(this).attr('href'),
            })
            .done(function(data)
            {
                console.log(data);
                $('#listSearchItems').html(data);
            });


        });
    }

我的看法:

@section('content')
<div class="header"></div>
<div id="listSearchItem" class="content">
<table>
</table>
@if($items->render())
                <nav class="pagination">
                    <a href="{{$items->previousPageUrl()}}">
                        <i class="material-icons">arrow_back</i>
                    </a>
                    <a href="{{$items->nextPageUrl() }}"> 
                        <i class="material-icons">arrow_forward</i>
                    </a>
                </nav>
            @endif</div>@stop

当我点击分页按钮时,视图代码在 中全部重复,表格信息被更新,但也添加了所有现有的视图 html,重复代码。

有人可以帮助我吗?关于这个问题我已经搜索了很多,我不知道如何返回控制器中的信息。

谢谢

【问题讨论】:

    标签: php jquery laravel-5


    【解决方案1】:

    您的代码存在一些问题。 首先,为什么需要 ajax 来呈现数据?

    如果你需要使用 ajax 渲染数据,你的控制器应该是这样的:

    public function index()
    {
        $items = Items::where('active', '=', 1)->paginate(5);
        if (Request::ajax())
        {
            $datas = $items->items(); 
            $results = '';   
            foreach ($datas as $item) {
                $results .="<tr><td>{$item->whateverInYourTable}</td><td>{$item->whateverInYourTable}</td></tr>"
        }
            return $results;
    
        }
        else
        {
        return view('app/items.index', array('items' => $items));
        }
    }
    

    如果你不需要ajax控制器应该是这样的:

    public function index()
    {
        $items = Items::where('active', '=', 1)->paginate(5);
        return view('app.items.index', array('items' => $items));
    }
    

    像这样查看:

    <nav class="pagination">
         {!! $items->render() !!}
    </nav>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2018-06-21
      • 2020-09-08
      • 1970-01-01
      • 2018-01-20
      • 2018-06-07
      相关资源
      最近更新 更多