$(document).on('turbolinks:load', function() { if ($('body.admins-salesman-channels-index-page').length > 0) { // ============= 添加销售人员 ============== var $addMemberModal = $('.admin-add-salesman-channel-user-modal'); var searchscForm = $(".saleman-channel-list-form .search-form"); var scFormUrl = searchscForm.data('data-search-form-url'); var $addMemberForm = $addMemberModal.find('.admin-add-salesman-channel-user-form'); var $memberSelect = $addMemberModal.find('.salesman-channel-user-select'); var $salesmanIdInput = $('.salesman-channel-list-form').find(".btn-primary"); var $start_time = $('#start_time'); var $end_time = $('#end_time'); $addMemberModal.on('show.bs.modal', function(event){ var $link = $(event.relatedTarget); // var salesmanId = $link.data('salesman_id'); // $salesmanIdInput.val(salesmanId); $memberSelect.select2('val', ' '); }); $memberSelect.select2({ theme: 'bootstrap4', placeholder: '请输入要添加的单位', multiple: true, minimumInputLength: 1, ajax: { delay: 500, url: '/admins/schools', dataType: 'json', data: function(params){ return { keyword: params.term }; }, processResults: function(data){ return { results: data.schools } } }, templateResult: function (item) { if(!item.id || item.id === '') return ''; return $("" + item.name + ""); }, templateSelection: function(item){ if (item.id) { } return item.name || ''; } }); $addMemberModal.on('click', '.submit-btn', function(){ $addMemberForm.find('.error').html(''); // var salesmanId = $salesmanIdInput.val(); var memberIds = $memberSelect.val(); if (memberIds && memberIds.length > 0) { alert(start_time); $.ajax({ method: 'POST', dataType: 'json', url: '/admins/salesman_channels/batch_add', data: { salesman_id: $salesmanIdInput.data("salesman-id"), school_ids: memberIds }, success: function(){ $.notify({ message: '创建成功' }); $addMemberModal.modal('hide'); setTimeout(function(){ window.location.reload(); }, 500); }, error: function(res){ var data = res.responseJSON; $form.find('.error').html(data.message); } }); } else { $addMemberModal.modal('hide'); } }); // 时间跨度 var baseOptions = { autoclose: true, language: 'zh-CN', format: 'yyyy-mm-dd', startDate: '2017-04-01' }; var defineDateRangeSelect = function(element){ var options = $.extend({inputs: $(element).find('.start-date, .end-date')}, baseOptions); $(element).datepicker(options); $(element).find('.start-date').datepicker().on('changeDate', function(e){ $(element).find('.end-date').datepicker('setStartDate', e.date); }); }; defineDateRangeSelect('.grow-date-input-daterange'); // 区间搜索 searchscForm.on('click', ".search-btn", function(){ submitForm(); }); var submitForm = function(){ var form = searchscForm; var url = "/admins/salesman_channels"; $.ajax({ url: url, data: form.serialize(), dataType: "script" }) }; } });