accounts.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. {% extends "base.html" %}
  2. {% set active_page = "accounts" %}
  3. {% block title %} Account listing {% endblock %}
  4. {% block divs %}
  5. <div class="container-fluid">
  6. <div class="row">
  7. <div class="col-md-2"></div>
  8. <div class="col-md-8">
  9. <div class="card">
  10. <h3>All Accounts
  11. </h3>
  12. <div class="table-responsive">
  13. <table class="table table-striped paginate nav-on-click" title="View data" id="accountsTable">
  14. </table>
  15. </div>
  16. </div>
  17. </div>
  18. </div>
  19. <div class="col-md-2"></div>
  20. </div>
  21. </div>
  22. <script>
  23. function Account(
  24. id,
  25. name,
  26. assets,
  27. users,
  28. roles,
  29. ) {
  30. this.id = id;
  31. this.name = name;
  32. this.assets = assets;
  33. this.users = users;
  34. this.roles = roles.map((role) => role.name).join(", ");
  35. this.url = `/accounts/${id}`;
  36. }
  37. $(document).ready(function () {
  38. let unit = "";
  39. // Initialize the DataTable
  40. const table = $("#accountsTable").dataTable({
  41. order: [[0, "asc"]],
  42. serverSide: true,
  43. // make the table row vertically aligned with header
  44. columns: [
  45. { data: "id", title: "ID", orderable: true},
  46. { data: "name", title: "Name", orderable: true},
  47. { data: "assets", title: "Assets", orderable: true},
  48. { data: "users", title: "Users", orderable: true},
  49. { data: "roles", title: "Roles", orderable: false},
  50. { data: "url", title: "URL", className: "d-none" },
  51. ],
  52. ajax: function (data, callback, settings) {
  53. const basePath = window.location.origin;
  54. let filter = data["search"]["value"];
  55. let orderColumnIndex = data["order"][0]["column"]
  56. let orderDirection = data["order"][0]["dir"];
  57. let orderColumnName = data["columns"][orderColumnIndex]["data"];
  58. let url = `${basePath}/api/v3_0/accounts?page=${
  59. data["start"] / data["length"] + 1
  60. }&per_page=${data["length"]}`;
  61. if (filter.length > 0) {
  62. url = `${url}&filter=${filter}`;
  63. }
  64. if (orderColumnName){
  65. url = `${url}&sort_by=${orderColumnName}&sort_dir=${orderDirection}`;
  66. }
  67. $.ajax({
  68. type: "get",
  69. url: url,
  70. success: function (response, text) {
  71. let clean_response = [];
  72. response["data"].forEach((element) =>
  73. clean_response.push(
  74. new Account(
  75. element["id"],
  76. element["name"],
  77. element["asset_count"],
  78. element["user_count"],
  79. element["account_roles"],
  80. )
  81. )
  82. );
  83. callback({
  84. data: clean_response,
  85. recordsTotal: response["num-records"],
  86. recordsFiltered: response["filtered-records"],
  87. });
  88. },
  89. error: function (request, status, error) {
  90. console.log("Error: ", error);
  91. },
  92. });
  93. },
  94. });
  95. });
  96. </script>
  97. {% block paginate_tables_script %} {{ super() }} {% endblock %} {% endblock%}