From d212707a6bb472803335f65608b144e4941c945f Mon Sep 17 00:00:00 2001 From: Eugene Alvin Villar Date: Thu, 6 Oct 2016 19:13:02 +0800 Subject: [PATCH] Generalize solution --- cadasta/core/static/js/dataTables.forms.js | 37 +++++++++++++++++++ .../organization_members_edit.html | 15 ++------ .../organization/project_add_permissions.html | 13 ++++++- .../project_edit_permissions.html | 12 +++++- .../party/relationship_resources_add.html | 4 +- cadasta/templates/party/resources_add.html | 4 +- .../resources/project_add_existing.html | 4 +- .../templates/resources/script_add_lib.html | 21 +++-------- cadasta/templates/spatial/resources_add.html | 4 +- 9 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 cadasta/core/static/js/dataTables.forms.js diff --git a/cadasta/core/static/js/dataTables.forms.js b/cadasta/core/static/js/dataTables.forms.js new file mode 100644 index 000000000..15f7294ee --- /dev/null +++ b/cadasta/core/static/js/dataTables.forms.js @@ -0,0 +1,37 @@ +'use strict'; + +// This function should be called for any DataTable inside an HTML form that +// contains form fields. This ensures that when the form is submitted, any form +// fields that are hidden because of the DataTable paging/search/filter +// functionality is still submitted with the form. +function activateFormFieldsInDataTable( + formSelector, // jQuery selector for the HTML form + columnIndex, // column index in the DataTable that contains the form fields + fieldType // form field type as a string (supported: 'checkbox', 'select') +) { + var form = $(formSelector); + form.submit(function() { + var cells = $(form).find('.datatable').DataTable().column(columnIndex).nodes(); + for (var i = 0; i < cells.length; i++) { + if (!document.body.contains(cells[i])) { + var cell = $(cells[i]); + if (fieldType == 'select') { + var select = cell.find('select'); + if (select.length > 0) { + form.append(''); + } + } + else if (fieldType == 'checkbox') { + var checkbox = cell.find('input[type="checkbox"]')[0]; + if (checkbox && checkbox.checked) { + form.append(''); + } + } + else { + console.log('activateFormFieldsInDataTables: "' + fieldType + '" is not supported.'); + } + } + } + return true; + }); +} diff --git a/cadasta/templates/organization/organization_members_edit.html b/cadasta/templates/organization/organization_members_edit.html index b5aa759e6..5570dbac3 100644 --- a/cadasta/templates/organization/organization_members_edit.html +++ b/cadasta/templates/organization/organization_members_edit.html @@ -2,13 +2,13 @@ {% load i18n %} {% load widget_tweaks %} +{% load staticfiles %} {% block page_title %}{% trans "Edit member" %} | {% endblock %} {% block left-nav %}members{% endblock %} {% block content %} -
@@ -109,20 +109,11 @@

{% trans "Member Permissions" %}

{% endblock %} {% block extra_script %} + {% endblock %} diff --git a/cadasta/templates/organization/project_add_permissions.html b/cadasta/templates/organization/project_add_permissions.html index 2e5c400c5..e845eabf3 100644 --- a/cadasta/templates/organization/project_add_permissions.html +++ b/cadasta/templates/organization/project_add_permissions.html @@ -1,7 +1,8 @@ {% extends "organization/project_add_wrapper.html" %} -{% load widget_tweaks %} {% load i18n %} +{% load widget_tweaks %} +{% load staticfiles %} {% block page_title %}| {% trans "Assign team" %}{% endblock %} @@ -74,3 +75,13 @@ {% endblock %} + +{% block extra_script %} + + +{% endblock %} diff --git a/cadasta/templates/organization/project_edit_permissions.html b/cadasta/templates/organization/project_edit_permissions.html index 6f046868e..2065fff42 100644 --- a/cadasta/templates/organization/project_edit_permissions.html +++ b/cadasta/templates/organization/project_edit_permissions.html @@ -1,8 +1,8 @@ {% extends "organization/project_wrapper.html" %} -{% load staticfiles %} {% load i18n %} {% load widget_tweaks %} +{% load staticfiles %} {% block page_title %}{% trans "Edit member permissions" %} | {% endblock %} @@ -13,7 +13,6 @@ {% block left-nav %}overview{% endblock %} {% block content %} -
{% csrf_token %}
@@ -45,5 +44,14 @@

{% trans "Edit member permissions" %}

+{% endblock %} +{% block extra_script %} + + {% endblock %} diff --git a/cadasta/templates/party/relationship_resources_add.html b/cadasta/templates/party/relationship_resources_add.html index a67719f30..e7d223eb1 100644 --- a/cadasta/templates/party/relationship_resources_add.html +++ b/cadasta/templates/party/relationship_resources_add.html @@ -1,10 +1,12 @@ {% extends "party/relationship_detail.html" %} {% load i18n %} +{% load staticfiles %} {% block page_title %}{% trans "Add new resource for relationship" %} | {% endblock %} {% block location_extra_script %} - {% include 'resources/script_add_lib.html' %} + +{% include 'resources/script_add_lib.html' %} {% endblock %} {% block modals %} diff --git a/cadasta/templates/party/resources_add.html b/cadasta/templates/party/resources_add.html index 82e8cf16d..db8c150ef 100644 --- a/cadasta/templates/party/resources_add.html +++ b/cadasta/templates/party/resources_add.html @@ -1,10 +1,12 @@ {% extends "party/party_detail.html" %} {% load i18n %} +{% load staticfiles %} {% block page_title %}{% trans "Add new resource for party" %} | {% endblock %} {% block extra_script %} - {% include 'resources/script_add_lib.html' %} + +{% include 'resources/script_add_lib.html' %} {% endblock %} {% block modals %} diff --git a/cadasta/templates/resources/project_add_existing.html b/cadasta/templates/resources/project_add_existing.html index 589339974..bfe482dbd 100644 --- a/cadasta/templates/resources/project_add_existing.html +++ b/cadasta/templates/resources/project_add_existing.html @@ -1,10 +1,12 @@ {% extends "resources/project_list.html" %} {% load i18n %} +{% load staticfiles %} {% block page_title %}{% trans "Add new resource for project" %} | {% endblock %} {% block extra_script %} - {% include 'resources/script_add_lib.html' %} + +{% include 'resources/script_add_lib.html' %} {% endblock %} {% block modals %} diff --git a/cadasta/templates/resources/script_add_lib.html b/cadasta/templates/resources/script_add_lib.html index 7d31cb3f1..fabf54698 100644 --- a/cadasta/templates/resources/script_add_lib.html +++ b/cadasta/templates/resources/script_add_lib.html @@ -1,23 +1,12 @@ diff --git a/cadasta/templates/spatial/resources_add.html b/cadasta/templates/spatial/resources_add.html index cdadefa6a..ddb9f0769 100644 --- a/cadasta/templates/spatial/resources_add.html +++ b/cadasta/templates/spatial/resources_add.html @@ -1,10 +1,12 @@ {% extends "spatial/location_detail.html" %} {% load i18n %} +{% load staticfiles %} {% block page_title %}{% trans "Add new resource for location" %} | {% endblock %} {% block location_extra_script %} - {% include 'resources/script_add_lib.html' %} + +{% include 'resources/script_add_lib.html' %} {% endblock %} {% block modals %}