the_list_view.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. The List View
  2. =============
  3. You've given the admin a nice interface to create and edit blog posts and
  4. categories. But there is not much to change if the admin doesn't have a list of
  5. all available blog posts. Because of this, this chapter will teach you more
  6. about the list view.
  7. If you have created some posts and go to
  8. http://localhost:8000/admin/app/blogpost/list, you'll see an empty list page.
  9. That's not because there is no content, but because you didn't configure your
  10. Admin's list view. As Sonata doesn't know which fields to show, it just shows
  11. empty rows.
  12. Configuring the List Mapper
  13. ---------------------------
  14. Fixing the above problem is easy: Just add the fields you want to show on the
  15. list page to the list view:
  16. .. code-block:: php
  17. // src/AppBundle/Admin/BlogPostAdmin.php
  18. namespace AppBundle\Admin;
  19. // ...
  20. class BlogPostAdmin extends Admin
  21. {
  22. // ...
  23. protected function configureListFields(ListMapper $listMapper)
  24. {
  25. $listMapper
  26. ->add('title')
  27. ->add('draft')
  28. ;
  29. }
  30. }
  31. Going to the list view of the blog post admin again, you'll see the available
  32. blog posts:
  33. .. image:: ../images/getting_started_basic_list_view.png
  34. You can see that Sonata already guesses a correct field type and makes sure it
  35. shows it in a friendly way. The boolean draft field for instance is show as a
  36. red "no" block, indicating ``false``.
  37. Cool! But... how can an admin go from this page to the edit page for a blog post?
  38. There seem to be nothing that looks like a link. That's correct, you need to
  39. tell Sonata which field(s) you want to use as a link.
  40. Defining the Identifier Field(s)
  41. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. The fields which contain a link to the edit pages are called identifier fields.
  43. It makes sense to make the title field link to the edit page, so you can add it
  44. as an identifier field. This is done by using ``ListMapper#addIdentifier()``
  45. instead of ``ListMapper#add()``:
  46. .. code-block:: php
  47. // src/AppBundle/Admin/BlogPostAdmin.php
  48. namespace AppBundle\Admin;
  49. // ...
  50. class BlogPostAdmin extends Admin
  51. {
  52. // ...
  53. protected function configureListFields(ListMapper $listMapper)
  54. {
  55. $listMapper
  56. ->addIdentifier('title')
  57. ->add('draft')
  58. ;
  59. }
  60. }
  61. When saving this, you can now see that the title field has the link you were
  62. looking for.
  63. Displaying Other Models
  64. ~~~~~~~~~~~~~~~~~~~~~~~
  65. Now you probably also want the Category to be included in the list. To do that,
  66. you need to reference it. You can't add the ``category`` field to the list
  67. mapper, as it will then try to show the entity as a string. As you've learned
  68. in the previous chapter, adding ``__toString`` to the entity is not recommended
  69. as well.
  70. Fortunately, there is an easy way to reference other models by using the dot
  71. notation. Using this notation, you can specify which fields you want to show.
  72. For instance, ``category.name`` will show the ``name`` property of the
  73. category.
  74. .. code-block:: php
  75. // src/AppBundle/Admin/BlogPostAdmin.php
  76. namespace AppBundle\Admin;
  77. // ...
  78. class BlogPostAdmin extends Admin
  79. {
  80. // ...
  81. protected function configureListFields(ListMapper $listMapper)
  82. {
  83. $listMapper
  84. ->addIdentifier('title')
  85. ->add('category.name')
  86. ->add('draft')
  87. ;
  88. }
  89. }
  90. Adding Filter/Search Options
  91. ----------------------------
  92. Assume you had a very succesfull blog site containing many blog posts. After a
  93. while, finding the blog post you wanted to edit would be like finding a needle
  94. in a haystack. As with all user experience problems, Sonata provides a solution
  95. for it!
  96. It does this by allowing you to configure datagrid filters in the
  97. ``Admin#configureDatagridFilters()`` method. For instance, to allow the admin
  98. to search blog posts by title (and also order them by alfabet in the list), you
  99. would do something like:
  100. .. code-block:: php
  101. // src/AppBundle/Admin/BlogPostAdmin.php
  102. namespace AppBundle\Admin;
  103. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  104. // ...
  105. class BlogPostAdmin extends Admin
  106. {
  107. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  108. {
  109. $datagridMapper->add('title');
  110. }
  111. }
  112. This will add a little block to the left of the block showing a search input
  113. for the title field.
  114. Filtering by Category
  115. ~~~~~~~~~~~~~~~~~~~~~
  116. Filtering by another model's properties is a little bit more difficult. The add
  117. field has 5 arguments:
  118. .. code-block:: php
  119. public function add(
  120. $name,
  121. // filter
  122. $type = null,
  123. array $filterOptions = array(),
  124. // field
  125. $fieldType = null,
  126. $fieldOptions = null
  127. )
  128. As you can see, you can both customize the type used to filter and the type
  129. used to display the search field. You can rely on the type guessing mechanism
  130. of Sonata to pick the correct field types. However, you still need to configure
  131. the search field to use the ``name`` property of the Category:
  132. .. code-block:: php
  133. // src/AppBundle/Admin/BlogPostAdmin.php
  134. namespace AppBundle\Admin;
  135. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  136. // ...
  137. class BlogPostAdmin extends Admin
  138. {
  139. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  140. {
  141. $datagridMapper
  142. ->add('title');
  143. ->add('category', null, array(), 'entity', array(
  144. 'class' => 'AppBundle\Entity\Category',
  145. 'property' => 'name',
  146. ))
  147. ;
  148. }
  149. }
  150. With this code, a dropdown will be shown including all available categories.
  151. This will make it easy to filter by category.
  152. .. image:: ../images/getting_started_filter_category.png
  153. Round Up
  154. --------
  155. This time, you've learned how to make it easy to find posts to edit. You've
  156. learned how to create a nice list view and how to add options to search, order
  157. and filter this list.
  158. There might have been some very difficult things, but imagine the difficulty
  159. writing everything yourself! As you're now already quite good with the basics,
  160. you can start reading other articles in the documentation, like:
  161. * :doc:`Customizing the Dashboard <../reference/dashboard>`
  162. * :doc:`Configuring the Security system <../reference/security>`
  163. * :doc:`Adding export functionality <../reference/action_export>`
  164. * :doc:`Adding a preview page <../reference/preview_mode>`