2017-02-09 2 views
1

私はLaravelTwigに基づいてOctoberCMSを使って簡単なギャラリーを作っています。カテゴリでレコードをフィルタリングする方法Laravel Twig?

カテゴリごとにレコードをフィルタリングするにはどうすればよいですか?ここで

は、私は今それをやっている方法ですが、私は、これは良い解決策だとは思わない:私はHTMLをフィルタリング

が、完全なレコードがまだ引き起こし、カテゴリ外のすべてのアイテムのために存在します余分なページ番号と空白ページ。

データベース

  • Aテーブル 'ギャラリー' は、カテゴリ名のリストを保持しています。

  • テーブル '画像'は、画像名とそのタグ付けされたカテゴリを保持します。

画像一覧

URLパラメータ/ギャラリー/です:カテゴリ/:?ページは?

/gallery/nature/1のようなURLを訪問すると、forループを使用してカテゴリ別に画像をフィルタリングします。

<!-- Gallery Category Record Variable --> 
{% set category = record.category %} 

<!-- Image List --> 
{% for record in records %} 

    <!-- If Image Category Tag matches Gallery Category Name --> 
    {% if record.category_tag == category %} 
     <img src="/{{ record.name }}.jpg"> 
    {% endif %} 

{% endfor %} 

レコード '記録' と '記録' の

結果。

forループは 'images→category_tag'レコードに 'gallery→category'レコードを表示しています。

ページネーションはすべての「画像→名前」レコードを表示しています。

{% for record in records %} = (All in 'images') 
{{ records }} = {<ul><li>...<a href="gallery/nature?page=2">2</a>} (All in 'images') 
{{ record }} = {"category":"nature","id":7} (Current category in 'gallery') 

解決策?

htmlリストを生成する前にカテゴリ別に 'レコード'をフィルタリングする方法はありますか?

+1

わからないからインスピレーションを得た

// list the allowed sorting options that will show up in your component public static $allowedSortingOptions = array( 'created_at asc' => 'Images Created (ascending)', 'created_at desc' => 'Images Created (descending)', 'updated_at asc' => 'Images Updated (ascending)', // ect.... ); // Scope for your component public function scopelistImages($query, $options) { /* * Default options */ extract(array_merge([ 'page' => 1, 'perPage' => 15, 'sort' => 'updated_at', 'category' => null ], $options)); // SORTING if (!is_array($sort)) { $sort = [$sort]; } foreach ($sort as $_sort) { if (in_array($_sort, array_keys(self::$allowedSortingOptions))) { $parts = explode(' ', $_sort); if (count($parts) < 2) { array_push($parts, 'desc'); } list($sortField, $sortDirection) = $parts; if ($sortField == 'random') { $sortField = Db::raw('RAND()'); } $query->orderBy($sortField, $sortDirection); } } // Filter by category ........ return $query->paginate($perPage, $page); } 

回答は、それは単純にそれを渡す前に、条件と一致するレコードをフェッチすることではありません景色? 'Gallery :: where( 'category_id'、$ catId) - > get()'のようなものです。もちろん、パラメータ –

+0

@ChristopherFranciscoから取得した名前を使用してカテゴリIDを取得する必要があります。OctoberCMSを使用して、レコードリストまたはレコードの詳細をドラッグアンドドロップし、モデルクラスと表示列を設定します。そこから、Twig構文を使ってレコードを使用することができます。私はこれを初めて使っていて、小枝の外でLaravelを使っています。 –

+0

私の悪いのは、OctoberCMSのリンクがページ区切りページにリダイレクトされているので、ページネーションのコンポーネント(名前がCMSだと言っても、ばかげて私はハハ)と思った。 –

答えて

2

これは私がそれに近づくだろうかです:

  • あなたのイメージに適用範囲を作成します、カテゴリー電気ショック療法によってフィルタリングモデル...
  • は、あなたのコンポーネントでURLのプロパティとロジックを処理
  • を表示しません

ギャラリーコンポーネント:

URL:page.com/:cat/:page?あなたの画像のモデルで

public function defineProperties() 
    { 
     return [ 
      // Page # for pagination.. 
      'pageNumber' => [ 
       'title'  => 'Page #', 
       'description' => 'Gallery Page #', 
       'type'  => 'string', 
       'default'  => '{{ :page }}', 
      ], 
      // Category slug 
      'category' => [ 
       'title'  => 'Category', 
       'description' => 'Gallery Cat', 
       'type'  => 'string', 
       'default'  => '{{ :cat }}', 
      ], 
      // Images to show per page 
      'perPage' => [ 
       'title'    => 'Images per page', 
       'type'    => 'string', 
       'validationPattern' => '^[0-9]+$', // validation 
       'validationMessage' => 'VValidation Error', 
       'default'   => '15', 
      ], 
      // if you want to add sorting 
      'sortOrder' => [ 
       'title'  => 'Sort Order', 
       'description' => 'Images Sort Order', 
       'type'  => 'dropdown', 
       'default'  => 'updated_at desc' 
      ], 
     ]; 
    } 

public function getSortOrderOptions() 
{ 
    return Image::$allowedSortingOptions; 
} 


public function init() 
{ 
    $this->pageNumber = empty($this->property('pageNumber')) ? 1 : $this->property('pageNumber'); 
    $this->perPage  = $this->property('perPage'); 
    $this->sortOrder = $this->property('sortOrder'); 
    $this->category  = $this->property('category'); 
} 

public function onRun() 
{ 

    // here you may want to do some checks 
// and add logic before querying your DB 

    return $this->listImages($this->pageNumber , $this->sortOrder, $this->perPage, $this->category); 

} 

public function listImages($pageNumber, $sortOrder, $perPage, $category){ 

    // this is the scope you will define in your Images Model 
// to handle pagination, sorting, category filtering ect.. 

    $images = Images::listFrontEnd([ 
     'page'   => $pageNumber, 
     'sort'   => $sortOrder, 
     'perPage'  => $perPage, 
     'category'  => $category, 
    ]); 



    // small helper if the pagination # is > than last page 
    // redirect to last page.. 

    $lastPage = $images->lastPage(); 

    if ($this->pageNumber > $lastPage && $this->pageNumber > 1){ 
      return Redirect::to($this->currentPageUrl(["page" => $lastPage])); 
     } 


$this->images = $this->page['images'] = $images; 

} 

:私は誤解が、場合 RainLab Blog Plugin

関連する問題