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_ujs
//= require select2
//= require select2-full
//= require twitter/bootstrap
//= require jquery.tokeninput
//= require bootstrap-datepicker/core
@ -20,6 +20,8 @@
//= require delta_input
//= require recurring_select
$.fn.select2.defaults.set('theme', 'bootstrap');
// Load following statements, when DOM is ready
$(function() {
@ -151,7 +153,7 @@ $(function() {
$(document).tooltip({
selector: '[data-toggle~="tooltip"]',
});
// See stupidtable.js for initialization of local table sorting
});
@ -177,5 +179,3 @@ function highlightRow(checkbox) {
function setHiddenId(text, li) {
$('hidden_id').value = li.id;
}

View file

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

View file

@ -3,7 +3,7 @@
$(function() {
$('#stock_changes').on('touchclick', '.destroy_stock_change', function() {
$(this).prev('input').val('1'); // check for destruction
var stock_change = $(this).closest('tr');
stock_change.hide(); // do not remove (to ensure destruction)
stock_change.removeAttr('id'); // remove id to allow re-adding
@ -17,37 +17,31 @@
mark_article_for_delivery( stock_change.data('id') );
return false;
})
$('#new_stock_article').removeAttr('disabled').select2({
tags: true,
placeholder: '#{t '.create_stock_article'}',
data: #{articles_for_select2(@supplier.articles).to_json},
createSearchChoice: function(term) {
createTag: function(data) {
return {
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=[];
Select2.util.markMatch(result.text, query.term, markup, escapeMarkup);
return markup.join("");
}
}).on('change', function(e) {
var selectedArticle = $(e.currentTarget).select2('data');
if(!selectedArticle) {
}).on('select2:select', function(e) {
var selectedArticle = e.params.data;
if (!selectedArticle) {
return false;
}
if('new' == selectedArticle.id) {
$.ajax({
url: '#{new_stock_article_path}',
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'
});
$('#new_stock_article').select2('data', null);
$('#new_stock_article').val('').trigger('change');
return true;
}
if('' != selectedArticle.id) {
@ -57,11 +51,11 @@
data: {old_article_id: selectedArticle.id},
contentType: 'application/json; charset=UTF-8'
});
$('#new_stock_article').select2('data', null);
$('#new_stock_article').val('').trigger('change');
return true;
}
});
// Subscribe to database changes.
// See publish/subscribe design pattern in /doc.
$(document).on('StockArticle#create', function(e) {
@ -72,7 +66,7 @@
contentType: 'application/json; charset=UTF-8'
});
});
$(document).on('StockArticle#update', function(e) {
$.ajax({
url: '#{form_on_stock_article_update_supplier_deliveries_path(@supplier)}',
@ -82,7 +76,7 @@
});
});
});
function mark_article_for_delivery(stock_article_id) {
var articleTr = $('#stock_article_' + stock_article_id);
if( is_article_available_for_delivery(stock_article_id) ) {
@ -102,7 +96,7 @@
= f.error_notification
= base_errors f.object
= f.association :supplier, :as => :hidden
%h2= t '.title_select_stock_articles'
%table#stock_articles_for_adding.table.table-hover.stupidtable
%thead
@ -118,11 +112,11 @@
- if @supplier.articles.empty?
= link_to t('.create_stock_article'), new_stock_article_path, :remote => true, :class => 'btn'
- else
%input#new_stock_article{:style => 'width: 500px;'}
%select#new_stock_article{:style => 'width: 500px;'}
%tbody
- for article in articles_for_table(@supplier.stock_articles)
= render :partial => 'stock_article_for_adding', :locals => {:article => article}
%h2= t '.title_fill_quantities'
%table.table#stock_changes.stupidtable
%thead
@ -135,7 +129,7 @@
%tbody
= f.simple_fields_for :stock_changes do |stock_change_form|
= render :partial => 'stock_change_fields', :locals => {:f => stock_change_form}
%h2= t '.title_finish_delivery'
= f.input :delivered_on, as: :date_picker
= f.input :note, input_html: {size: '35x4'}

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) } }
%>
(function() {
function update_select2(id, data) {
var ele = $(id);
function update_select2(sel, data) {
var ele = $(sel);
if (!ele.length)
return;
ele.select2("destroy");

View file

@ -45,32 +45,34 @@
$('input[data-units-expected]').each(function() {
update_delta(this);
});
init_add_article('#add_article');
});
function init_add_article(sel) {
$(sel).removeAttr('disabled').select2({
placeholder: '#{j t('orders.receive.add_article')}',
formatNoMatches: function(term) { return '#{j t('.no_articles_available')}';}
// TODO implement adding a new article, like in deliveries
}).on('change', function(e) {
var selectedArticle = $(e.currentTarget).select2('data');
if(!selectedArticle) {
var $input = $(e.target);
var selectedArticleId = $input.val();
if(!selectedArticleId) {
return false;
}
$.ajax({
url: '#{order_order_articles_path(@order)}',
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'
});
$('#add_article').select2('data', null);
}).select2('data', null);
$input.val('').trigger('change');
});
$(sel).val('').trigger('change');
}
function unlock_receive_input_field() {
$('.units_received', $(this).closest('tr')).prop('disabled', false).focus();
$(this).closest('.input-prepend').prop('title', I18n.t('orders.edit_amount.field_unlocked_title'));
@ -99,4 +101,3 @@
%select#add_article{:style => 'width: 500px;'}
- new_article_data.each do |option|
%option{id: "add_article_#{option[:id]}", value: option[:id]}= option[:text]

View file

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

View file

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