学校的查询与现实问题重做

This commit is contained in:
lizanle 2015-10-15 12:04:55 +08:00
parent d012820a54
commit b3d15b886a
10 changed files with 161 additions and 27 deletions

View File

@ -28,6 +28,7 @@ gem 'ruby-ole'
gem 'rails_kindeditor',path:'lib/rails_kindeditor'
#gem "rmagick", ">= 2.0.0"
gem 'binding_of_caller'
gem 'chinese_pinyin'
group :development do
gem 'grape-swagger'

View File

@ -126,11 +126,13 @@ class MyController < ApplicationController
end
@se = @user.extensions
if params[:occupation].to_i.to_s == params[:occupation]
# if params[:occupation].to_i.to_s == params[:occupation]
# @se.school_id = params[:occupation]
# else
# @se.occupation = params[:occupation]
# end
@se.school_id = params[:occupation]
else
@se.occupation = params[:occupation]
end
@se.gender = params[:gender]
@se.location = params[:province] if params[:province]
@se.location_city = params[:city] if params[:city]

View File

@ -105,4 +105,31 @@ class SchoolController < ApplicationController
render :text => options
end
#根据学校名字或者拼音来查询
def on_search
condition = "#{params[:name].strip}".gsub(" ","")
#将条件截断为汉字和拼音(全汉字 或者 全拼音 或者 汉字和拼音),
#获取拼音的第一次出现的位置
chinese = []
pinyin = []
condition.scan(/./).each_with_index do |char,index|
if char =~ /[a-zA-Z0-9]/
pinyin << char
else
chinese << char
end
end
if(condition == '')
@school = School.all
else
@school = School.where("name like '%#{chinese.join("")}%' and pinyin like '%#{pinyin.join("")}%'").all
end
result = []
# @school.each do |sc|
# result << {:value=>sc.name,:data=>sc.id}
# end
render :json => @school.to_json
end
end

View File

@ -1,5 +1,5 @@
class School < ActiveRecord::Base
attr_accessible :name, :province
attr_accessible :name, :province,:pinyin
has_many :courses
def to_s

View File

@ -18,8 +18,8 @@
<li>登录名&nbsp;:&nbsp;<span style="color:red;">*</span></li>
<li>邮箱&nbsp;:&nbsp;<span style="color:red;">*</span></li>
<li>身份&nbsp;:&nbsp;</li>
<li nhname="tag" nh_tag_0="true" nh_tag_1="true" nh_tag_3="true" style="display:none;">姓&nbsp;:&nbsp;<span style="color:red;">*</span></li>
<li nhname="tag" nh_tag_0="true" nh_tag_1="true" nh_tag_3="true" style="display:none;">名&nbsp;:&nbsp;<span style="color:red;">*</span></li>
<li nhname="tag" nh_tag_0="true" nh_tag_1="true" nh_tag_3="true" style="display:none;">姓(First Name)&nbsp;:&nbsp;<span style="color:red;">*</span></li>
<li nhname="tag" nh_tag_0="true" nh_tag_1="true" nh_tag_3="true" style="display:none;">名(Last Name)&nbsp;:&nbsp;<span style="color:red;">*</span></li>
<li nhname="tag" nh_tag_2="true" style="display:none;">组织名&nbsp;:&nbsp;<span style="color:red;">*</span></li>
<li nhname="tag" nh_tag_0="true" nh_tag_1="true" nh_tag_3="true" style="display:none;">性别&nbsp;:&nbsp;</li>
<li nhname="tag" nh_tag_0="true" nh_tag_1="true" nh_tag_3="true" style="display:none;">工作单位&nbsp;:&nbsp;</li>
@ -72,21 +72,25 @@
<li nhname="tag" nh_tag_0="true" nh_tag_1="true" nh_tag_3="true" style="display:none;">
<% if User.current.user_extensions.nil? %>
<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="province" name="province" style="display: none;width:130px;" class="w70" type="text" value=<%= l(:field_occupation_click) %> readonly>
<input nhname="tag" autocomplete="off" nh_tag_0="true" nh_tag_1="true" id="province" name="province" style="display: none;width:130px;" class="w70" type="text" >
<input nhname="tag" nh_tag_3="true" id="occupation" name="occupation" style="display: none;" class="w210" type="text" value="" />
<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="occupation_name" type="text" style="display: none;width:117px;" readonly/>
<span id="hint" style="color: #7f7f7f;display: none">平台找到了<a id="school_num" href="javascript:void(0)" style="color: red" >0</a>个包含<a id="search_condition" href="javascript:void(0)">"国防"</a>的高校</span>
<!--<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="occupation_name" type="text" style="display: none;width:117px;" readonly/>-->
<% elsif User.current.user_extensions.identity == 3 || User.current.user_extensions.identity == 2 %>
<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="province" name="province" style="display: none;width:130px;" class="w70" type="text" value=<%= l(:field_occupation_click) %> readonly>
<input nhname="tag" autocomplete="off" nh_tag_0="true" nh_tag_1="true" id="province" name="province" style="display: none;width:130px;" class="w70" type="text" >
<input nhname="tag" nh_tag_3="true" id="occupation" name="occupation" style="display: none;" class="w210" type="text" value="<%= @user.user_extensions.occupation %>" />
<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="occupation_name" type="text" style="display: none;width:117px;" readonly/>
<span id="hint" style="color: #7f7f7f;display: none">平台找到了<a id="school_num" href="javascript:void(0)" style="color: red" >0</a>个包含<a id="search_condition" href="javascript:void(0)">"国防"</a>的高校</span>
<!--<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="occupation_name" type="text" style="display: none;width:117px;" readonly/>-->
<% elsif User.current.user_extensions.school.nil? %>
<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="province" name="province" style="display: none;width:130px;" class="w70" type="text" value=<%= l(:field_occupation_click) %> readonly>
<input nhname="tag" autocomplete="off" nh_tag_0="true" nh_tag_1="true" id="province" name="province" style="display: none;width:130px;" class="w70" type="text" >
<input nhname="tag" nh_tag_3="true" id="occupation" name="occupation" style="display: none;" class="w210" type="text" />
<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="occupation_name" type="text" style="display: none;width:117px;" readonly/>
<span id="hint" style="color: #7f7f7f;display: none">平台找到了<a id="school_num" href="javascript:void(0)" style="color: red" >0</a>个包含<a id="search_condition" href="javascript:void(0)">"国防"</a>的高校</span>
<!--<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="occupation_name" type="text" style="display: none;width:117px;" readonly/>-->
<% else %>
<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="province" name="province" style="display: none;width:130px;" class="w70" type="text" value="<%= User.current.user_extensions.school.province %>" readonly/>
<input nhname="tag" autocomplete="off" nh_tag_0="true" nh_tag_1="true" id="province" name="province" style="display: none;width:130px;" class="w70" type="text" value="<%= User.current.user_extensions.school %>" />
<input nhname="tag" nh_tag_3="true" id="occupation" name="occupation" type="text" style="display: none;" class="w210" value="<%= User.current.user_extensions.school.id %>"/>
<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="occupation_name" type="text" style="display: none;width:117px;" value="<%= User.current.user_extensions.school.name %>" readonly="true" style="background-color: #E2E2E2;"/>
<span id="hint" style="color: #7f7f7f;display: none" >平台找到了<a id="school_num" href="javascript:void(0)" style="color: red" >0</a>个包含<a id="search_condition" href="javascript:void(0)">"国防"</a>的高校</span>
<!--<input nhname="tag" nh_tag_0="true" nh_tag_1="true" id="occupation_name" type="text" style="display: none;width:117px;" value="<%#= User.current.user_extensions.school.name %>" readonly="true" style="background-color: #E2E2E2;"/>-->
<% end %>
</li>
@ -180,7 +184,9 @@
</div><!--users_setting end-->
</div>
<div id="search_school_result_list"
style="width: 135px;line-height: 1.5;min-height:20px; max-height: 200px; height: auto!; !important;display: none;background: white;overflow: scroll;border: solid 1px #cccccc; overflow-x: hidden; overflow-y: auto;">
</div>
<div id="WOpenWindow">
<a class="modal_close" href="#"></a>
<h2 style="margin: 10px"><%= l(:lable_school_list)%></h2>
@ -202,6 +208,7 @@
</ul>
</div>
</div>
<%= stylesheet_link_tag 'nyan' %>
@ -518,10 +525,77 @@
}
}
}
$(function(){
//学校
$("#province").attr("href", "#WOpenWindow")
$("#province").leanModal({top: 100, closeButton: ".modal_close"});
function changeValue(value,data){
//console.log(value+","+data)
$("input[name='province']").val(value);
$("input[name='occupation']").val(data);
$("#search_school_result_list").hide();
$("#hint").hide();
}
var lastSearchCondition = '';
$(function() {
//查询学校
$("input[name='province']").on('input', function (e) {
$("input[name='occupation']").val(''); //一旦有输入就清空id。
if($(e.target).val().trim() == lastSearchCondition){
return;
}
lastSearchCondition = $(e.target).val().trim();
$.ajax({
url: '<%= url_for(:controller => 'school',:action => 'on_search') %>' + '?name=' + e.target.value,
type: 'post',
success: function (data) {
if(data.lengh != 0) {
var i = 0;
$("#search_school_result_list").html('');
for (; i < data.length; i++) {
link = '<a onclick="window.changeValue(\'' + data[i].school.name + '\',\'' + data[i].school.id + '\')" href="javascript:void(0)">' + data[i].school.name + '</a><br/>';
$("#search_school_result_list").append(link);
}
$("#search_school_result_list").css('left', $(e.target).offset().left);
$("#search_school_result_list").css('top', $(e.target).offset().top + 28);
$("#search_school_result_list").css("position", "absolute");
$("#search_school_result_list").show();
if($(e.target).val().trim() != '') {
$("#hint").html('平台找到了' + data.length + '个包含"' + e.target.value + '"的高校');
$("#hint").show();
}else{
$("#hint").hide();
}
}else{
$("#hint").html('平台没有找到包含"'+e.target.value+'"的高校,创建该高校。');
$("#hint").show();
}
}
});
});
$(document.body).click(function(e){
if($(e.target).attr("id") != 'search_school_result_list')
{
$("#search_school_result_list").hide();
$("#hint").hide();
}
})
$("input[name='province']").on('focus', function (e) {
$.ajax({
url: '<%= url_for(:controller => 'school',:action => 'on_search') %>' + '?name=' + e.target.value,
type: 'post',
success: function (data) {
var i = 0;
$("#search_school_result_list").html('');
for ( ;i<data.length;i++){
link = '<a onclick="window.changeValue(\''+data[i].school.name+'\',\''+data[i].school.id+'\')" href="javascript:void(0)">' +data[i].school.name+'</a><br/>';
$("#search_school_result_list").append(link);
}
$("#search_school_result_list").css('left',$(e.target).offset().left);
$("#search_school_result_list").css('top',$(e.target).offset().top + 28);
$("#search_school_result_list").css("position", "absolute");
$("#search_school_result_list").show();
}
});
});
// $("#province").leanModal({top: 100, closeButton: ".modal_close"});
//地区
var province = "<%= "#{province}" %>"
@ -544,6 +618,9 @@
$("#users_tb_2").click();
<% end %>
$('#my_account_form_link').click(function(){
if( $("input[name='province']").val().trim() != '' && $("input[name='occupation']").val().trim() == ''){ //学校名字和id不对的话
return;
}
$('#my_account_form_btn').click();
});
$('#my_password_form_link').click(function(){

View File

@ -879,6 +879,7 @@ RedmineApp::Application.routes.draw do
post 'school/search_school/', :to => 'school#search_school'
get 'school/search_school/', :to => 'school#search_school'
post 'school/on_search'
######added by nie
match 'tags/show_projects_tags'
########### added by liuping

View File

@ -0,0 +1,5 @@
class AddColumnPinyinToSchools < ActiveRecord::Migration
def change
add_column :schools, :pinyin, :string
end
end

View File

@ -0,0 +1,11 @@
class TransferNameColumnValueToPinyinColumnValue < ActiveRecord::Migration
def up
School.all.each do |school|
school.pinyin = Pinyin.t(school.name, splitter: '')
school.save
end
end
def down
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20150930011457) do
ActiveRecord::Schema.define(:version => 20151014023806) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -782,6 +782,16 @@ ActiveRecord::Schema.define(:version => 20150930011457) do
add_index "journal_details", ["journal_id"], :name => "journal_details_journal_id"
create_table "journal_details_copy", :force => true do |t|
t.integer "journal_id", :default => 0, :null => false
t.string "property", :limit => 30, :default => "", :null => false
t.string "prop_key", :limit => 30, :default => "", :null => false
t.text "old_value"
t.text "value"
end
add_index "journal_details_copy", ["journal_id"], :name => "journal_details_journal_id"
create_table "journal_replies", :id => false, :force => true do |t|
t.integer "journal_id"
t.integer "user_id"
@ -1245,6 +1255,7 @@ ActiveRecord::Schema.define(:version => 20150930011457) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "logo_link"
t.string "pinyin"
end
create_table "seems_rateable_cached_ratings", :force => true do |t|
@ -1308,9 +1319,9 @@ ActiveRecord::Schema.define(:version => 20150930011457) do
create_table "student_work_tests", :force => true do |t|
t.integer "student_work_id"
t.integer "status"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "status", :default => 9
t.text "results"
t.text "src"
end
@ -1558,7 +1569,6 @@ ActiveRecord::Schema.define(:version => 20150930011457) do
t.string "identity_url"
t.string "mail_notification", :default => "", :null => false
t.string "salt", :limit => 64
t.integer "gid"
end
add_index "users", ["auth_source_id"], :name => "index_users_on_auth_source_id"

View File

@ -81,7 +81,7 @@ a.select_btn_select{ background:#64bddb; color:#fff;}
.users_dis{display:block; }
.users_undis{display:none;}
.users_ctt{ font-size:14px; color:#666; margin-top:10px;}
.setting_left{ width:85px; text-align:right; float:left;}
.setting_left{ width:115px; text-align:right; float:left;}
.setting_left li{ height:28px;line-height:28px;}
.setting_right{width:500px; text-align:left; float:left; margin-left:8px;}
.setting_right li{ height:28px;line-height:28px;}