state list by ajax in station edit

This commit is contained in:
HB9HIL
2024-02-17 15:42:27 +01:00
parent 044e1b2da4
commit efe8a2eab0
3 changed files with 84 additions and 75 deletions

View File

@@ -93,56 +93,24 @@
<small id="stationCityInputHelp" class="form-text text-muted"><?php echo lang("station_location_city_hint"); ?></small>
</div>
<!-- US State -->
<div class="mb-3" id="us_state">
<label for="stateInput"><?php echo lang("station_location_state"); ?></label>
<select class="form-select" name="station_state" id="StateHelp" aria-describedby="stationCntyInputHelp">
<?php
$CI = &get_instance();
$CI->load->library('subdivisions');
<!-- State -->
<script>
var set_state = '<?php echo $my_station_profile->state; ?>';
</script>
<div class="mb-3" id="location_state">
<label for="stateInput" id="stateInputLabel"></label>
<select class="form-select" name="station_state" id="stateDropdown">
<option value=""></option>
</select>
<small id="StateHelp" class="form-text text-muted"><?php echo lang("station_location_state_hint"); ?></small>
</div>
$state_list = $CI->subdivisions->get_state_list($my_station_profile->station_dxcc);
?>
<option value=""></option>
<?php foreach ($state_list->result() as $state) {
$selected = ($my_station_profile->state == $state->state) ? 'selected="selected"' : '';
?>
<option value="<?php echo $state->state; ?>" <?php echo $selected; ?>>
<?php echo $state->subdivision . ' (' . $state->state . ')'; ?>
</option>
<?php } ?>
</select>
<small id="StateHelp" class="form-text text-muted"><?php echo lang("station_location_state_hint"); ?></small>
</div>
<!-- Canada State -->
<div class="mb-3" id="canada_state">
<label for="stateInput"><?php echo lang("station_location_state"); ?></label>
<select class="form-select" name="station_ca_state" id="StateHelp" aria-describedby="stationCntyInputHelp">
<option value=""></option>
<option value="AB" <?php if($my_station_profile->state == "AB") { echo "selected"; } ?>>Alberta</option>
<option value="BC" <?php if($my_station_profile->state == "BC") { echo "selected"; } ?>>British Columbia</option>
<option value="MB" <?php if($my_station_profile->state == "MB") { echo "selected"; } ?>>Manitoba</option>
<option value="NB" <?php if($my_station_profile->state == "NB") { echo "selected"; } ?>>New Brunswick</option>
<option value="NL" <?php if($my_station_profile->state == "NL") { echo "selected"; } ?>>Newfoundland & Labrador</option>
<option value="NS" <?php if($my_station_profile->state == "NS") { echo "selected"; } ?>>Nova Scotia</option>
<option value="NT" <?php if($my_station_profile->state == "NT") { echo "selected"; } ?>>Northwest Territories</option>
<option value="NU" <?php if($my_station_profile->state == "NU") { echo "selected"; } ?>>Nunavut</option>
<option value="ON" <?php if($my_station_profile->state == "ON") { echo "selected"; } ?>>Ontario</option>
<option value="PE" <?php if($my_station_profile->state == "PE") { echo "selected"; } ?>>Prince Edward Island</option>
<option value="QC" <?php if($my_station_profile->state == "QC") { echo "selected"; } ?>>Quebec</option>
<option value="SK" <?php if($my_station_profile->state == "SK") { echo "selected"; } ?>>Saskatchewan</option>
<option value="YT" <?php if($my_station_profile->state == "YT") { echo "selected"; } ?>>Yukon</option>
</select>
<small id="StateHelp" class="form-text text-muted"><?php echo lang("station_location_state_hint"); ?></small>
</div>
<!-- US County -->
<div class="mb-3">
<label for="stationCntyInput"><?php echo lang("station_location_county"); ?></label>
<input disabled="disabled" type="text" class="form-control" name="station_cnty" id="stationCntyInput" aria-describedby="stationCntyInputHelp" value="<?php if(set_value('station_cnty') != "") { echo set_value('station_cnty'); } else { echo $my_station_profile->station_cnty; } ?>">
<small id="stationCntyInputHelp" class="form-text text-muted"><?php echo lang("station_location_county_hint"); ?></small>
</div>
<!-- US County -->
<div class="mb-3" id="location_us_county">
<label for="stationCntyInput"><?php echo lang("station_location_county"); ?></label>
<input disabled="disabled" type="text" class="form-control" name="station_cnty" id="stationCntyInput" aria-describedby="stationCntyInputHelp" value="<?php if(set_value('station_cnty') != "") { echo set_value('station_cnty'); } else { echo $my_station_profile->station_cnty; } ?>">
<small id="stationCntyInputHelp" class="form-text text-muted"><?php echo lang("station_location_county_hint"); ?></small>
</div>
</div>
</div>
</div>

View File

@@ -627,6 +627,7 @@ function showQsoActionsMenu(_this) {
}
});
}
if ($('.table-responsive .dropdown-toggle').length>0) {
$('.table-responsive .dropdown-toggle').off('mouseenter').on('mouseenter', function () {
showQsoActionsMenu($(this).closest('.dropdown'));
@@ -637,6 +638,33 @@ function getDataTablesLanguageUrl() {
return "../assets/json/datatables_languages/" + lang_datatables_language + ".json";
}
function statesDropdown(states, set_state = null) {
var dropdown = $('#stateDropdown');
dropdown.empty();
dropdown.append($('<option>', {
value: '',
}));
if (states.status == 'ok') {
dropdown.prop('disabled', false);
$.each(states.data, function(index, state) {
var option = $('<option>', {
value: state.state,
text: state.subdivision + ' (' + state.state + ')'
});
dropdown.append(option);
});
$(dropdown).val(set_state);
} else {
dropdown.empty();
var option = $('<option>', {
value: '',
text: 'No states for this DXCC in database'
});
dropdown.append(option);
dropdown.prop('disabled', true);
}
}
console.log("Ready to unleash your coding prowess and join the fun?\n\n" +
"Check out our GitHub Repository and dive into the coding adventure:\n\n" +
"🚀 https://www.github.com/wavelog/wavelog");

View File

@@ -1,31 +1,44 @@
$(document).ready( function () {
$(document).ready(function () {
$("#station_locations_table").DataTable({
stateSave: true,
language: {
url: getDataTablesLanguageUrl(),
},
});
// Use Jquery to hide div ca_state
if (window.location.pathname.indexOf("/station/edit") !== -1) {
updateStateDropdown();
$("#dxcc_select").change(function () {
updateStateDropdown();
});
}
});
$('#station_locations_table').DataTable({
"stateSave": true,
"language": {
url: getDataTablesLanguageUrl(),
}
});
function updateStateDropdown() {
var selectedDxcc = $("#dxcc_select");
$("#canada_state").hide();
if (selectedDxcc.val() !== "") {
$.ajax({
url: base_url + "index.php/lookup/get_state_list",
type: "POST",
data: { dxcc: selectedDxcc.val() },
success: function (response) {
if (response.status === "ok") {
statesDropdown(response, set_state);
$('#stateInputLabel').html(response.subdivision_name);
} else {
statesDropdown(response);
}
},
error: function () {
console.log('ERROR', response.status);
},
});
}
var selectedDXCCID = $('#dxcc_select').find(":selected").val();
if(selectedDXCCID == '1'){
$("#canada_state").show();
$("#us_state").hide();
if (selectedDxcc.val() == '291' || selectedDxcc.val() == '110' || selectedDxcc.val() == '6') {
$("#location_us_county").show();
} else {
$("#location_us_county").hide();
}
$('#dxcc_select').change(function(){
if($(this).val() == '1'){ // or this.value == 'volvo'
console.log("CANADA!");
$("#canada_state").show();
$("#us_state").hide();
} else {
$("#canada_state").hide();
$("#us_state").show();
}
});
} );
}