Last active
October 10, 2019 15:53
-
-
Save mm580486/e6ecb30c5b275db223bee75d531ccddb to your computer and use it in GitHub Desktop.
This file contains 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
var STATES_LIST = JSON.parse($('#state_codes').text()); | |
function initStates() { | |
STATES_LIST.forEach(function(item, index) { | |
var newOption = new Option(item.name, item.state_code, false, false); | |
$('#states').append(newOption).trigger('change'); | |
}) | |
} | |
function initCities(id) { | |
var request = $.ajax({ | |
url: "https://newkhat.khatoghalam.com/api/shop/v1/show-cities", | |
type: "POST", | |
cache: false, | |
data: { | |
state_code: id | |
}, | |
dataType: "json" | |
}); | |
request.done(function(data) { | |
var json = data; | |
$('#cities').select2('data', null); | |
$('#cities').empty().trigger("change"); | |
$('#cities').prop('disabled', false); | |
var filteredObj = json.find(function(item, i) { | |
var newOption = new Option(item.name, item.city_code, false, false); | |
$('#cities').append(newOption).trigger('change'); | |
$('#cities').prop('disabled', false); | |
}); | |
}); | |
request.fail(function(jqXHR, textStatus) { | |
alert('خطایی در ارتباط با سرور رخ داده است' + textStatus); | |
}); | |
} | |
function initSelect2() { | |
$('.select2').select2({ | |
dir: 'rtl', | |
closeOnSelect: true, | |
debug: false, | |
language: 'fa', | |
multiple: false, | |
tags: false, | |
theme: "classic", | |
scrollAfterSelect: false, | |
allowClear: false | |
}); | |
$('#cities').prop('disabled', true); | |
$("#cities").select2('placeholder', "لطفا شهر خود را انتخاب کنید"); | |
$("#states").select2('placeholder', "لطفا استان خود را انتخاب کنید"); | |
initStates(); | |
} | |
initSelect2(); | |
$('#states').on('select2:select', function(e) { | |
var state = $(this); | |
initCities($(state).val()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment