據我理解你所面臨的兩個問題:堅持跨越分頁要求檢查的項目,並檢索選中的項回到視圖。
要堅持檢查項目跨分頁請求,我會flash檢查項目進入會議。控制器方法看起來如下所示。
public function fill_form()
{
$items = Item::paginate(25);
// Retrieve checked items in session.
$checked_items = []
if (Session::has('checked_items'))
$checked_items = Session::get('checked_items');
// Persist new checked items.
$checked_items = array_merge($checked_items, Input::get('item'));
Session::flash('checked_items', $checked_items);
return View::make('form')
->with('items', $items);
}
正如你可以看到分頁請求中的檢查項目將在本屆會議。
現在爲了將檢查的項目顯示回視圖,我會發送檢查的項目在會話中通過old input查看。也就是說,返回值會改變如下。
public function fill_form()
{
# code intentionally omitted #
return View::make('form')
->with('items', $items)
->withInput($checked_items);
}
然後在您的意見中檢查的項目將堅持他們的檢查值。顯然,你應該use Laravel to generate your checkboxes。
如何獲取所有項目(選中或不選)提交?
也許,如果您使用複選框渲染項目,您將需要知道哪些複選框已選中,哪些選項未進行分頁。一個簡單的解決方案將增加額外的輸入隱藏字段用默認值的每個複選框,它會看起來如下:
{{ Form::hidden('item1', 'off') }}
{{ Form::checkbox('item1', 'on') }}
{{ Form::hidden('item2', 'off') }}
{{ Form::checkbox('item2', 'on') }}
{{ Form::hidden('item3', 'off') }}
{{ Form::checkbox('item3', 'on') }}
提交表格後,分頁時,託運物品,你會收到預期的值,對於那些沒有檢查的人,你會收到隱藏的價值。
注意1,重要的是把隱藏輸入每個複選框之前。 注意2,每個隱藏的輸入應該與複選框具有相同的名稱。
將它們存儲在數據庫中 –
我將明確使用該會話。對於每個請求,我都會_flash_所選項目的ID。請參閱:http://laravel.com/docs/session#flash-data –