joacub/zf2-doctrine-autocomplete

为 ZF2 和 Doctrine2 提供自动完成表单。

dev-master 2016-09-02 05:19 UTC

This package is not auto-updated.

Last update: 2024-09-19 09:42:53 UTC


README

Doctrine 2 和 ZF2 的表单元素自动完成

要求

DoctrineModule, jQuery, jQueryUi,

安装

使用 github

cd vendor
git clone https://github.com/fabiopaiva/Zf2DoctrineAutocomplete

使用 composer

php composer.phar require fabiopaiva/zf2-doctrine-autocomplete:dev-master

将javascript初始化器复制到您的公共文件夹

# if composer
cp vendor/fabiopaiva/zf2-doctrine-autocomplete/data/zf2-doctrine-autocomplete.min.js public/js/
# if github
cp vendor/Zf2DoctrineAutocomplete/data/zf2-doctrine-autocomplete.min.js public/js/

启用模块

在 application.config.php 中启用此模块

return array(
    'modules' => array(
    'DoctrineModule',
    'Zf2DoctrineAutocomplete',
    'Application',
    )   
);

将javascript文件添加到您的布局

从数据文件夹复制此文件

echo $this
    ->headScript()
    ->prependFile($this->basePath() . '/js/zf2-doctrine-autocomplete.min.js');

使用您的参数创建自定义表单元素

此文件必须静态配置,因为它是从 Zf2DoctrineAutocomplete 引擎调用的

<?php

namespace Application\Form\Element;
use Zf2DoctrineAutocomplete\Form\Element\ObjectAutocomplete;

class MyAutocompleteElement extends ObjectAutocomplete {

    private $initialized = false;

    public function setOptions($options) {
    if (!$this->initialized) {
        $options = array_merge($options, array(
            'class' => get_class($this),
            'object_manager' => $options['sm']->get('Doctrine\ORM\EntityManager'), // For Doctrine ORM
            // 'object_manager' => $options['sm']->get('doctrine.documentmanager.odm_default'), // For Doctrine ODM (Mongodb)
            'target_class' => 'Application\Entity\MyEntity',
            'searchFields' => array('code', 'description'),
            'empty_item_label' => 'Nothing found',
            'select_warning_message' => 'Select a itemName in list',
            'property' => 'description',
            'orderBy' => array('code','ASC')
        ));
        $this->initialized = true;
        }

    parent::setOptions($options);
    }

}

将自定义元素添加到您的表单

$form->add(array(
        'name' => 'myAutocompleteElement',
        'type' => 'Application\Form\Element\MyAutocompleteElement',
        'options' => array(
            'label' => 'My label here',
            'sm' => $serviceManager // don't forget to send Service Manager
        ),
        'attributes' => array(
            'required' => true,
            'class' => 'form-control input-sm'
        )
    ));

动态添加元素

在页面上添加新元素后,调用初始化器

zf2DoctrineAutocomplete.init('#jQuerySelector');