如何在Laravel HTML

时间:2018-09-26 07:39:38

标签: database laravel sorting columnsorting

我正在尝试对表格中的特定行进行排序。但是我陷入其中。我正在使用Kyslik \ ColumnSortable \ Sortable;在模型文件中。但是没有实际效果。有这个主意吗?

class estates extends Model
{
use Sortable;

protected $fillable = [ 'Entry', 'Price' ];

protected $table="estates";

public $Sortable = ['Entry', 'Price']; 
}

我的控制器;

public function sumos()
{
  $data['este'] = estates::all();
  return view('pages.sumo', $data);

  return view('pages.sumo',['este' => estates::sortable()->get()]);

}

和html;

<table align="center">
<tr>
    <td colspan="11" id="header"><h1>物件概要<h1></td>
</tr>
    <tr>
        <th width="340px">会社名</th>
        <th width="80px">物件名</th>
        <th width="135px">所在地</th>
        <th width="135px">総戸数</th>
        <th width="80px">間取り</th>
        <th width="100px">専有面積</th>
        <th width="80px">バルコニー面積</th>
        <th width="100px">竣工年月日</th>
        <th width="100px">@sortablelink('Entry', '入居年月日')</th>
        <th width="100px">@sortablelink('Price', '価格')</th>
        <th width="100px">販売会社名</th>
    </tr>
<table>


@foreach($este as $row) 
<table align="center">
    <tr>
        <td width="340px">Company Name</td>
        <td width="80px">{{ $row->Building_Names }}</td>
        <td width="135px">{{ $row->Addresses }}</td>
        <td width="135px">{{ $row->HouseHolds }}</td>
        <td width="80px">{{ $row->Rooms }}</td>
        <td width="100px">{{ $row->Balconys }}</td>
        <td width="80px">{{ $row->Extents }}</td>
        <td width="100px">{{ $row->Constrution }}</td>
        <td width="100px">{{ $row->Entry }}</td>
        <td width="100px">{{ $row->Price }}</td>
        <td width="100px">{{ $row->Company }}</td> 
    </tr>
</table>
@endforeach

关于如何解决(如何排序)特定数据的任何想法? 谢谢

4 个答案:

答案 0 :(得分:1)

最简单的解决方案之一是使用datatables

答案 1 :(得分:1)

使用laravel内置的方法通过两种方式进行排序。

by queryby collection

  

尝试这个

estates::orderBy('Entry')->orderBy('Price')->get()

答案 2 :(得分:1)

我快速浏览了您所使用的软件包的文档。

https://github.com/Kyslik/column-sortable

您必须在表标题中添加@sortablelink('field', 'Field')

此外,查看控制器方法,将永远无法达到您的排序。

public function sumos()
{
  return view('pages.sumo',['este' => estates::sortable()->get()]);
}

希望这会有所帮助

答案 3 :(得分:1)

您应该写这个。希望这能解决您的问题

<table align="center">
    <tr>
        <td colspan="11" id="header"><h1>物件概要<h1></td>
    </tr>
        <tr>
            <th width="340px">会社名</th>
            <th width="80px">物件名</th>
            <th width="135px">所在地</th>
            <th width="135px">総戸数</th>
            <th width="80px">間取り</th>
            <th width="100px">専有面積</th>
            <th width="80px">バルコニー面積</th>
            <th width="100px">竣工年月日</th>
            <th width="100px">@sortablelink('Entry', '入居年月日')</th>
            <th width="100px">@sortablelink('Price', '価格')</th>
            <th width="100px">販売会社名</th>
        </tr>

@foreach($este as $row) 
        <tr>
            <td width="340px">Company Name</td>
            <td width="80px">{{ $row->Building_Names }}</td>
            <td width="135px">{{ $row->Addresses }}</td>
            <td width="135px">{{ $row->HouseHolds }}</td>
            <td width="80px">{{ $row->Rooms }}</td>
            <td width="100px">{{ $row->Balconys }}</td>
            <td width="80px">{{ $row->Extents }}</td>
            <td width="100px">{{ $row->Constrution }}</td>
            <td width="100px">{{ $row->Entry }}</td>
            <td width="100px">{{ $row->Price }}</td>
            <td width="100px">{{ $row->Company }}</td> 
        </tr>
 @endforeach
 </table>

还要在模型顶部添加use Kyslik\ColumnSortable\Sortable;

在您的控制器中修改此

public function sumos()
{
  $este= estates::sortable(['Price' => 'desc'])->get();
  return view('pages.sumo', compact('este'));
}