tableconfig.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. $(document).ready(function() {
  2. // Select or reset table columns
  3. $('#save_tableconfig').click(function(event) {
  4. $('select[name="columns"] option').attr("selected", "selected");
  5. });
  6. $('#reset_tableconfig').click(function(event) {
  7. $('select[name="columns"]').val([]);
  8. });
  9. // Swap columns between available and selected lists
  10. $('#add_columns').click(function(e) {
  11. let selected_columns = $('#id_available_columns option:selected');
  12. $('#id_columns').append($(selected_columns).clone());
  13. $(selected_columns).remove();
  14. e.preventDefault();
  15. });
  16. $('#remove_columns').click(function(e) {
  17. let selected_columns = $('#id_columns option:selected');
  18. $('#id_available_columns').append($(selected_columns).clone());
  19. $(selected_columns).remove();
  20. e.preventDefault();
  21. });
  22. $('form.userconfigform').submit(function(event) {
  23. event.preventDefault();
  24. // Derive an array from the dotted path to the config root
  25. let path = this.getAttribute('data-config-root').split('.');
  26. let data = {};
  27. let pointer = data;
  28. // Construct a nested JSON object from the path
  29. let node;
  30. for (node of path) {
  31. pointer[node] = {};
  32. pointer = pointer[node];
  33. }
  34. // Assign the form data to the child node
  35. let field;
  36. $.each($(this).find('[id^="id_"]:input'), function(index, value) {
  37. field = $(value);
  38. pointer[field.attr("name")] = field.val();
  39. });
  40. // Make the REST API request
  41. $.ajax({
  42. url: netbox_api_path + 'users/config/',
  43. async: true,
  44. contentType: 'application/json',
  45. dataType: 'json',
  46. type: 'PATCH',
  47. beforeSend: function(xhr, settings) {
  48. xhr.setRequestHeader("X-CSRFToken", netbox_csrf_token);
  49. },
  50. data: JSON.stringify(data),
  51. }).done(function () {
  52. // Reload the page
  53. window.location.reload(true);
  54. }).fail(function (xhr, status, error) {
  55. alert("Failed to update user config (" + status + "): " + error);
  56. });
  57. });
  58. });