After the deployment we came across a bug in the custom js code.
It just doesn't work...
$(document).ready(function() {
(function($){
$(document).on('autocompleteLightInitialize', '[data-autocomplete-light-function=select2]', function() {
/*** Nothing is running here ***/
$(this).on('select2:select', function (e) {
var data = e.params.data;
//...
});
});
})(django.jQuery);
});
This is only happening when debug = False.
Comparing what changes when loading a development page and a production page, I found this condition:
I think there may be some difference between the .min and full versions.
Avoiding the use of _.min.js_ files, it starts to work properly.
extra = '' #if settings.DEBUG else '.min'
Environment:
Django 2.2.14
Django-autocomplete-light 3.7.0.dev0
3.7.0.dev0 includes a major rewrite of the JS initialization. Please try this...
document.addEventListener('dal-init-function', function () {
yl.registerFunction( 'your_autocomplete_function', function ($, element) {
var $element = $(element);
$element.on('select2:select', function (e) {
var data = e.params.data;
//...
});
});
});
You can find more in the docs... https://github.com/yourlabs/django-autocomplete-light/blob/master/docs/tutorial.rst#overriding-javascript-code
hai guys how to change the selection of dal select2 widget from jquery in 3.7.0.dev0. I tried $(element).val(id).trigger('change') but not changing the selection. I can access the element but the value is not being changed.
@nikijoe can you provide a little more of your JS. It is difficult to understand the context of what you are attempting with just that one line.
Hai @danielmorell ,
I have an issue like i am using django-autocomplete-light.ModelSelect2 widget in django form
class TransProduct(models.Model):
barcode = models.CharField(max_length=50, null=True, blank=True)
product = models.ForeignKey(Product, on_delete=models.PROTECT)
unit = models.CharField(max_length=10, null=True, blank=True)
rate = models.DecimalField(max_digits=19, decimal_places=2, default=0)
class TransProductForm(forms.ModelForm):
'product':autocomplete.ModelSelect2(url='/trans/product-autocomplete' ),}
class Meta:
model = TransProduct
fields = ('__all__')
to access a product field which is a ForeignKey in the bill inline form. When the product which is dal field is typed in it is doing autocomplete function and rest fields like rate, unit etc. are populated using jquery llike
$('[id$=-product]').change(function() {
var currow = $(this).closest('tr');
var id = currow.find('[id$=-product]').val();
$.ajax({
url: '/trans/load_product_data',
data: {
'id': id
},
success: function (data) {
currow.find("[id$=-barcode]").val(data.barcode);
currow.find("[id$=-unit]").val(data.unit);
currow.find("[id$=-rate]").val(data.rate);
}
});
});
but if barcode is changed it is not updating the dal product field with the code.
$("[id$=-barcode]").change(function (e) {
var currow = $(this).closest('tr');
var barcode = currow.find("[id$=-barcode]").val();
$.ajax({
url: '/trans/load_barcode_data',
data: {
'barcode': barcode
},
success: function (data) {
currow.find('[id$=-product]').val(data.id).trigger('change');
currow.find("[id$=-unit]").val(data.unit);
currow.find("[id$=-rate]").val(data.rate);
}
});
});
my issue in short is with this line
currow.find('[id$=-product]').val(data.id).trigger('change');
not updating my product dal field. But the other things like unit and rate is updated.
Using Django-autocomplete-light 3.7.0.dev0 version.
jquery-3.4.1.min.js
Django 3.0.8
Python 3.8.0
Hope everything is clear.
Regards,
Nijo
There really is a difference in the content of these two files, at least on the name of the listened event :
3.7.0.dev0 includes a major rewrite of the JS initialization. Please try this...
Thanks for your reply @danielmorell, but I tried to follow your example and it isn't working for me in the development environment.
As @challet said above, there is a difference in the content of these two files, because what works with the _select2.js_ file should also work with the _select2.min.js_ file, right?
There really is a difference in the content of these two files, at least on the name of the listened event :
@danielmorell that's a real bug right ? How are you not affected ?
@gustavo-sdo Are you seeing any console errors when using select2.min.js?
I have read through both the minified version and non-minified version, and don't see anything that should cause it to function differently.
I will see if I can reproduce the bug.
There was a difference in master prior to 6 days ago.
This is not the case anymore : https://github.com/yourlabs/django-autocomplete-light/commit/3b71b0a923097ba7ffc20d3306db6224bc52d285
Please confirm that the bug is still present with DAL 3.8.0 because I do not believe this is the case.
Thank you for your support
Have a great day
@jpic That seems like a likely answer. Do you know if npm build was run as part of PR #1171? It doesn't look like those changes are reflected in the current select2.min.js?
I have run npm build and the generated files differed.
Please try with 3.8.1 release that I just issued.
Thanks @jpic!
The two changes from #1171 are now in the minified version.
@gustavo-sdo can you test with 3.8.1 and let us know if it is working on your end?
can you test with 3.8.1 and let us know if it is working on your end?
@danielmorell, @jpic
Great, it's working properly with DAL 3.8.1.
For both versions, complete and minified file.
can you test with 3.8.1 and let us know if it is working on your end?
@danielmorell, @jpic
Great, it's working properly with DAL 3.8.1.
For both versions, complete and minified file.
Awesome! I am delighted to hear it!
Most helpful comment
Awesome! I am delighted to hear it!