繁体   English   中英

如何将排序添加到 Laravel 视图

[英]How to add sorting to Laravel view

大家好,我想在这里添加一个排序过滤器。我想按 ASC 顺序过滤数据,但问题是来自使用 webpack 的数据。 我可以在此处添加 ASC 订单过滤器吗 {{ $option->value }}

</option>
    @if ($field->options->count() > 0)
        @foreach ($field->options as $option)
            <option value="{{ $option->id }}"
            @if ($defaultValue == $option->id)
            selected="selected"
            @endif
            >
            {{ $option->value }}
</option>

@extends('layout')

@section('content')
<h1>Products</h1>

<p>
    <a class="btn btn-primary" href="/products/create"><span class="glyphicon glyphicon-plus"></span> Add Product</a>
</p>

<table class="table table-bordered table-hover">
    <thead>
        <th>@sortablelink('category.name', 'Category')</th>
        <th>@sortablelink('name', 'Name')</th>
        <th>@sortablelink('sku', 'SKU')</th>
        <th>@sortablelink('price', 'Price')</th>
        <th>Actions</th>
    </thead>
    <tbody>
        @if ($products->count() == 0)
        <tr>
            <td colspan="5">No products to display.</td>
        </tr>
        @endif

        @foreach ($products as $product)
        <tr>
            <td>{{ $product->category->name }}</td>
            <td>{{ $product->name }}</td>
            <td>{{ $product->sku }}</td>
            <td>${{ $product->price }}</td>
            <td>
                <a class="btn btn-sm btn-success" href="{{ action('ProductsController@edit', ['id' => $product->id]) }}">Edit</a>

                <form style="display:inline-block" action="{{ action('ProductsController@destroy', ['id' => $product->id]) }}" method="POST">
                    @method('DELETE')
                    @csrf
                    <button class="btn btn-sm btn-danger"> Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

{!! $products->appends(Request::except('page'))->render() !!}

<p>
    Displaying {{$products->count()}} of {{ $products->total() }} product(s).
</p>

@endsection

您可以对集合进行排序:

@foreach ($field->options->sortBy('your_column_name') as $option)

// Example
->sortBy('your_column_name') // ASC
->sortByDesc('your_column_name') // DESC

如果有任何问题,您可以使用 ->all() 收集方法

->sortBy('your_column_name')->all()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM