Skip to content

Commit

Permalink
Merge pull request #7059 from bgeuken/subprojects_datatables_serverside
Browse files Browse the repository at this point in the history
Use serverside pagination for rendering projects on the subproject tab
  • Loading branch information
Ana06 committed Mar 1, 2019
2 parents dcb31bd + b454098 commit ffd8dd6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 29 deletions.
28 changes: 18 additions & 10 deletions src/api/app/assets/javascripts/webui2/project.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
function projectsDatatable() {
$("#projects-datatable").dataTable({
function projectsDatatable(tableSelector, params) {
var defaultParams = {
"processing": true,
"serverSide": true,
"ajax": {
"url": $("#projects-datatable").data("source"),
"data": function (d) {
d.all = $("#projects-datatable").data("all");
}
},
"ajax": $(tableSelector).data("source"),
"pagingType": "full_numbers",
"columns": [
{"data": "name"},
Expand All @@ -16,7 +11,10 @@ function projectsDatatable() {
// pagingType is optional, if you want full pagination controls.
// Check dataTables documentation to learn more about
// available options.
});
};
var newParams = $.extend(defaultParams, params);

$(tableSelector).dataTable(newParams);
}

function toggleProjectsDatatable() {
Expand All @@ -36,6 +34,16 @@ function toggleProjectsDatatable() {
}

function initializeProjectDatatable() { // jshint ignore:line
projectsDatatable();
projectsDatatable(
"#projects-datatable",
{
"ajax": {
"url": $("#projects-datatable").data("source"),
"data": function (d) {
d.all = $("#projects-datatable").data("all");
}
}
}
);
$(".toggle-projects").click(function() { toggleProjectsDatatable(); });
}
2 changes: 1 addition & 1 deletion src/api/app/controllers/webui/project_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ def users
end

def subprojects
return if switch_to_webui2 && (Rails.env.development? || Rails.env.test?)
@subprojects = @project.subprojects.order(:name)
@parentprojects = @project.ancestors.order(:name)
parent = @project.parent
@parent_name = parent.name unless parent.nil?
@siblings = @project.siblingprojects
switch_to_webui2
end

def new
Expand Down
20 changes: 20 additions & 0 deletions src/api/app/controllers/webui2/project_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,24 @@ def webui2_index
def webui2_show
@remote_projects = Project.where.not(remoteurl: nil).pluck(:id, :name, :title)
end

def webui2_subprojects
respond_to do |format|
format.html
format.json { render json: ProjectDatatable.new(params, view_context: view_context, projects: project_for_datatable) }
end
end

private

def project_for_datatable
case params[:type]
when 'sibling project'
@project.siblingprojects
when 'subproject'
@project.subprojects.order(:name)
when 'parent project'
@project.ancestors.order(:name)
end
end
end
10 changes: 9 additions & 1 deletion src/api/app/datatables/project_datatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ def show_all
@show_all ||= options[:show_all]
end

def projects
@projects ||= options[:projects]
end

def get_raw_records
show_all ? Project.all : Project.filtered_for_list
if projects
projects
else
show_all ? Project.all : Project.filtered_for_list
end
end

def data
Expand Down
7 changes: 4 additions & 3 deletions src/api/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ def subprojects

def siblingprojects
parent_name = parent.try(:name)
return [] unless parent_name
Project.where('name like (?) and name != (?)', "#{parent_name}:%", name).order(:name).select do |sib|
return Project.none unless parent_name
projects_id = Project.where('name like (?) and name != (?)', "#{parent_name}:%", name).order(:name).select do |sib|
sib if parent_name == sib.possible_ancestor_names.first
end
end.pluck(:id)
Project.where(id: projects_id)
end

def maintained_project_names
Expand Down
11 changes: 6 additions & 5 deletions src/api/app/views/webui2/webui/project/_projects_table.html.haml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
%h3 #{table_title.pluralize} of #{project}
%table.responsive.table.table-sm.table-striped.table-bordered.projects-table
- table_id = table_title.tr(' ', '_')
%table.responsive.table.table-sm.table-striped.table-bordered.projects-table{ data: { source: project_subprojects_path(project,
type: table_title.downcase) }, id: table_id }
%thead
%tr
%th= table_title
%th Title
%tbody
- projects.each do |project|
%tr
%td.project_name= link_to(word_break(project.name), project_show_path(project))
%td.project_title= word_break(project.title)

- content_for :ready_function do
projectsDatatable("##{table_id}", {});
15 changes: 6 additions & 9 deletions src/api/app/views/webui2/webui/project/subprojects.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
.card-body
.row
.col-md-12
- if @parentprojects.present?
= render partial: 'projects_table', locals: { table_title: 'Parent Project', project: @project, projects: @parentprojects }
- if @project.ancestors.exists?
= render partial: 'projects_table', locals: { table_title: 'Parent Project', project: @project }
.row{ class: ('mt-3' if @parentprojects.present?) }
.col-md-12
- if @subprojects.present?
= render partial: 'projects_table', locals: { table_title: 'Subproject', project: @project, projects: @subprojects }
- if @project.subprojects.exists?
= render partial: 'projects_table', locals: { table_title: 'Subproject', project: @project }
- else
%p
%i This project has no subprojects
Expand All @@ -21,10 +21,7 @@
Add New Subproject
.row.mt-3
.col-md-12
- if @siblings.present?
= render partial: 'projects_table', locals: { table_title: 'Sibling Project', project: @project, projects: @siblings }
- if @project.siblingprojects.exists?
= render partial: 'projects_table', locals: { table_title: 'Sibling Project', project: @project }

= render partial: 'new_project_modal', locals: { project: @project, configuration: @configuration }

:javascript
initializeDataTable('.projects-table');

0 comments on commit ffd8dd6

Please sign in to comment.