From 0058207fadfbc0a407c5dc924b295aa1259da22b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 8 May 2019 19:31:43 +1000 Subject: [PATCH] Functionalize loadPartTable --- InvenTree/part/templates/part/category.html | 77 ++----------------- InvenTree/static/script/inventree/part.js | 85 +++++++++++++++++++++ 2 files changed, 92 insertions(+), 70 deletions(-) diff --git a/InvenTree/part/templates/part/category.html b/InvenTree/part/templates/part/category.html index b0b7fe44a1..b9695ea6f2 100644 --- a/InvenTree/part/templates/part/category.html +++ b/InvenTree/part/templates/part/category.html @@ -119,81 +119,18 @@ {% endif %} - $("#part-table").bootstrapTable({ - sortable: true, - search: true, - sortName: 'name', - idField: 'pk', - method: 'get', - pagination: true, - rememberOrder: true, - queryParams: function(p) { - return { - active: true, + loadPartTable( + "#part-table", + "{% url 'api-part-list' %}", + { + query: { {% if category %} category: {{ category.id }}, include_child_categories: true, {% 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 "No stock"; - } - } - } - ], - url: "{% url 'api-part-list' %}", - }); - - linkButtonsToSelection( - $("#part-table"), - ['#part-options'] ); {% endblock %} \ No newline at end of file diff --git a/InvenTree/static/script/inventree/part.js b/InvenTree/static/script/inventree/part.js index c3b0f8182d..41098ba71b 100644 --- a/InvenTree/static/script/inventree/part.js +++ b/InvenTree/static/script/inventree/part.js @@ -72,4 +72,89 @@ 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 "No stock"; + } + } + } + ], + }); + + if (options.buttons) { + linkButtonsToSelection($(table), options.buttons); + } } \ No newline at end of file