Last active
January 5, 2022 10:02
-
-
Save dhamkur/bb532314ead67eba8180f44cfec4632b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| app/assets/javascripts/custom.js | |
| $(document).ready(function() { | |
| let arraySelect2 = [".select2-yourvarname"] | |
| function select2Dynamic(name) { | |
| let id = $(name).attr("data-id") | |
| let text = $(name).attr("data-name") | |
| $(name).select2({ | |
| placeholder: "Select Data", | |
| allowClear: true, | |
| ajax: { | |
| url: $(name).attr("data-uri"), | |
| dataType: "json", | |
| data: function (params) { | |
| return { | |
| term: params.term || "", | |
| page: params.page || 1 | |
| } | |
| } | |
| } | |
| }) | |
| if (id != null && text != null) { | |
| let data = {id: id, text: text} | |
| let newOption = new Option(data.text, data.id, true, true) | |
| $(name).append(newOption).trigger('change') | |
| } | |
| } | |
| arraySelect2.forEach(function(name) { | |
| select2Dynamic(name) | |
| }) | |
| }) | |
| ========================================================================================================= | |
| ========================================================================================================= | |
| app/views/cities/_form.html.erb | |
| <%= f.select(:city_id, options_for_select([], f.object.try(:city_id)), { include_blank: 'Select City' }, { class: 'form-control select2-city', | |
| 'data-uri': address_admin_somethings_path(action_type: 'cities'), 'data-name': "#{@district.city.present? ? @district.city.name : ''}", | |
| 'data-id': "#{@district.city.present? ? @district.city.id : ''}" }) %> | |
| ========================================================================================================= | |
| ========================================================================================================= | |
| app/controllers/concerns/controller_helper.rb | |
| def get_json(klass) | |
| page = params[:page].to_i || 1 | |
| per_page = 5 | |
| offset = (page - 1) * per_page | |
| code_or_name = klass.to_s == "SpecificClassUseCode" ? "code" : "name" | |
| data = params[:term].present? ? klass.where("#{code_or_name} ilike ?", "%#{params[:term]}%").order(name: :asc).offset(offset).limit(per_page) : | |
| klass.order(name: :asc).offset(offset).limit(per_page) | |
| data_count = klass.count | |
| end_count = offset + per_page | |
| more_page = data_count > end_count | |
| results = { | |
| results: select2_api(data), | |
| pagination: { | |
| more: more_page | |
| } | |
| } | |
| render json: results, status: :ok | |
| end | |
| def select2_api(data) | |
| data.map do |data| | |
| cache data do | |
| { | |
| id: data.id, | |
| text: data.class.name == "PostalCode" ? data.code : data.name | |
| } | |
| end | |
| end | |
| end | |
| ========================================================================================================= | |
| ========================================================================================================= | |
| app/controllers/admin/somethings_controller.rb | |
| include ControllerHelper | |
| def address | |
| if params[:action_type] == "cities" | |
| get_json(City) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment