2017-02-13 96 views

回答

2

轉到所需的列表。 複製瀏覽器的網址:

現在使用jQuery:

$.get(url_you_just_copied, function(result){ 
    console.log(result); 
}; 

看一看你是從的listAction呼叫取回和適應請求參數(過濾器,...),相應地得到你想要的清單。

旁邊有一個乾淨的解決方案,你需要用樹枝幫手生成您的網址:https://stackoverflow.com/a/15857401/5758328

+0

是的,通過調用ajax調用列表,我得到了可以管理顯示的ajax模板的數據。謝謝 – fliim

0

這是我如何做它用sonataBlockBundle顯示郵件列表:

塊服務類:

namespace Librinfo\EmailBundle\Block; 

use Doctrine\ORM\EntityManager; 
use Librinfo\EmailBundle\Entity\Email; 
use Sonata\BlockBundle\Block\BlockContextInterface; 
use Sonata\BlockBundle\Block\Service\TextBlockService; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\HttpFoundation\Response; 

class EmailsListBlock extends TextBlockService 
{ 
    /** 
    * @var EntityManager 
    */ 
    protected $manager; 

    /** 
    * @param EntityManager $manager 
    */ 
    public function setManager(EntityManager $manager) { 
     $this->manager = $manager; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function execute(BlockContextInterface $blockContext, Response $response = null) 
    { 
     $settings = $blockContext->getSettings(); 
     $targetEntity = $settings['target_entity']; 
     $maxResults = $settings['max_results']; 
     $emails = $this->getEmails($targetEntity, $maxResults); 

     return $this->renderResponse($blockContext->getTemplate(), array(
      'block' => $blockContext->getBlock(), 
      'settings' => $settings, 
      'emails' => $emails, 
     ), $response); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function configureSettings(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'content' => 'Insert your custom content here', 
      'template' => 'LibrinfoEmailBundle:Block:block_emails_list.html.twig', 
      'target_entity' => null, 
      'max_results' => 20, 
     )); 
    } 

    /** 
    * @param object $targetEntity 
    * @param int $maxResults 
    * @return array 
    * @throws \Exception 
    */ 
    private function getEmails($targetEntity, $maxResults) 
    { 
     if (!$targetEntity || !is_object($targetEntity)) 
      return []; 
     $rc = new \ReflectionClass($targetEntity); 
     if (!$rc->hasProperty('emailMessages')) 
      return []; 

     $repo = $this->manager->getRepository($rc->getName()); 
     if (method_exists($repo, 'getEmailMessagesQueryBuilder')) { 
      $qb = $repo->getEmailMessagesQueryBuilder($targetEntity->getId()); 
     } 
     else { 
      $repo = $this->manager->getRepository(Email::class); 
      $targets = strtolower($rc->getShortName()) . 's'; // ex. contacts 
      $qb = $repo->createQueryBuilder('e') 
       ->leftJoin('e.'.$targets, 't') 
       ->where('t.id = :targetid') 
       ->setParameter('targetid', $targetEntity->getId()) 
      ; 
     } 
     $qb->orderBy('e.updatedAt', 'desc') 
      ->setMaxResults($maxResults); 

     return $qb->getQuery()->getResult(); 
    } 


} 

服務定義:

librinfo.email.block.emails_list: 
     class: Librinfo\EmailBundle\Block\EmailsListBlock 
     arguments: 
      - librinfo.email.block.emails_list 
      - '@templating' 
     calls: 
      - [setManager, [@doctrine.orm.entity_manager]] 
     tags: [{ name: sonata.block }] 

和模板(略)

{% extends sonata_block.templates.block_base %} 

{% block block %} 
    <table class="table table-bordered table-striped sonata-ba-list emails-history"> 
     <thead> 
      <tr> 
       <th>Expéditeur</th> 
       <th>Destinataires</th> 
       <th>Objet</th> 
       <th>Envoyé</th> 
       <th>Date</th> 
       <th>Actions</th> 
      </tr> 
     </thead> 
     <tbody> 
      {% for email in emails %} 
       <tr data-email-id="{{ email.id }}"> 
then use it like this in your edit template: 

    {{ sonata_block_render({'type': 'librinfo.email.block.emails_list'}, {'target_entity': object}) }} 
+0

我想使用相同的數據網格渲染風格管理員列表視圖頁面,以避免再次重新定義的模板。 – fliim

+0

使用第二種技巧,我建議那麼它應該很簡單 – Mawcel

+0

這是我在尋找什麼。可以在我的編輯頁面上包含列表頁面控制器本身,其中只包含數據網格? – fliim