-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
executable file
·164 lines (127 loc) · 6.09 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# ynWidgetAjaxAutocomplete plugin (for symfony 1.3 - 1.4) #
The `ynWidgetAjaxAutocompletePlugin` is a symfony plugin that provides an AJAX
autocomplete replacement for `sfWidgetForm*Choice`. It uses the jQueryUI
Autocomplete widget.
## Installation ##
* Install the plugin (via a package - **not currently available**)
symfony plugin:install ynWidgetAjaxAutocompletePlugin
* Install the plugin (via Subversion checkout)
svn co http://svn.symfony-project.com/plugins/ynWidgetAjaxAutocompletePlugin/tags/0.1.0b1 plugins/ynWidgetAjaxAutocompletePlugin
* Activate the plugin in your project's `config/ProjectConfiguration.class.php`
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugins(array(
'sfDoctrinePlugin',
'ynWidgetAjaxAutocompletePlugin',
'...'
));
}
}
* Enable jQuery in your application via `view.yml` (you may also
want/need to maintain a local copy):
javascripts:
- https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js
* Publish assets
symfony plugin:publish-assets
* Clear your cache
symfony cc
## Utilization ##
To implement the plugin to your form:
* You will need an AJAX handler to populate the autocomplete.
If you wish to use the included AJAX actions, enable the appropriate module
in the plugin's `settings.yml` (see the example below). The Propel version
has not yet been written,
but it will be easy to create (just write one generic action), if you are
using Propel. Alternately can also enable the AJAX module at the application level,
in your application's `settings.yml`.
all:
.settings:
enabled_modules:
- ynWidgetAjaxAutocompleteDoctrine
# - ynWidgetAjaxAutocompletePropel
* Make your `Doctrine_Table` or `*Peer` class implement
ynWidgetAjaxTable, and write `retrieveForYnAjax(). The AJAX action
will supply these parameters:
* `term` - the value to match
* `limit` - the greatest number of results to return
* `not` - keys to exclude, separated by commas
class PersonTable extends Doctrine_Table implements ynWidgetAjaxTable
{
/**
* Returns a JSON-encoded array of persons based on an AJAX-requested query
*
* @param string $term
* @param int $limit
* @param array $not Exclude these IDs
*/
public function retrieveForYnAjax( $term, $limit = 10, array $not = array() )
{
$q = self::getInstance()
->createQuery('p')
->where('p.name LIKE ?', $term.'%')
->limit( $limit )
->orderBy('p.last_name, p.first_name ASC')
;
if ( $not ) {
$q->whereNotIn('p.id', $not);
}
return $q->execute();
}
* Now, add the widget to your form, with some options. The autocomplete
will visually replace the default widget on the page, but you must pass
that widget as the `noscript_widget` option. This allows the plugin's
Javascript to extract data from the stock `select` element, as well as
providing for graceful degradation.
* Supply the route of the AJAX action as `source`. Include the `not`
parameter as comma-separated IDs, if you wish to exclude one or more records.
This is useful if you are dealing with a nested relation, to avoid associating
the record with itself.
* Supply a route for linking to the referenced record as `item_route`. The
Javascript will append `?id=12345` to the value, so your routing must be able
to work with this.
* Supply a URL for a link next to the widget, e.g. to create a new
record of the referenced class, as `aux_url`, and the text of the link as
`aux_link_text` (optional)
/* in FooForm */
// handy for composing paths
sfProjectConfiguration::getActive()->loadHelpers('Url');
$this_id = $this->getObject()->getId();
$this->widgetSchema['animals_list'] = new ynWidgetAjaxAutocomplete(
array(
'noscript_widget' => $this->widgetSchema['animals_list'],
'source' => url_for( 'ynwidgetajax/animal' ),
'item_route' => url_for( 'animal/edit' ),
'aux_url' => url_for( 'animal/new' ),
'aux_link_text' => 'Create a new animal',
)
);
And that's it! You should now have a working AJAX autocomplete widget on
your form.
## jQueryUI ##
See also
* http://jqueryui.com/
* http://docs.jquery.com/UI/Autocomplete
Since this plugin extends the jQueryUI Autocomplete widget, you can easily
substitute the theme of your choosing. Pass the path of your main theme CSS
file as the `theme_css` option.
The included jQueryUI library contains only the widgets necessary for the
plugin. If you need other widgets, you can change the path to the jQueryUI
library, or remove it entirely if you have loaded it via `view.yml` or
some other means. Pass either the path or `null` as the `jqueryui_js`
option.
You can pass options to the jQueryUI Autocomplete widget itself as
`autocomplete_options`.
For example:
$this->widgetSchema['animals_list'] = new ynWidgetAjaxAutocomplete(
array(
'noscript_widget' => $this->widgetSchema['animals_list'],
'source' => url_for( 'ynwidgetajax/animal' ),
'item_url' => url_for( 'animal/edit?id=9999999999' ),
'aux_url' => url_for( 'animal/new' ),
'theme_css' => '/path/to/my/jquery-ui.css',
'jqueryui_js' => null, // will not load jQueryUI from this plugin
'autocomplete_options' => array( 'minLength' => 4 ),
)
);