在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:crowdint/rails3-jquery-autocomplete开源软件地址:https://github.com/crowdint/rails3-jquery-autocomplete开源编程语言:Ruby 81.6%开源软件介绍:Project MovedThere has been some confusion around the naming as to where to find the Rails4 version of this gem due to the original naming. With the latest release I have taken the opportunity to address this by moving the official repo. The official repo is now located at rails-jquery-autocomplete rails3-jquery-autocompleteAn easy way to use jQuery's autocomplete with Rails 3. Supports both ActiveRecord, mongoid, and MongoMapper. Works with Formtastic and SimpleForm ActiveRecordYou can find a detailed example on how to use this gem with ActiveRecord here. MongoIDYou can find a detailed example on how to use this gem with MongoID here. (Same thing, different branch) Before you startMake sure your project is using jQuery-UI and the autocomplete widget before you continue. You can find more info about that here: I'd encourage you to understand how to use those 3 amazing tools before attempting to use this gem. InstallingInclude the gem on your Gemfile
Install it
Rails 3.0.xRun the generator
And include autocomplete-rails.js on your layouts
Upgrading from older versionsIf you are upgrading from a previous version, run the generator after installing to replace the javascript file.
I'd recommend you do this every time you update to make sure you have the latest JS file. Uncompressed Javascript fileIf you want to make changes to the JS file, you can install the uncompressed version by running:
Rails 3.1.x and higherJust add it to your app/assets/javascripts/application.js file
UsageModel ExampleAssuming you have a Brand model:
ControllerTo set up the required action on your controller, all you have to do is call it with the class name and the method as in the following example:
This will create an action autocomplete_brand_name on your controller, don't forget to add it on your routes file
Options:full => trueBy default, the search starts from the beginning of the string you're searching for. If you want to do a full search, set the full parameter to true.
The following terms would match the query 'un':
:full => false (default behavior)Only the following terms mould match the query 'un':
:limit => 10 (default behavior)By default your search result set is limited to the first 10 records. This can be overridden by specifying the limit option. :extra_dataBy default, your search will only return the required columns from the database needed to populate your form, namely id and the column you are searching (name, in the above example). Passing an array of attributes/column names to this option will fetch and return the specified data.
:display_valueIf you want to display a different version of what you're looking for, you can use the :display_value option. This options receives a method name as the parameter, and that method will be called on the instance when displaying the results.
In the example above, you will search by name, but the autocomplete list will display the result of funky_method This wouldn't really make much sense unless you use it with the "id_element" attribute. (See below) Only the object's id and the column you are searching on will be returned in JSON, so if your display_value method requires another parameter, make sure to fetch it with the :extra_data option :hstoreAdded option to support searching in hstore columns. Pass a hash with two keys: e.g :scopesAdded option to use scopes. Pass scopes in an array.
e.g :column_nameBy default autocomplete uses method name as column name. Now it can be specified using column_name options
json encoderAutocomplete uses Yajl as JSON encoder/decoder, but you can specify your own
ViewOn your view, all you have to do is include the attribute autocomplete on the text field using the url to the autocomplete action as the value.
This will generate an HTML tag that looks like:
If you are not using a FormBuilder (form_for) or you just want to include an autocomplete field without the form, you can use the autocomplete_field_tag helper.
Multiple Values Separated by DelimiterTo generate an autocomplete input field that accepts multiple values separated by a given delimiter, add the
NOTE: Setting the Automatically focus on the first autocompleted itemTo have the first item be automatically focused on when the autocomplete menu is shown, add the
Now your autocomplete code is unobtrusive, Rails 3 style. Getting the object idIf you need to use the id of the selected object, you can use the id_element attribute too:
This will update the field with id *#some_element with the id of the selected object. The value for this option can be any jQuery selector. Changing destination elementIf you need to change destination element where the autocomplete box will be appended to, you can use the :append_to option which generates a data-append-to HTML attribute that is used in jQuery autocomplete as append_to attribute. The :append_to option accepts a string containing jQuery selector for destination element:
The previous example would append the autocomplete box containing suggestions to element jQuery('#product_modal'). This is very useful on page where you use various z-indexes and you need to append the box to the topmost element, for example using modal window. Sending extra search fieldsIf you want to send extra fields from your form to the search action, you can use the :fields options which generates a data-autocomplete-fields HTML attribute. The :fields option accepts a hash where the keys represent the Ajax request parameter name and the values represent the jQuery selectors to retrieve the form elements to get the values:
Getting extra object dataIf you need to extra data about the selected object, you can use the :update_elements HTML attribute. The :update_elements attribute accepts a hash where the keys represent the object attribute/column data to use to update and the values are jQuery selectors to retrieve the HTML element to update:
The previous example would fetch the extra attribute slogan and update jQuery('#some_other_element') with the slogan value. Running custom code on selectionA javascript event named railsAutocomplete.select is fired on the input field when a value is selected from the autocomplete drop down. If you need to do something more complex than update fields with data, you can hook into this event, like so:
FormtasticIf you are using Formtastic, you automatically get the autocompleted_input helper on semantic_form_for:
The only difference with the original helper is that you must specify the autocomplete url using the :url option. SimpleFormIf you want to use it with simple_form, all you have to do is use the :as option on the input and set the autocomplete path with the :url option.
CucumberI have created a step to test your autocomplete with Cucumber and Capybara, all you have to do is add the following lines to your env.rb file:
Then you'll have access to the following step:
An example on how to use it:
I have only tested this using Capybara, no idea if it works with something else, to see it in action, check the example app. SteakI have created a helper to test your autocomplete with Steak and Capybara, all you have to do is add the following lines to your acceptance_helper.rb file:
Then you'll have access to the following helper:
An example on how to use it:
I have only tested this using Capybara, no idea if it works with something else. DevelopmentIf you want to make changes to the gem, first install bundler 1.0.0:
And then, install all your dependencies:
Running the test suite
To run all the tests once, simply use
while you're developing, it is recommended that you run
to have the relevant test run every time you save a file. Integration testsIf you make changes or add features to the jQuery part, please make sure you write a cucumber test for it. You can find an example Rails app on the integration folder. You can run the integration tests with the cucumber command while on the integration folder:
Where to test whatIf you're making or tweaking a plugin (such as the formtastic plugin or
simple_form plugin), check out the simple_form_plugin_test for an
example of how to test it as part of the main Thanks toEveryone on this list About the AuthorCrowd Interactive is an American web design and development company that happens to work in Colima, Mexico. We specialize in building and growing online retail stores. We don’t work with everyone – just companies we believe in. Call us today to see if there’s a fit. Find more info here! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论