Skip to content

Commit

Permalink
Merge pull request #11 from CottaCush/features/dependent-dropdown-asset
Browse files Browse the repository at this point in the history
Features/dependent-drop-down-asset
  • Loading branch information
olajideoye authored Sep 27, 2017
2 parents fec93a1 + 68fc714 commit 989e444
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Assets/DependentDropdownAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CottaCush\Yii2\Assets;

/**
* Class DependentDropdownAsset
* @package CottaCush\Yii2\Assets
* @author Kehinde Ladipo <ladipokenny@gmail.com>
*/
class DependentDropdownAsset extends LocalAssetBundle
{
public $js = [
'js/dependent-dropdown.js'
];
public $css = [
'css/dependent-dropdown.css'
];

public $depends = [
'yii\web\JqueryAsset'
];
}
10 changes: 10 additions & 0 deletions src/public-assets/css/dependent-dropdown.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/public-assets/css/maps/dependent-dropdown.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/public-assets/css/maps/dropzone-image-upload.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added src/public-assets/img/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions src/public-assets/js/dependent-dropdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @file
* @author Kehinde Ladipo <ladipokenny@gmail.com>
*
* Make an API request to get the dependent of a parent <select>. The API should return the full <select> element.
*
* A parent <select> looks like the following:
*
* <select data-dependent-dropdown data-url="https://example.com/api/endpoint">
* ...
* </select>
*
* The `data-dependent-dropdown` attribute signifies that this is a parent drop-down and attaches this functionality
* The `data-url` attribute carries the endpoint for the url
*
*/
$(function () {

/**
* Clears and reloads the child drop-down for a parent drop-down
* @event change
*/
$(document).on('change', 'select[data-dependent-dropdown]', function () {
var $element = $(this);
// Remove a dependent drop-down child if it already exists
$element.next('.dependent-dropdown-child').remove();

$.ajax({
type: 'POST',
url: $element.data('url'),
data: { 'parent_id': $element.val() },
beforeSend: function() {
$element.addClass('dependent-dropdown-loading');
},
success: function(response) {
$element.after('<div class="dependent-dropdown-child"></div>');
$element.next('.dependent-dropdown-child').append(response.data);
},
error: function(xhr) {
console.log(xhr.status + ': ' + xhr.message);
},
complete: function() {
$element.removeClass('dependent-dropdown-loading');
}
});
});
});
9 changes: 9 additions & 0 deletions src/public-assets/less/dependent-dropdown.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.dependent-dropdown-loading{
background: url('../img/loading.gif') right 20px center no-repeat;
cursor: wait;
opacity: 0.6;
}

.dependent-dropdown-child{
margin-top: 10px;
}

0 comments on commit 989e444

Please sign in to comment.