Merge pull request #135 from SchrodingersGat/select-button-enable

Disable buttons that require row selection, until a selection has been made
This commit is contained in:
Oliver 2019-04-18 22:42:36 +10:00 committed by GitHub
commit 2191b7f719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 4 deletions

View File

@ -9,7 +9,7 @@
<div id='button-toolbar'>
<button class='btn btn-success' id='add-stock-item'>New Stock Item</button>
<div id='opt-dropdown' class="dropdown" style='float: right;'>
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Options
<button id='stock-options' class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Options
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href='#' id='multi-item-take' title='Take items from stock'>Take items</a></li>
@ -49,6 +49,9 @@
params: {
part: {{ part.id }},
},
buttons: [
'#stock-options',
],
url: "{% url 'api-stock-list' %}",
});

View File

@ -319,8 +319,11 @@ function deleteStockItems(items, options) {
}
function loadStockTable(modal, options) {
modal.bootstrapTable({
function loadStockTable(table, options) {
table.bootstrapTable({
sortable: true,
search: true,
method: 'get',
@ -376,4 +379,8 @@ function loadStockTable(modal, options) {
],
url: options.url,
});
if (options.buttons) {
linkButtonsToSelection(table, options.buttons);
}
};

View File

@ -60,3 +60,23 @@ function renderEditable(text, options) {
return html;
}
function enableButtons(elements, enabled) {
for (let item of elements) {
$(item).prop('disabled', !enabled);
}
}
function linkButtonsToSelection(table, buttons) {
/* Link a bootstrap-table object to one or more buttons.
* The buttons will only be enabled if there is at least one row selected
*/
// Initially set the enable state of the buttons
enableButtons(buttons, table.bootstrapTable('getSelections').length > 0);
// Add a callback
table.on('check.bs.table uncheck.bs.table check-some.bs.table uncheck-some.bs.table check-all.bs.table uncheck-all.bs.table', function(row) {
enableButtons(buttons, table.bootstrapTable('getSelections').length > 0);
});
}

View File

@ -43,7 +43,7 @@
<div class='container-fluid' style='float: right;'>
<button class="btn btn-success" id='item-create'>New Stock Item</span></button>
<div class="dropdown" style='float: right;'>
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Options
<button id='stock-options' class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Options
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" id='multi-item-add' title='Add to selected stock items'>Add stock</a></li>
@ -158,6 +158,9 @@
});
loadStockTable($("#stock-table"), {
buttons: [
'#stock-options',
],
params: {
{% if location %}
location: {{ location.id }},