From f4f6253178dbf96adf806f35d8b82cd724b918c2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 23 Oct 2020 00:58:35 +1100 Subject: [PATCH] Better table sorting for allocation quantity --- InvenTree/templates/js/build.js | 42 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/InvenTree/templates/js/build.js b/InvenTree/templates/js/build.js index ab6215519d..3cb0a3e81b 100644 --- a/InvenTree/templates/js/build.js +++ b/InvenTree/templates/js/build.js @@ -107,6 +107,21 @@ function loadBuildOutputAllocationTable(buildId, partId, output, options={}) { $(table).bootstrapTable('refresh'); } + function sumAllocations(row) { + // Calculat total allocations for a given row + if (!row.allocations) { + return 0; + } + + var quantity = 0; + + row.allocations.forEach(function(item) { + quantity += item.quantity; + }); + + return quantity; + } + function setupCallbacks() { // Register button callbacks once table data are loaded @@ -196,11 +211,7 @@ function loadBuildOutputAllocationTable(buildId, partId, output, options={}) { tableRow.allocations = allocations[key]; // Calculate the total allocated quantity - var allocatedQuantity = 0; - - tableRow.allocations.forEach(function (allocation) { - allocatedQuantity += allocation.quantity; - }); + var allocatedQuantity = sumAllocations(tableRow); // Is this line item fully allocated? if (allocatedQuantity >= (tableRow.quantity * output.quantity)) { @@ -354,6 +365,11 @@ function loadBuildOutputAllocationTable(buildId, partId, output, options={}) { title: '{% trans "Reference" %}', sortable: true, }, + { + field: 'quantity', + title: '{% trans "Quantity Per" %}', + sortable: true, + }, { field: 'allocated', title: '{% trans "Allocated" %}', @@ -370,6 +386,22 @@ function loadBuildOutputAllocationTable(buildId, partId, output, options={}) { var required = row.quantity * output.quantity; return makeProgressBar(allocated, required); + }, + sorter: function(valA, valB, rowA, rowB) { + var aA = sumAllocations(rowA); + var aB = sumAllocations(rowB); + + var qA = rowA.quantity * output.quantity; + var qB = rowB.quantity * output.quantity; + + if (aA == 0 && aB == 0) { + return (qA > qB) ? 1 : -1; + } + + var progressA = parseFloat(aA) / qA; + var progressB = parseFloat(aB) / qB; + + return (progressA < progressB) ? 1 : -1; } }, {