课程体系的新增和编辑弹框均支持回车提交、并增加提示信息

dev_video
cxt 5 years ago
parent 94a711d8e0
commit ff919bddae

@ -1,31 +1,42 @@
$(document).on('turbolinks:load', function() { $(document).on('turbolinks:load', function () {
$('.admin-modal-container').on('show.bs.modal', '.modal.admin-edit-discipline-modal', function(){ $('.admin-modal-container').on('show.bs.modal', '.modal.admin-edit-discipline-modal', function () {
var $modal = $('.modal.admin-edit-discipline-modal'); var $modal = $('.modal.admin-edit-discipline-modal');
var $form = $modal.find('form.admin-edit-discipline-form'); var $form = $modal.find('form.admin-edit-discipline-form');
$form.validate({ $form.validate({
errorElement: 'span', errorElement: 'span',
errorClass: 'danger text-danger', errorClass: 'danger text-danger',
rules: { rules: {
'discipline[name]': { 'discipline[name]': {
required: true, required: true,
maxlength: 20 maxlength: 20
} }
} }
}); });
$modal.on('click', '.submit-btn', function(){ $modal.on('click', '.submit-btn', submit_edit_form);
$form.find('.error').html('');
var url = $form.attr('action');
if ($form.valid()) { $form.find("#discipline_name").keydown(function (e) {
$.ajax({ var ev = e || event;
method: 'PATCH', var keycode = ev.which || ev.keyCode;
dataType: 'script', if (keycode == 13) {
url: url, submit_edit_form();
data: $form.serialize() return false;
}
}); });
}
function submit_edit_form() {
$form.find('.error').html('');
var url = $form.attr('action');
if ($form.valid()) {
$.ajax({
method: 'PATCH',
dataType: 'script',
url: url,
data: $form.serialize()
});
}
}
}); });
});
}); });

@ -1,31 +1,43 @@
$(document).on('turbolinks:load', function() { $(document).on('turbolinks:load', function () {
$('.admin-modal-container').on('show.bs.modal', '.modal.admin-edit-sub-discipline-modal', function(){ $('.admin-modal-container').on('show.bs.modal', '.modal.admin-edit-sub-discipline-modal', function () {
var $modal = $('.modal.admin-edit-sub-discipline-modal'); var $modal = $('.modal.admin-edit-sub-discipline-modal');
var $form = $modal.find('form.admin-edit-sub-discipline-form'); var $form = $modal.find('form.admin-edit-sub-discipline-form');
$form.validate({ $form.validate({
errorElement: 'span', errorElement: 'span',
errorClass: 'danger text-danger', errorClass: 'danger text-danger',
rules: { rules: {
'sub_discipline[name]': { 'sub_discipline[name]': {
required: true, required: true,
maxlength: 20 maxlength: 20
} }
} }
}); });
$modal.on('click', '.submit-btn', function(){ $modal.on('click', '.submit-btn', submit_edit_form);
$form.find('.error').html('');
var url = $form.attr('action');
if ($form.valid()) { $form.find("#sub_discipline_name").keydown(function (e) {
$.ajax({ var ev = e || event;
method: 'PATCH', var keycode = ev.which || ev.keyCode;
dataType: 'script', if (keycode == 13) {
url: url, submit_edit_form();
data: $form.serialize() return false;
}
}); });
}
function submit_edit_form() {
$form.find('.error').html('');
var url = $form.attr('action');
if ($form.valid()) {
$.ajax({
method: 'PATCH',
dataType: 'script',
url: url,
data: $form.serialize()
});
}
}
}); });
});
}); });

@ -1,31 +1,42 @@
$(document).on('turbolinks:load', function() { $(document).on('turbolinks:load', function () {
$('.admin-modal-container').on('show.bs.modal', '.modal.admin-edit-tag-discipline-modal', function(){ $('.admin-modal-container').on('show.bs.modal', '.modal.admin-edit-tag-discipline-modal', function () {
var $modal = $('.modal.admin-edit-tag-discipline-modal'); var $modal = $('.modal.admin-edit-tag-discipline-modal');
var $form = $modal.find('form.admin-edit-tag-discipline-form'); var $form = $modal.find('form.admin-edit-tag-discipline-form');
$form.validate({ $form.validate({
errorElement: 'span', errorElement: 'span',
errorClass: 'danger text-danger', errorClass: 'danger text-danger',
rules: { rules: {
'tag_discipline[name]': { 'tag_discipline[name]': {
required: true, required: true,
maxlength: 20 maxlength: 20
} }
} }
}); });
$modal.on('click', '.submit-btn', function(){ $modal.on('click', '.submit-btn', submit_edit_form);
$form.find('.error').html('');
var url = $form.attr('action');
if ($form.valid()) { $form.find("#tag_discipline_name").keydown(function (e) {
$.ajax({ var ev = e || event;
method: 'PATCH', var keycode = ev.which || ev.keyCode;
dataType: 'script', if (keycode == 13) {
url: url, submit_edit_form();
data: $form.serialize() return false;
}
}); });
}
function submit_edit_form() {
$form.find('.error').html('');
var url = $form.attr('action');
if ($form.valid()) {
$.ajax({
method: 'PATCH',
dataType: 'script',
url: url,
data: $form.serialize()
});
}
}
}); });
});
}); });

@ -1,65 +1,76 @@
$(document).on('turbolinks:load', function() { $(document).on('turbolinks:load', function () {
if ($('body.admins-sub-disciplines-index-page').length > 0) { if ($('body.admins-sub-disciplines-index-page').length > 0) {
// ============== 新建 =============== // ============== 新建 ===============
var $modal = $('.modal.admin-create-sub-discipline-modal'); var $modal = $('.modal.admin-create-sub-discipline-modal');
var $form = $modal.find('form.admin-create-sub-discipline-form'); var $form = $modal.find('form.admin-create-sub-discipline-form');
var $nameInput = $form.find('input[name="name"]'); var $nameInput = $form.find('input[name="name"]');
$form.validate({ $form.validate({
errorElement: 'span', errorElement: 'span',
errorClass: 'danger text-danger', errorClass: 'danger text-danger',
rules: { rules: {
name: { name: {
required: true required: true
} }
} }
}); });
// modal ready fire // modal ready fire
$modal.on('show.bs.modal', function () { $modal.on('show.bs.modal', function () {
$nameInput.val(''); $nameInput.val('');
}); });
$modal.on('click', '.submit-btn', function(){ $modal.on('click', '.submit-btn', submit_create_form);
$form.find('.error').html('');
if ($form.valid()) { $nameInput.keydown(function (e) {
var url = $form.data('url'); var ev = e || event;
var keycode = ev.which || ev.keyCode;
if (keycode == 13) {
submit_create_form();
return false;
}
});
$.ajax({ function submit_create_form() {
method: 'POST', $form.find('.error').html('');
dataType: 'json',
url: url,
data: $form.serialize(),
success: function(){
$.notify({ message: '创建成功' });
$modal.modal('hide');
setTimeout(function(){ if ($form.valid()) {
window.location.reload(); var url = $form.data('url');
}, 500);
},
error: function(res){
var data = res.responseJSON;
$form.find('.error').html(data.message);
}
});
}
});
$(".sub-discipline-list-container").on("change", '.sub-discipline-source-form', function () { $.ajax({
var s_id = $(this).attr("data-id"); method: 'POST',
var s_value = $(this).val(); dataType: 'json',
var s_name = $(this).attr("name"); url: url,
var json = {}; data: $form.serialize(),
json[s_name] = s_value; success: function () {
$.ajax({ $.notify({message: '创建成功'});
url: "/admins/sub_disciplines/" + s_id, $modal.modal('hide');
type: "PUT",
dataType:'script', setTimeout(function () {
data: json window.location.reload();
}); }, 500);
}); },
} error: function (res) {
var data = res.responseJSON;
$form.find('.error').html(data.message);
}
});
}
}
$(".sub-discipline-list-container").on("change", '.sub-discipline-source-form', function () {
var s_id = $(this).attr("data-id");
var s_value = $(this).val();
var s_name = $(this).attr("name");
var json = {};
json[s_name] = s_value;
$.ajax({
url: "/admins/sub_disciplines/" + s_id,
type: "PUT",
dataType: 'script',
data: json
});
});
}
}); });

@ -1,65 +1,76 @@
$(document).on('turbolinks:load', function() { $(document).on('turbolinks:load', function () {
if ($('body.admins-tag-disciplines-index-page').length > 0) { if ($('body.admins-tag-disciplines-index-page').length > 0) {
// ============== 新建 =============== // ============== 新建 ===============
var $modal = $('.modal.admin-create-tag-discipline-modal'); var $modal = $('.modal.admin-create-tag-discipline-modal');
var $form = $modal.find('form.admin-create-tag-discipline-form'); var $form = $modal.find('form.admin-create-tag-discipline-form');
var $nameInput = $form.find('input[name="name"]'); var $nameInput = $form.find('input[name="name"]');
$form.validate({ $form.validate({
errorElement: 'span', errorElement: 'span',
errorClass: 'danger text-danger', errorClass: 'danger text-danger',
rules: { rules: {
name: { name: {
required: true required: true
} }
} }
}); });
// modal ready fire // modal ready fire
$modal.on('show.bs.modal', function () { $modal.on('show.bs.modal', function () {
$nameInput.val(''); $nameInput.val('');
}); });
$modal.on('click', '.submit-btn', function(){ $modal.on('click', '.submit-btn', submit_create_form);
$form.find('.error').html('');
if ($form.valid()) { $nameInput.keydown(function (e) {
var url = $form.data('url'); var ev = e || event;
var keycode = ev.which || ev.keyCode;
if (keycode == 13) {
submit_create_form();
return false;
}
});
$.ajax({ function submit_create_form() {
method: 'POST', $form.find('.error').html('');
dataType: 'json',
url: url,
data: $form.serialize(),
success: function(){
$.notify({ message: '创建成功' });
$modal.modal('hide');
setTimeout(function(){ if ($form.valid()) {
window.location.reload(); var url = $form.data('url');
}, 500);
},
error: function(res){
var data = res.responseJSON;
$form.find('.error').html(data.message);
}
});
}
});
$(".tag-discipline-list-container").on("change", '.tag-discipline-source-form', function () { $.ajax({
var s_id = $(this).attr("data-id"); method: 'POST',
var s_value = $(this).val(); dataType: 'json',
var s_name = $(this).attr("name"); url: url,
var json = {}; data: $form.serialize(),
json[s_name] = s_value; success: function () {
$.ajax({ $.notify({message: '创建成功'});
url: "/admins/tag_disciplines/" + s_id, $modal.modal('hide');
type: "PUT",
dataType:'script', setTimeout(function () {
data: json window.location.reload();
}); }, 500);
}); },
} error: function (res) {
var data = res.responseJSON;
$form.find('.error').html(data.message);
}
});
}
}
$(".tag-discipline-list-container").on("change", '.tag-discipline-source-form', function () {
var s_id = $(this).attr("data-id");
var s_value = $(this).val();
var s_name = $(this).attr("name");
var json = {};
json[s_name] = s_value;
$.ajax({
url: "/admins/tag_disciplines/" + s_id,
type: "PUT",
dataType: 'script',
data: json
});
});
}
}); });

@ -16,19 +16,23 @@ class Admins::DisciplinesController < Admins::BaseController
end end
def update def update
if params[:discipline] && params[:discipline][:name].present? begin
name = params[:discipline][:name].to_s.strip if params[:discipline] && params[:discipline][:name]
current_discipline.update_attributes!(name: name) name = params[:discipline][:name].to_s.strip
else current_discipline.update_attributes!(name: name)
ActiveRecord::Base.transaction do else
current_discipline.update_attributes!(setting_params) ActiveRecord::Base.transaction do
current_discipline.sub_disciplines.each do |sub| current_discipline.update_attributes!(setting_params)
sub.tag_disciplines.each do |tag| current_discipline.sub_disciplines.each do |sub|
tag.update_attributes!(setting_params) sub.tag_disciplines.each do |tag|
tag.update_attributes!(setting_params)
end
sub.update_attributes!(setting_params)
end end
sub.update_attributes!(setting_params)
end end
end end
rescue Exception => e
@message = e.message
end end
@disciplines = Discipline.all @disciplines = Discipline.all
end end

@ -7,6 +7,7 @@ class Admins::SubDisciplinesController < Admins::BaseController
def create def create
name = params[:name].to_s.strip name = params[:name].to_s.strip
return render_error('名称不能为空') if name.blank?
return render_error('名称重复') if current_discipline.sub_disciplines.where(name: name).exists? return render_error('名称重复') if current_discipline.sub_disciplines.where(name: name).exists?
SubDiscipline.create!(name: name, discipline_id: current_discipline.id) SubDiscipline.create!(name: name, discipline_id: current_discipline.id)
render_ok render_ok
@ -17,16 +18,20 @@ class Admins::SubDisciplinesController < Admins::BaseController
end end
def update def update
if params[:sub_discipline] && params[:sub_discipline][:name].present? begin
name = params[:sub_discipline][:name].to_s.strip if params[:sub_discipline] && params[:sub_discipline][:name]
current_sub_discipline.update_attributes!(name: name) name = params[:sub_discipline][:name].to_s.strip
else current_sub_discipline.update_attributes!(name: name)
ActiveRecord::Base.transaction do else
current_sub_discipline.update_attributes!(setting_params) ActiveRecord::Base.transaction do
current_sub_discipline.tag_disciplines.each do |tag| current_sub_discipline.update_attributes!(setting_params)
tag.update_attributes!(setting_params) current_sub_discipline.tag_disciplines.each do |tag|
tag.update_attributes!(setting_params)
end
end end
end end
rescue Exception => e
@message = e.message
end end
@sub_disciplines = current_sub_discipline.discipline&.sub_disciplines @sub_disciplines = current_sub_discipline.discipline&.sub_disciplines
end end

@ -17,11 +17,15 @@ class Admins::TagDisciplinesController < Admins::BaseController
end end
def update def update
if params[:tag_discipline] && params[:tag_discipline][:name].present? begin
name = params[:tag_discipline][:name].to_s.strip if params[:tag_discipline] && params[:tag_discipline][:name]
current_tag_discipline.update_attributes!(name: name) name = params[:tag_discipline][:name].to_s.strip
else current_tag_discipline.update_attributes!(name: name)
current_tag_discipline.update_attributes!(setting_params) else
current_tag_discipline.update_attributes!(setting_params)
end
rescue Exception => e
@message = e.message
end end
@tag_disciplines = current_tag_discipline.sub_discipline&.tag_disciplines @tag_disciplines = current_tag_discipline.sub_discipline&.tag_disciplines
end end

@ -2,8 +2,6 @@ class TagDisciplinesController < ApplicationController
before_action :require_login before_action :require_login
def create def create
tip_exception("请输入知识点") if params[:name].blank?
tip_exception("输入字符长度限制在15个以内") if params[:name].length > 15
sub_discipline = SubDiscipline.find_by!(id: params[:sub_discipline_id]) sub_discipline = SubDiscipline.find_by!(id: params[:sub_discipline_id])
tip_exception("重复的知识点") if sub_discipline.tag_disciplines.exists?(name: params[:name].to_s.strip) tip_exception("重复的知识点") if sub_discipline.tag_disciplines.exists?(name: params[:name].to_s.strip)
tag_discipline = TagDiscipline.create!(name: params[:name].to_s.strip, sub_discipline: sub_discipline, user_id: current_user.id) tag_discipline = TagDiscipline.create!(name: params[:name].to_s.strip, sub_discipline: sub_discipline, user_id: current_user.id)

@ -4,4 +4,7 @@ class Discipline < ApplicationRecord
has_many :shixun_sub_disciplines, -> { where("shixun = 1") }, class_name: "SubDiscipline" has_many :shixun_sub_disciplines, -> { where("shixun = 1") }, class_name: "SubDiscipline"
has_many :subject_sub_disciplines, -> { where("subject = 1") }, class_name: "SubDiscipline" has_many :subject_sub_disciplines, -> { where("subject = 1") }, class_name: "SubDiscipline"
has_many :question_sub_disciplines, -> { where("question = 1") }, class_name: "SubDiscipline" has_many :question_sub_disciplines, -> { where("question = 1") }, class_name: "SubDiscipline"
validates_presence_of :name
end end

@ -7,4 +7,7 @@ class SubDiscipline < ApplicationRecord
has_many :shixun_tag_disciplines, -> { where("shixun = 1") }, class_name: "TagDiscipline" has_many :shixun_tag_disciplines, -> { where("shixun = 1") }, class_name: "TagDiscipline"
has_many :subject_tag_disciplines, -> { where("subject = 1") }, class_name: "TagDiscipline" has_many :subject_tag_disciplines, -> { where("subject = 1") }, class_name: "TagDiscipline"
has_many :question_tag_disciplines, -> { where("question = 1") }, class_name: "TagDiscipline" has_many :question_tag_disciplines, -> { where("question = 1") }, class_name: "TagDiscipline"
validates_presence_of :name
end end

@ -4,6 +4,7 @@ class TagDiscipline < ApplicationRecord
has_many :tag_discipline_containers, dependent: :destroy has_many :tag_discipline_containers, dependent: :destroy
validates_presence_of :name validates_presence_of :name
validates :name, length: { maximum: 15, too_long: "不能超过15个字符" }
def discipline def discipline
sub_discipline&.discipline sub_discipline&.discipline

@ -1,2 +1,6 @@
<% if @message.present? %>
$.notify({ message: "<%= @message %>" });
<% else %>
$('.modal.admin-edit-discipline-modal').modal("hide"); $('.modal.admin-edit-discipline-modal').modal("hide");
$(".discipline-list-container").html("<%= j(render :partial => 'admins/disciplines/shared/list') %>"); $(".discipline-list-container").html("<%= j(render :partial => 'admins/disciplines/shared/list') %>");
<% end %>

@ -1,2 +1,6 @@
<% if @message.present? %>
$.notify({ message: "<%= @message %>" });
<% else %>
$('.modal.admin-edit-sub-discipline-modal').modal("hide"); $('.modal.admin-edit-sub-discipline-modal').modal("hide");
$(".sub-discipline-list-container").html("<%= j(render :partial => 'admins/sub_disciplines/shared/list') %>"); $(".sub-discipline-list-container").html("<%= j(render :partial => 'admins/sub_disciplines/shared/list') %>");
<% end %>

@ -1,2 +1,6 @@
<% if @message.present? %>
$.notify({ message: "<%= @message %>" });
<% else %>
$('.modal.admin-edit-tag-discipline-modal').modal("hide"); $('.modal.admin-edit-tag-discipline-modal').modal("hide");
$(".tag-discipline-list-container").html("<%= j(render :partial => 'admins/tag_disciplines/shared/list') %>"); $(".tag-discipline-list-container").html("<%= j(render :partial => 'admins/tag_disciplines/shared/list') %>");
<% end %>

@ -164,6 +164,12 @@ zh-CN:
notes: '笔记' notes: '笔记'
trustie_hack: trustie_hack:
description: '描述' description: '描述'
discipline:
name: '名称'
sub_discipline:
name: '名称'
tag_discipline:
name: '名称'

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save