您需要以某种方式定义更新下拉列表。
$('.certificate-list')
返回页面上的所有证书下拉列表。为此,您可以使用强制签名的ID。
- @mandatory_signatures.each do |mandatory_signature|
.form-group.row.mt-4
.col-sm-5
%label Choose
.centered-vertical
= select_tag '', options_from_collection_for_select(find_users_with_certificate_for(mandatory_signature), :id, :full_name), { include_blank: "Select User", class: 'form-control select2 find-user-certificate', style: 'width: 100%;', data: { mandatory: mandatory_signature.id } }
.col-sm-5
%label Choose Certificate
.centered-vertical
= select_tag nil, "", { include_blank: "Select Certificate", class: "form-control select2 certificate-list js-#{ mandatory_signature.id }", style: 'width: 100%;' }
$('.find-user-certificate').change(function() {
let user_id = $(this).val();
let mandatory_id = $(this).data('mandatory')
getUserCertificates(user_id, mandatory_id);
});
function getUserCertificates(userId, mandatoryId) {
let hostname = window.location.origin;
$.ajax({
url: hostname + '/users/' + userId + '/certificates',
dataType: 'json',
method: 'get',
success: function(data) {
options = '';
data.forEach(function(object) {
options += `<option value=${object.id}>${object.name}</option>`;
});
$('.certificate-list.js-' + mandatoryId).empty();
$('.certificate-list.js-' + mandatoryId).append(options);
}
});
}