GB2260
https://github.com/cn/GB2260.rb
行政区域代码
地址接口
module Api
class RegionsController < ApplicationController
# 国标 GB2260 划分规则
# 第一、二位表示省(自治区、直辖市、特别行政区)
# 第三、四位表示市(地区、自治州、盟及国家直辖市所属市辖区和县的汇总码)。
# 其中,01-20,51-70表示省直辖市;21-50表示地区(自治州、盟)
# 第五、六位表示县(市辖区、县级市、旗)。
# 01-18表示市辖区或地区(自治州、盟)辖县级市;21-80表示县(旗);81-99表示省直辖县级市
def provinces
render json: GB2260.new.provinces
end
def prefectures
divisions = GB2260.new.prefectures(params[:code]).map do |prefecture|
{
code: prefecture.code,
name: prefecture.name == '市辖区' ? prefecture.province.name : prefecture.name,
revision: prefecture.revision
}
end
render json: divisions
end
def counties
divisions = GB2260.new.counties(params[:code]).map do |division|
{
code: division.code,
name: division.name == '区/县' ? division.prefecture.name : division.name,
revision: division.revision
}
end
render json: divisions
end
end
end
js,三级地址
$(document).on 'turbolinks:load', ->
# 空option
create_empty_options = (obj) ->
obj[0].options.length = 0
option = document.createElement('option')
text = document.createTextNode('请选择')
option.setAttribute('value', '')
option.appendChild(text)
obj[0].options.add(option)
# 创建option
create_options = (obj, regions, current_code) ->
create_empty_options(obj)
for region in regions
option = document.createElement('option')
text = document.createTextNode(region.name)
option.setAttribute('value', region.code)
if current_code == region.code
option.setAttribute('selected', true)
option.appendChild(text)
obj[0].options.add(option)
# 获取省份
provinces = (obj, current_code) ->
$.get("/api/regions/provinces",
(data) ->
if data?
options = create_options(obj, data, current_code)
)
# 获取市
prefectures = (obj, code, current_code) ->
$.get("/api/regions/prefectures",
{code: code},
(data) ->
if data?
options = create_options(obj, data, current_code)
)
# 获取区/县
counties = (obj, code, current_code) ->
$.get("/api/regions/counties",
{code: code},
(data) ->
if data?
options = create_options(obj, data, current_code)
)
# 改变事件
region_code_change = (obj) ->
provinces_obj = obj.find('.provinces')
prefectures_obj = obj.find('.prefectures')
counties_obj = obj.find('.counties')
# 省
provinces_obj.change ->
# 清空县/区
options = create_empty_options(counties_obj)
counties_obj.html(options)
region_code = $(this).val()
prefectures(prefectures_obj, region_code, '')
# 市
prefectures_obj.change ->
region_code = $(this).val()
counties(counties_obj, region_code, '')
# 修改时初始化
region_code_edit_init = (obj, region_code) ->
provinces_obj = obj.find('.provinces')
prefectures_obj = obj.find('.prefectures')
counties_obj = obj.find('.counties')
# 根据国标GB2260定义,可以获取到相应的列表
# 第一,二位表示省
prefectures_code = region_code.substr(0, 2) + '0000'
provinces(provinces_obj, prefectures_code)
# 第三,四位表示市
counties_code = region_code.substr(0, 4) + '00'
prefectures(prefectures_obj, prefectures_code, counties_code)
counties(counties_obj, counties_code, region_code)
$('.region-picker').each ->
# 获取当前值
region_code = $(this).attr('data-region-code')
if region_code?
# 修改时初始化
region_code_edit_init($(this), region_code)
else
# 初始化省份
provinces_obj = $(this).find('.provinces')
provinces(provinces_obj, '')
# 改变事件
region_code_change($(this))
自定义select
class RegionInput < SimpleForm::Inputs::CollectionSelectInput
include ActionView::Helpers::FormTagHelper
def input(wrapper_options = nil)
data = options[:data] || {}
class_name = self.class.name.underscore.dasherize
template.content_tag(:div, class: "region-picker #{class_name}", data: data) do
template.concat(
select_tag(:"#{attribute_name}_province_code", nil, class: 'form-control provinces', prompt: '请选择')
)
template.concat(
select_tag(:"#{attribute_name}_prefecture_code", nil, class: 'form-control prefectures', prompt: '请选择')
)
template.concat super(wrapper_options)
end
end
end
使用
<%= f.input :region_code, as: :region, collection: [], data: {'region-code': f.object.region_code}, input_html: { class: "counties" }, label: '地址', prompt: '请选择' %>
列表获取地址名称
在对应model中添加
def region_text
GB2260.new.get(region_code).description
end
列表使用
<%= data.region_text %>