bookinventory/public/js/app.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-12-21 07:09:34 +00:00
$(document).ready(function(e) {
var authorOptions = {
placeholder: 'Authors',
tags: true,
ajax: {
url: '/authors/search',
dataType: 'json'
// Additional AJAX parameters go here; see the end of this chapter for the full code of this example
}
};
var locationOptions = {
placeholder: 'Location',
tags: true
};
$('#add-form .select2-author').select2(authorOptions);
$('#add-form .select2-location').select2(locationOptions);
$('#add-form').on('keyup', '.barcode_input', function (e) {
if (e.keyCode == 13) {
// Do something
e.preventDefault();
var $row = $(this).closest('.form-row');
$.get('/lookup/' + $(this).val(), function(res) {
$row.find('input[name*=name]').val(res.title);
var $authors = $row.find('.select2-author');
for (var i = 0; i < res.authors.length; i++) {
$authors.append($('<option>', {value: res.authors[i], text: res.authors[i], selected: 'selected'}));
}
$authors.trigger('change');
}, 'json');
var count = emptyRowCount();
console.log('Empty rows:', count);
if (count < 1) {
var $template = $('#add-template'),
$clone = $template.clone();
$clone.find('input[type=text]').val('');
$clone.removeClass('invisible').addClass('form-row');
$row.parent().append($clone);
$clone.find('.barcode_input').focus();
$clone.find('.select2-author').select2(authorOptions);
}
}
});
});
function emptyRowCount() {
var $barcodes = $('#add-form .barcode_input');
var count = 0;
$barcodes.each(function() {
if ($(this).val() == '') {
count++;
}
});
return count;
}