Functionalize loadPartTable

This commit is contained in:
Oliver Walters 2019-05-08 19:31:43 +10:00
parent 2a66224952
commit 0058207fad
2 changed files with 92 additions and 70 deletions

View File

@ -119,81 +119,18 @@
{% endif %} {% endif %}
$("#part-table").bootstrapTable({ loadPartTable(
sortable: true, "#part-table",
search: true, "{% url 'api-part-list' %}",
sortName: 'name', {
idField: 'pk', query: {
method: 'get',
pagination: true,
rememberOrder: true,
queryParams: function(p) {
return {
active: true,
{% if category %} {% if category %}
category: {{ category.id }}, category: {{ category.id }},
include_child_categories: true, include_child_categories: true,
{% endif %} {% endif %}
} },
buttons: ['#part-options'],
}, },
columns: [
{
checkbox: true,
title: 'Select',
searchable: false,
},
{
field: 'pk',
title: 'ID',
visible: false,
},
{
field: 'name',
title: 'Part',
sortable: true,
formatter: function(value, row, index, field) {
return imageHoverIcon(row.image_url) + renderLink(value, row.url);
}
},
{
sortable: true,
field: 'description',
title: 'Description',
},
{
sortable: true,
field: 'category_name',
title: 'Category',
formatter: function(value, row, index, field) {
if (row.category) {
return renderLink(row.category_name, "/part/category/" + row.category + "/");
}
else {
return '';
}
}
},
{
field: 'total_stock',
title: 'Stock',
searchable: false,
sortable: true,
formatter: function(value, row, index, field) {
if (value) {
return renderLink(value, row.url + 'stock/');
}
else {
return "<span class='label label-warning'>No stock</span>";
}
}
}
],
url: "{% url 'api-part-list' %}",
});
linkButtonsToSelection(
$("#part-table"),
['#part-options']
); );
{% endblock %} {% endblock %}

View File

@ -73,3 +73,88 @@ function toggleStar(options) {
} }
); );
} }
function loadPartTable(table, url, options={}) {
/* Load part listing data into specified table.
*
* Args:
* - table: HTML reference to the table
* - url: Base URL for API query
* - options: object containing following (optional) fields
* query: extra query params for API request
* buttons: If provided, link buttons to selection status of this table
*/
// Default query params
options.active = true;
$(table).bootstrapTable({
url: url,
sortable: true,
search: true,
sortName: 'name',
method: 'get',
pagination: true,
pageSize: 25,
rememberOrder: true,
queryParams: function(p) {
return options;
},
columns: [
{
checkbox: true,
title: 'Select',
searchable: false,
},
{
field: 'pk',
title: 'ID',
visible: false,
},
{
field: 'name',
title: 'Part',
sortable: true,
formatter: function(value, row, index, field) {
return imageHoverIcon(row.image_url) + renderLink(value, row.url);
}
},
{
sortable: true,
field: 'description',
title: 'Description',
},
{
sortable: true,
field: 'category_name',
title: 'Category',
formatter: function(value, row, index, field) {
if (row.category) {
return renderLink(row.category_name, "/part/category/" + row.category + "/");
}
else {
return '';
}
}
},
{
field: 'total_stock',
title: 'Stock',
searchable: false,
sortable: true,
formatter: function(value, row, index, field) {
if (value) {
return renderLink(value, row.url + 'stock/');
}
else {
return "<span class='label label-warning'>No stock</span>";
}
}
}
],
});
if (options.buttons) {
linkButtonsToSelection($(table), options.buttons);
}
}