简体   繁体   中英

Multiselect tags in sonata admin form

I want to set up a multiselect tags in my sonata admin form like in the picturebelow :

http://i.stack.imgur.com/FUjRV.jpg

Here is the code of my entity :

    class Company{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;


    /**
     * @ORM\ManyToMany(targetEntity="City", cascade={"persist"})
     */
    protected  $city;




    /**
     * Constructor
     */
    public function __construct()
    {
        $this->city = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Add city
     *
     * @param \Company\AdminBundle\Entity\City $city
     * @return Company
     */
    public function addVille(\Company\AdminBundle\Entity\City $city)
    {
        $this->city[] = $city;

        return $this;
    }

    /**
     * Remove city
     *
     * @param \Company\AdminBundle\Entity\City $city
     */
    public function removeCity(\Company\AdminBundle\Entity\City $city)
    {
        $this->city->removeElement($city);
    }

    /**
     * Get city
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Company
     */
    public function setName($name)
    {
        $this->name= $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    public function __toString()
    {

        return (string) $this->name;
    }
}

So I want that the first select tag contains all the cities and in the second , I select any element I want.

how can I make this kind of type?

Sonata admin will automatically give you multiselect for properly configured Many-to-many relationships. You just need to use a jquery multiselect plugin to make the standard more useable, like your picture above.

See: https://github.com/yanickrochon/jquery.uix.multiselect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM