Fix dynamic select2 usage (oops!)

This commit is contained in:
wvengen 2016-08-12 15:54:28 +02:00
parent 326016dfb5
commit d06a9ed3f0
7 changed files with 41 additions and 44 deletions

View file

@ -1,6 +1,6 @@
//= require jquery //= require jquery
//= require jquery_ujs //= require jquery_ujs
//= require select2 //= require select2-full
//= require twitter/bootstrap //= require twitter/bootstrap
//= require jquery.tokeninput //= require jquery.tokeninput
//= require bootstrap-datepicker/core //= require bootstrap-datepicker/core
@ -20,6 +20,8 @@
//= require delta_input //= require delta_input
//= require recurring_select //= require recurring_select
$.fn.select2.defaults.set('theme', 'bootstrap');
// Load following statements, when DOM is ready // Load following statements, when DOM is ready
$(function() { $(function() {
@ -177,5 +179,3 @@ function highlightRow(checkbox) {
function setHiddenId(text, li) { function setHiddenId(text, li) {
$('hidden_id').value = li.id; $('hidden_id').value = li.id;
} }

View file

@ -1,6 +1,7 @@
/* /*
*= require bootstrap_and_overrides *= require bootstrap_and_overrides
*= require select2 *= require select2
*= require select2-bootstrap
*= require token-input-bootstrappy *= require token-input-bootstrappy
*= require bootstrap-datepicker *= require bootstrap-datepicker
*= require list.unlist *= require list.unlist

View file

@ -19,24 +19,18 @@
}) })
$('#new_stock_article').removeAttr('disabled').select2({ $('#new_stock_article').removeAttr('disabled').select2({
tags: true,
placeholder: '#{t '.create_stock_article'}', placeholder: '#{t '.create_stock_article'}',
data: #{articles_for_select2(@supplier.articles).to_json}, data: #{articles_for_select2(@supplier.articles).to_json},
createSearchChoice: function(term) { createTag: function(data) {
return { return {
id: 'new', id: 'new',
text: term name: data.term, // store for ajax request
text: data.term + ' (#{t '.create_from_blank'})'
}; };
},
formatResult: function(result, container, query, escapeMarkup) {
if(result.id == 'new') {
return result.text + ' (#{t '.create_from_blank'})';
} }
var markup=[]; }).on('select2:select', function(e) {
Select2.util.markMatch(result.text, query.term, markup, escapeMarkup); var selectedArticle = e.params.data;
return markup.join("");
}
}).on('change', function(e) {
var selectedArticle = $(e.currentTarget).select2('data');
if (!selectedArticle) { if (!selectedArticle) {
return false; return false;
} }
@ -44,10 +38,10 @@
$.ajax({ $.ajax({
url: '#{new_stock_article_path}', url: '#{new_stock_article_path}',
type: 'get', type: 'get',
data: {stock_article: {name: selectedArticle.text, supplier_id: #{@supplier.id}}}, data: {stock_article: {name: selectedArticle.name, supplier_id: #{@supplier.id}}},
contentType: 'application/json; charset=UTF-8' contentType: 'application/json; charset=UTF-8'
}); });
$('#new_stock_article').select2('data', null); $('#new_stock_article').val('').trigger('change');
return true; return true;
} }
if('' != selectedArticle.id) { if('' != selectedArticle.id) {
@ -57,7 +51,7 @@
data: {old_article_id: selectedArticle.id}, data: {old_article_id: selectedArticle.id},
contentType: 'application/json; charset=UTF-8' contentType: 'application/json; charset=UTF-8'
}); });
$('#new_stock_article').select2('data', null); $('#new_stock_article').val('').trigger('change');
return true; return true;
} }
}); });
@ -118,7 +112,7 @@
- if @supplier.articles.empty? - if @supplier.articles.empty?
= link_to t('.create_stock_article'), new_stock_article_path, :remote => true, :class => 'btn' = link_to t('.create_stock_article'), new_stock_article_path, :remote => true, :class => 'btn'
- else - else
%input#new_stock_article{:style => 'width: 500px;'} %select#new_stock_article{:style => 'width: 500px;'}
%tbody %tbody
- for article in articles_for_table(@supplier.stock_articles) - for article in articles_for_table(@supplier.stock_articles)
= render :partial => 'stock_article_for_adding', :locals => {:article => article} = render :partial => 'stock_article_for_adding', :locals => {:article => article}

View file

@ -3,8 +3,8 @@ deliveries_data = @deliveries_collection.map {|d| { id: d.id, text: format_deliv
orders_data = @orders_collection.map {|o| { id: o.id, text: format_order_item(o) } } orders_data = @orders_collection.map {|o| { id: o.id, text: format_order_item(o) } }
%> %>
(function() { (function() {
function update_select2(id, data) { function update_select2(sel, data) {
var ele = $(id); var ele = $(sel);
if (!ele.length) if (!ele.length)
return; return;
ele.select2("destroy"); ele.select2("destroy");

View file

@ -55,20 +55,22 @@
formatNoMatches: function(term) { return '#{j t('.no_articles_available')}';} formatNoMatches: function(term) { return '#{j t('.no_articles_available')}';}
// TODO implement adding a new article, like in deliveries // TODO implement adding a new article, like in deliveries
}).on('change', function(e) { }).on('change', function(e) {
var selectedArticle = $(e.currentTarget).select2('data'); var $input = $(e.target);
if(!selectedArticle) { var selectedArticleId = $input.val();
if(!selectedArticleId) {
return false; return false;
} }
$.ajax({ $.ajax({
url: '#{order_order_articles_path(@order)}', url: '#{order_order_articles_path(@order)}',
type: 'post', type: 'post',
data: JSON.stringify({order_article: {article_id: selectedArticle.id}}), data: JSON.stringify({order_article: {article_id: selectedArticleId}}),
contentType: 'application/json; charset=UTF-8' contentType: 'application/json; charset=UTF-8'
}); });
$('#add_article').select2('data', null); $input.val('').trigger('change');
}).select2('data', null); });
$(sel).val('').trigger('change');
} }
function unlock_receive_input_field() { function unlock_receive_input_field() {
@ -99,4 +101,3 @@
%select#add_article{:style => 'width: 500px;'} %select#add_article{:style => 'width: 500px;'}
- new_article_data.each do |option| - new_article_data.each do |option|
%option{id: "add_article_#{option[:id]}", value: option[:id]}= option[:text] %option{id: "add_article_#{option[:id]}", value: option[:id]}= option[:text]

View file

@ -53,7 +53,8 @@
$(document).on('#update_articles_summary', function() { $(document).on('#update_articles_summary', function() {
// initialize add ordergroup control // initialize add ordergroup control
$('#group_order_article_ordergroup_id').select2().select2('data', null); $('#group_order_article_ordergroup_id').select2();
$('#group_order_article_ordergroup_id').val('').trigger('change');
}); });
$(function() { $(function() {
@ -64,7 +65,7 @@
$(document).on('GroupOrderArticle#create', function(e) { $(document).on('GroupOrderArticle#create', function(e) {
var base_unit = $('#articles_by_articles').data('base-unit'); var base_unit = $('#articles_by_articles').data('base-unit');
// reset selection // reset selection
$('#group_order_article_ordergroup_id').select2('data', null); $('#group_order_article_ordergroup_id').val('').trigger('change');
// update table // update table
$.ajax({ $.ajax({
url: '#{show_on_group_order_article_create_current_orders_articles_path}', url: '#{show_on_group_order_article_create_current_orders_articles_path}',

View file

@ -36,7 +36,7 @@
$(document).on('GroupOrderArticle#create', function(e) { $(document).on('GroupOrderArticle#create', function(e) {
var base_unit = $('#articles_by_groups_table').data('base-unit'); var base_unit = $('#articles_by_groups_table').data('base-unit');
// reset selection // reset selection
$('#group_order_article_order_article_id').select2('data', null); $('#group_order_article_order_article_id').val('').trigger('change');
// update table // update table
$.ajax({ $.ajax({
url: '#{show_on_group_order_article_create_current_orders_ordergroups_path}', url: '#{show_on_group_order_article_create_current_orders_ordergroups_path}',