Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds sortable option to select2 tags input field #423

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/assets/javascripts/activeadmin_addons/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,13 @@
$(el).on("select2:select", onItemAdded);
$(el).on("select2:unselect", onItemRemoved);
$(el).select2(selectOptions);
if ($(el).data("sortable")) addSorting();
function addSorting() {
$(el).next().find("ul.select2-selection__rendered").sortable({
containment: "parent",
update: fillHiddenInput
});
}
function getSelectedItems() {
var choices = $(el).parent("li.input").find(".select2-selection__choice");
return $.map(choices, function(item) {
Expand Down
1 change: 1 addition & 0 deletions app/inputs/tags_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def load_control_attributes
load_data_attr(:model, value: model_name)
load_data_attr(:method, value: method)
load_data_attr(:width, default: "80%")
load_data_attr(:sortable)

if active_record_select?
load_data_attr(:relation, value: true)
Expand Down
9 changes: 9 additions & 0 deletions app/javascript/activeadmin_addons/inputs/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ var initializer = function() {
$(el).on('select2:unselect', onItemRemoved);
$(el).select2(selectOptions);

if ($(el).data('sortable')) addSorting()

function addSorting() {
$(el).next().find('ul.select2-selection__rendered').sortable({
containment: 'parent',
update: fillHiddenInput
})
}

function getSelectedItems() {
var choices = $(el).parent('li.input').find('.select2-selection__choice');
return $.map(choices, function(item) {
Expand Down
1 change: 1 addition & 0 deletions docs/select2_tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ f.input :performer_ids, as: :tags, collection: Performer.all, display_name: :ful
* `display_name`: **(optional)** You can pass an optional `display_name` to set the attribute (or method) to show results on the select. It **defaults to**: `name`
* `value`: **(optional)** You can pass an optional `value` to set the attribute (or method) to use when an item is selected. It **defaults to**: `id`
* `width`: **(optional)** You can set the select input width (px or %).
* `sortable`: **(optional)** It **defaults** to: `false`
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module SelectHelpers
def array_to_select_options
selected_values = input_value.is_a?(Array) ? input_value : input_value.to_s.split(",")
array = collection.map(&:to_s) + selected_values
array.sort.map do |value|
array.map do |value|
option = { id: value, text: value }
option[:selected] = "selected" if selected_values.include?(value)
option
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "activeadmin_addons",
"version": "1.0.0",
"version": "1.0.1",
"description": "Set of addons to help with the activeadmin ui",
"main": "src/all.js",
"files": [
Expand Down