Add (very rough) function to add new custom table filters

- The javascript needs a LOT of work!
This commit is contained in:
Oliver Walters 2020-04-11 00:45:18 +10:00
parent b2565270a5
commit 613dd9d471
2 changed files with 97 additions and 13 deletions

View File

@ -15,10 +15,17 @@ function getStockLocations(filters={}, options={}) {
} }
// A map of available filters for the stock table // A map of available filters for the stock table
function getStockFilterOptions() { function getAvailableStockFilters() {
return { return {
'cascade': { 'cascade': {
'type': 'bool', 'type': 'bool',
'description': 'Include stock in sublocations',
'title': 'sublocations',
},
'active': {
'type': 'bool',
'title': 'part active',
'description': 'Show stock for active parts',
}, },
'status': { 'status': {
'options': { 'options': {
@ -28,6 +35,7 @@ function getStockFilterOptions() {
'DESTROYED': 60, 'DESTROYED': 60,
'LOST': 70 'LOST': 70
}, },
'description': 'Stock status',
} }
}; };
} }
@ -35,10 +43,10 @@ function getStockFilterOptions() {
function loadStockFilters() { function loadStockFilters() {
// Load the stock table filters from session-storage // Load the stock table filters from session-storage
var filterstring = inventreeLoad("stockfilters", "cascade=true&loc=1"); var filterstring = inventreeLoad("stockfilters", "cascade=true");
if (filterstring.length == 0) { if (filterstring.length == 0) {
filterstring = 'cascade=true&test=1&location=10&status=50'; filterstring = 'cascade=true&status=60';
} }
var split = filterstring.split("&"); var split = filterstring.split("&");
@ -91,9 +99,80 @@ function removeStockFilter(key) {
return filters; return filters;
} }
function addStockFilter(key, value) {
var filters = loadStockFilters();
filters[key] = value;
saveStockFilters(filters);
return filters;
}
function createStockFilter() { function createStockFilter() {
// TODO // TODO
console.log("create stock filter"); console.log("create stock filter");
var html = `<select id='filter-tag' name='tag'>`;
var available = getAvailableStockFilters();
var filters = loadStockFilters();
for (var key in available) {
// Ignore any keys that are already used..
if (key in filters) continue;
html += `<option value='${key}'>${available[key].title || key}</option>`;
}
html += `</select>`;
// Add in a (blank) selection for filter value
html += `<select id='filter-value' name='value'></select>`;
html += `<button class='btn btn-default' id='filter-make'>Add</Button>`;
var div = $("#add-new-filter");
div.html(html);
div.find("#filter-make").click(function() {
var tag = div.find("#filter-tag").val();
var val = div.find("#filter-value").val();
console.log(tag + " -> " + val);
addStockFilter(tag, val);
});
div.find('#filter-tag').on('change', function() {
console.log(this.value);
// Select the filter
var filter = available[this.value];
var list = div.find('#filter-value');
list.empty();
if ('type' in filter) {
if (filter.type == 'bool') {
list.append(`<option value='1'>True</option>`);
list.append(`<option value='0'>False</option>`);
}
} else if ('options' in filter) {
for (var opt in filter.options) {
list.append(`<option value='${filter.options[opt]}'>${opt}</option>`);
}
}
console.log("...");
});
console.log('done');
} }
function clearStockFilters() { function clearStockFilters() {

View File

@ -2,6 +2,7 @@
<div id='button-toolbar'> <div id='button-toolbar'>
<div class='button-toolbar container-fluid' style='float: right;'> <div class='button-toolbar container-fluid' style='float: right;'>
<button class='btn btn-default' id='stock-export' title='Export Stock Information'>{% trans "Export" %}</button>
{% if read_only %} {% if read_only %}
{% else %} {% else %}
<button class="btn btn-success" id='item-create'>{% trans "New Stock Item" %}</button> <button class="btn btn-success" id='item-create'>{% trans "New Stock Item" %}</button>
@ -17,11 +18,15 @@
</ul> </ul>
</div> </div>
{% endif %} {% endif %}
<button class='btn btn-default' id='stock-export' title='Export Stock Information'>{% trans "Export" %}</button>
<div class='filters'> <div class='filters'>
<button class='btn btn-default' id='filter-add'>Add filter</button>
<button class='btn btn-default' id='filter-clear'>Clear filters</button>
<ul class='filter-list' id='stock-filter-list'> <ul class='filter-list' id='stock-filter-list'>
<!-- This is an empty list which will be populated with the stock table filters --> <!-- This is an empty list which will be populated with the stock table filters -->
</ul> </ul>
<div id='add-new-filter'>
<!-- This is an empty div for placing an in-line form to add new filter -->
</div>
</div> </div>
</div> </div>
</div> </div>