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_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() {
|
||||
|
||||
|
@ -177,5 +179,3 @@ function highlightRow(checkbox) {
|
|||
function setHiddenId(text, li) {
|
||||
$('hidden_id').value = li.id;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/*
|
||||
*= require bootstrap_and_overrides
|
||||
*= require select2
|
||||
*= require select2-bootstrap
|
||||
*= require token-input-bootstrappy
|
||||
*= require bootstrap-datepicker
|
||||
*= require list.unlist
|
||||
|
|
|
@ -19,35 +19,29 @@
|
|||
})
|
||||
|
||||
$('#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,7 +51,7 @@
|
|||
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;
|
||||
}
|
||||
});
|
||||
|
@ -118,7 +112,7 @@
|
|||
- 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}
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -55,20 +55,22 @@
|
|||
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() {
|
||||
|
@ -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]
|
||||
|
||||
|
|
|
@ -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}',
|
||||
|
|
|
@ -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}',
|
||||
|
|
Loading…
Reference in a new issue