Fix dynamic select2 usage (oops!)
This commit is contained in:
parent
326016dfb5
commit
d06a9ed3f0
7 changed files with 41 additions and 44 deletions
|
@ -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() {
|
||||||
|
|
||||||
|
@ -151,7 +153,7 @@ $(function() {
|
||||||
$(document).tooltip({
|
$(document).tooltip({
|
||||||
selector: '[data-toggle~="tooltip"]',
|
selector: '[data-toggle~="tooltip"]',
|
||||||
});
|
});
|
||||||
|
|
||||||
// See stupidtable.js for initialization of local table sorting
|
// See stupidtable.js for initialization of local table sorting
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
$(function() {
|
$(function() {
|
||||||
$('#stock_changes').on('touchclick', '.destroy_stock_change', function() {
|
$('#stock_changes').on('touchclick', '.destroy_stock_change', function() {
|
||||||
$(this).prev('input').val('1'); // check for destruction
|
$(this).prev('input').val('1'); // check for destruction
|
||||||
|
|
||||||
var stock_change = $(this).closest('tr');
|
var stock_change = $(this).closest('tr');
|
||||||
stock_change.hide(); // do not remove (to ensure destruction)
|
stock_change.hide(); // do not remove (to ensure destruction)
|
||||||
stock_change.removeAttr('id'); // remove id to allow re-adding
|
stock_change.removeAttr('id'); // remove id to allow re-adding
|
||||||
|
@ -17,37 +17,31 @@
|
||||||
mark_article_for_delivery( stock_change.data('id') );
|
mark_article_for_delivery( stock_change.data('id') );
|
||||||
return false;
|
return false;
|
||||||
})
|
})
|
||||||
|
|
||||||
$('#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=[];
|
|
||||||
Select2.util.markMatch(result.text, query.term, markup, escapeMarkup);
|
|
||||||
return markup.join("");
|
|
||||||
}
|
}
|
||||||
}).on('change', function(e) {
|
}).on('select2:select', function(e) {
|
||||||
var selectedArticle = $(e.currentTarget).select2('data');
|
var selectedArticle = e.params.data;
|
||||||
if(!selectedArticle) {
|
if (!selectedArticle) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if('new' == selectedArticle.id) {
|
if('new' == selectedArticle.id) {
|
||||||
$.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,11 +51,11 @@
|
||||||
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;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Subscribe to database changes.
|
// Subscribe to database changes.
|
||||||
// See publish/subscribe design pattern in /doc.
|
// See publish/subscribe design pattern in /doc.
|
||||||
$(document).on('StockArticle#create', function(e) {
|
$(document).on('StockArticle#create', function(e) {
|
||||||
|
@ -72,7 +66,7 @@
|
||||||
contentType: 'application/json; charset=UTF-8'
|
contentType: 'application/json; charset=UTF-8'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$(document).on('StockArticle#update', function(e) {
|
$(document).on('StockArticle#update', function(e) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '#{form_on_stock_article_update_supplier_deliveries_path(@supplier)}',
|
url: '#{form_on_stock_article_update_supplier_deliveries_path(@supplier)}',
|
||||||
|
@ -82,7 +76,7 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function mark_article_for_delivery(stock_article_id) {
|
function mark_article_for_delivery(stock_article_id) {
|
||||||
var articleTr = $('#stock_article_' + stock_article_id);
|
var articleTr = $('#stock_article_' + stock_article_id);
|
||||||
if( is_article_available_for_delivery(stock_article_id) ) {
|
if( is_article_available_for_delivery(stock_article_id) ) {
|
||||||
|
@ -102,7 +96,7 @@
|
||||||
= f.error_notification
|
= f.error_notification
|
||||||
= base_errors f.object
|
= base_errors f.object
|
||||||
= f.association :supplier, :as => :hidden
|
= f.association :supplier, :as => :hidden
|
||||||
|
|
||||||
%h2= t '.title_select_stock_articles'
|
%h2= t '.title_select_stock_articles'
|
||||||
%table#stock_articles_for_adding.table.table-hover.stupidtable
|
%table#stock_articles_for_adding.table.table-hover.stupidtable
|
||||||
%thead
|
%thead
|
||||||
|
@ -118,11 +112,11 @@
|
||||||
- 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}
|
||||||
|
|
||||||
%h2= t '.title_fill_quantities'
|
%h2= t '.title_fill_quantities'
|
||||||
%table.table#stock_changes.stupidtable
|
%table.table#stock_changes.stupidtable
|
||||||
%thead
|
%thead
|
||||||
|
@ -135,7 +129,7 @@
|
||||||
%tbody
|
%tbody
|
||||||
= f.simple_fields_for :stock_changes do |stock_change_form|
|
= f.simple_fields_for :stock_changes do |stock_change_form|
|
||||||
= render :partial => 'stock_change_fields', :locals => {:f => stock_change_form}
|
= render :partial => 'stock_change_fields', :locals => {:f => stock_change_form}
|
||||||
|
|
||||||
%h2= t '.title_finish_delivery'
|
%h2= t '.title_finish_delivery'
|
||||||
= f.input :delivered_on, as: :date_picker
|
= f.input :delivered_on, as: :date_picker
|
||||||
= f.input :note, input_html: {size: '35x4'}
|
= f.input :note, input_html: {size: '35x4'}
|
||||||
|
|
|
@ -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");
|
||||||
|
|
|
@ -45,32 +45,34 @@
|
||||||
$('input[data-units-expected]').each(function() {
|
$('input[data-units-expected]').each(function() {
|
||||||
update_delta(this);
|
update_delta(this);
|
||||||
});
|
});
|
||||||
|
|
||||||
init_add_article('#add_article');
|
init_add_article('#add_article');
|
||||||
});
|
});
|
||||||
|
|
||||||
function init_add_article(sel) {
|
function init_add_article(sel) {
|
||||||
$(sel).removeAttr('disabled').select2({
|
$(sel).removeAttr('disabled').select2({
|
||||||
placeholder: '#{j t('orders.receive.add_article')}',
|
placeholder: '#{j t('orders.receive.add_article')}',
|
||||||
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() {
|
||||||
$('.units_received', $(this).closest('tr')).prop('disabled', false).focus();
|
$('.units_received', $(this).closest('tr')).prop('disabled', false).focus();
|
||||||
$(this).closest('.input-prepend').prop('title', I18n.t('orders.edit_amount.field_unlocked_title'));
|
$(this).closest('.input-prepend').prop('title', I18n.t('orders.edit_amount.field_unlocked_title'));
|
||||||
|
@ -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]
|
||||||
|
|
||||||
|
|
|
@ -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}',
|
||||||
|
|
|
@ -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}',
|
||||||
|
|
Loading…
Reference in a new issue