2016-09-10 4 views
0

ブール値ブロックをWagtailモデルに追加し、チェックされている場合にのみコンテンツパネルフィールドを表示します。私はブール値のブロックを追加し、その値に基づいてテンプレートのコンテンツをレンダリングする方法を考え出しましたが、エディタのインターフェースをどのように制御するかは分かりません。ここに私のモデルです。私は、heldover_from、date chooserブロックを、holdover booleanがチェックされているときにだけ表示したいと思います。Wagtail Editorのブール値ブロックに基づいて追加のコンテンツパネルを表示する

class AgendaPage(Page): 
author= models.CharField(max_length=255) 
date = models.DateField('Post date') 
agenda = StreamField([ 
    ('agenda_item', blocks.StreamBlock([ 
     ('item_title', blocks.TextBlock()), 
     ('item_text', blocks.TextBlock()), 
     ('mtg_doc', blocks.StructBlock([ 
      ('doc_description', blocks.TextBlock()), 
      ('doc_link', blocks.TextBlock()), 
      ('submitted_late', blocks.BooleanBlock(required=False, help_text='Submitted Late')), 
      ('heldover', blocks.BooleanBlock(required=False, help_text='Held Over')), 
      ('heldover_from', blocks.DateBlock(required=False, help_text="Held Over From") 

     ])) 
     ] 
    )) 
]) 




content_panels = Page.content_panels + [ 
    FieldPanel('author'), 
    FieldPanel('date'), 
    StreamFieldPanel('agenda'), 

] 

(そして、私はこれを理解した後、私はそれが必要で作ることができるかどうかを知りたいが、heldoverがチェックされている場合にのみ、全体ではなくstreamblock用)

{% for block in self.agenda %} 
    {% if block.block_type == "agenda_item" %} {# will always be true, but included here for clarity #} 
    <li> 
    {% for subblock in block.value %} 
     {% if subblock.block_type == "item_title" %} 
      <h2>{{subblock.value}}</h2> 
     {% elif subblock.block_type == "item_text" %} 
       <p>{{subblock.value}}</p> 
     {% elif subblock.block_type == "mtg_doc" %} 
       <p><a href="{{subblock.value.doc_link}}">{{subblock.value.doc_description}}</a><br /> 
      {% ifequal subblock.value.submitted_late True %} 
      (Submitted Late) 
      {% endifequal %} 
       </p> 
     {% endif %} 
    {% endfor %} 
</li> 
    {% endif %} 
{% endfor %} 
+0

現在のテンプレートコードを表示できますか? – gasman

+0

私は現在のテンプレートコードを投稿しました – JohnnyP

答えて

1

あなたがこれを行うことができますhttp://docs.wagtail.io/en/v1.6.2/topics/streamfield.html#custom-editing-interfaces-for-structblockで説明されているように、StructBlockのフォームテンプレートをオーバーライドすることで、フォームマークアップのかなり低いレベルの詳細を手にする必要があります。

まずは、もう少し余裕のために、独自のクラスにmtg_docブロック定義を引き出してみましょう:私はブロッククラスにform_templateパラメータを追加しました。ここ

class MtgDocBlock(blocks.StructBlock): 
    doc_description = blocks.TextBlock() 
    doc_link = blocks.TextBlock() 
    submitted_late = blocks.BooleanBlock(required=False, help_text='Submitted Late') 
    heldover = blocks.BooleanBlock(required=False, help_text='Held Over') 
    heldover_from = blocks.DateBlock(required=False, help_text="Held Over From") 

    class Meta: 
     form_template = 'myapp/block_forms/mtg_doc.html' 


class AgendaPage(Page): 
    ... 
    agenda = StreamField([ 
     ('agenda_item', blocks.StreamBlock([ 
      ('item_title', blocks.TextBlock()), 
      ('item_text', blocks.TextBlock()), 
      ('mtg_doc', MtgDocBlock()) 
     ]) 
    ]) 

に代替テンプレートを指定しますWagtailに組み込まれたフォームの代わりにフォームをレンダリングするのに使用します。実際のレンダリングを変更したくないので、組み込みのテンプレート(wagtailadmin/block_forms/struct.htmlにあります)をインクルードし、それに少しのJS動作を追加するだけです。 In templates/myapp/block_forms/mtg_doc.html

{% include "wagtailadmin/block_forms/struct.html" %} 

<script> 
    // all fields of the block have a common prefix on the ID, 
    // which is available as the template variable 'prefix'. 
    // Retrieve the 'heldover' checkbox 
    var checkbox = $('#{{ prefix }}-heldover'); 

    // Retrieve the 'li' element containing the 'heldover_from' field 
    var field = $('#{{ prefix }}-alignment').closest('li'); 

    function showHideField() { 
     // update the visibility of field according to the state of 
     // the checkbox 
     if (checkbox.is(':checked')) { 
      field.show(); 
     } else { 
      field.hide(); 
     } 
    } 
    // call showHideField immediately to reflect the initial state 
    // of the checkbox 
    showHideField(); 
    // trigger showHideField whenever the checkbox state is changed 
    checkbox.change(showHideField); 
</script> 
+0

ガスマン - ありがとう、私はこれを実現するためにjavascriptを追加したいと思っていたが、私はどこに/どのようにはわからなかった。私はnoobの質問を持っています:私は私のサイトのフォルダ構造にwagtailadminディレクトリが表示されません。理由は考えられますか? – JohnnyP

+0

これはアプリケーション/プロジェクトレベルではないwagtailコアレベルにあるためです – JohnnyP

関連する問題