Javascript function to render stock tracking table

- Added extra info to StockItemTracking serializer
This commit is contained in:
Oliver Walters 2019-04-25 22:04:02 +10:00
parent 77298c8fe8
commit 533fdb71c4
3 changed files with 138 additions and 91 deletions

View File

@ -383,4 +383,93 @@ function loadStockTable(table, options) {
if (options.buttons) {
linkButtonsToSelection(table, options.buttons);
}
};
}
function loadStockTrackingTable(table, options) {
var cols = [
{
field: 'pk',
visible: false,
},
{
field: 'date',
title: 'Date',
sortable: true,
formatter: function(value, row, index, field) {
var m = moment(value);
if (m.isValid()) {
var html = m.format('dddd MMMM Do YYYY') + '<br>' + m.format('h:mm a');
return html;
}
return 'N/A';
}
},
];
// If enabled, provide a link to the referenced StockItem
if (options.partColumn) {
cols.push({
field: 'item',
title: 'Stock Item',
sortable: true,
formatter: function(value, row, index, field) {
return renderLink(value.part_name, value.url);
}
});
}
// Stock transaction description
cols.push({
field: 'title',
title: 'Description',
sortable: true,
formatter: function(value, row, index, field) {
var html = "<b>" + value + "</b>";
if (row.notes) {
html += "<br><i>" + row.notes + "</i>";
}
return html;
}
});
cols.push({
field: 'quantity',
title: 'Quantity',
});
cols.push({
sortable: true,
field: 'user',
title: 'User',
formatter: function(value, row, index, field) {
if (value)
{
// TODO - Format the user's first and last names
return value.username;
}
else
{
return "No user information";
}
}
});
table.bootstrapTable({
sortable: true,
search: true,
method: 'get',
rememberOrder: true,
queryParams: options.params,
columns: cols,
url: options.url,
});
if (options.buttons) {
linkButtonsToSelection(table, options.buttons);
}
}

View File

@ -24,31 +24,22 @@ class LocationBriefSerializer(serializers.ModelSerializer):
]
class StockTrackingSerializer(serializers.ModelSerializer):
class StockItemSerializerBrief(serializers.ModelSerializer):
"""
Provide a brief serializer for StockItem
"""
url = serializers.CharField(source='get_absolute_url', read_only=True)
user = UserSerializerBrief(many=False, read_only=True)
part_name = serializers.CharField(source='part.name', read_only=True)
class Meta:
model = StockItemTracking
model = StockItem
fields = [
'pk',
'uuid',
'url',
'item',
'date',
'title',
'notes',
'quantity',
'user',
'system',
]
read_only_fields = [
'date',
'user',
'system',
'quantity',
'part_name',
]
@ -118,3 +109,33 @@ class LocationSerializer(serializers.ModelSerializer):
'parent',
'pathstring'
]
class StockTrackingSerializer(serializers.ModelSerializer):
url = serializers.CharField(source='get_absolute_url', read_only=True)
user = UserSerializerBrief(many=False, read_only=True)
item = StockItemSerializerBrief(many=False, read_only=True)
class Meta:
model = StockItemTracking
fields = [
'pk',
'url',
'item',
'date',
'title',
'notes',
'quantity',
'user',
'system',
]
read_only_fields = [
'date',
'user',
'system',
'quantity',
]

View File

@ -121,22 +121,11 @@
{% if item.has_tracking_info %}
<hr>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1">Stock Tracking</a><span class='badge'>{{ item.tracking_info.all|length }}</span>
</h4>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">
<table class='table table-condensed table-striped' id='track-table'>
</table>
</div>
</div>
</div>
<div id='table-toolbar'>
<h4>Stock Tracking Information</h4>
</div>
<table class='table table-condensed table-striped' id='track-table' data-toolbar='#table-toolbar'>
</table>
{% endif %}
{% endblock %}
{% block js_ready %}
@ -210,66 +199,14 @@
});
});
$('#track-table').bootstrapTable({
sortable: true,
search: true,
method: 'get',
queryParams: function(p) {
loadStockTrackingTable($("#track-table"), {
params: function(p) {
return {
ordering: '-date',
item: {{ item.pk }},
}
};
},
columns: [
{
field: 'date',
title: 'Date',
sortable: true,
formatter: function(value, row, index, field) {
var m = moment(value);
if (m.isValid()) {
var html = m.format('dddd MMMM Do YYYY') + '<br>' + m.format('h:mm a');
return html;
}
return 'N/A';
}
},
{
field: 'title',
title: 'Description',
sortable: true,
formatter: function(value, row, index, field) {
var html = "<b>" + value + "</b>";
if (row.notes) {
html += "<br><i>" + row.notes + "</i>";
}
return html;
}
},
{
field: 'quantity',
title: 'Quantity',
},
{
sortable: true,
field: 'user',
title: 'User',
formatter: function(value, row, index, field) {
if (value)
{
// TODO - Format the user's first and last names
return value.username;
}
else
{
return "No user information";
}
}
}
],
url: "{% url 'api-stock-track' %}",
})
url: "{% url 'api-stock-track' %}",
});
{% endblock %}