Skip to content

Commit

Permalink
New feature with show/hide default fields list
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleduyxyz committed Sep 3, 2024
1 parent 2ada1a2 commit ef5de57
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 46 deletions.
6 changes: 4 additions & 2 deletions app/controllers/kaui/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ def index
return
end

@dropdown_default = default_columns(Kaui.account_search_columns.call()[2], Kaui::Account::SENSIVITE_DATA_FIELDS)

@ordering = params[:ordering] || (@search_query.blank? ? 'desc' : 'asc')
@offset = params[:offset] || 0
@limit = params[:limit] || 50
@limit = params[:limit] || 4

@max_nb_records = @search_query.blank? ? Kaui::Account.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
end
Expand All @@ -44,7 +46,7 @@ def pagination
Kaui.account_search_columns.call(account, view_context)[1]
end

paginate searcher, data_extractor, formatter
paginate searcher, data_extractor, formatter, default_columns(Kaui.account_search_columns.call()[2], Kaui::Account::SENSIVITE_DATA_FIELDS)
end

def download_accounts
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/kaui/engine_controller_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_layout
end
# rubocop:enable Lint/UselessAssignment, Naming/AccessorMethodName

def paginate(searcher, data_extractor, formatter)
def paginate(searcher, data_extractor, formatter, table_default_columns = [])
search_key = (params[:search] || {})[:value].presence
offset = (params[:start] || 0).to_i
limit = (params[:length] || 10).to_i
Expand All @@ -30,7 +30,8 @@ def paginate(searcher, data_extractor, formatter)
# We need to fill-in a number to make DataTables happy
recordsTotal: pages.nil? ? 0 : (pages.pagination_max_nb_records || SIMPLE_PAGINATION_THRESHOLD),
recordsFiltered: pages.nil? ? 0 : (pages.pagination_total_nb_records || SIMPLE_PAGINATION_THRESHOLD),
data: []
data: [],
columns: table_default_columns
}
json[:error] = error unless error.nil?

Expand Down Expand Up @@ -151,5 +152,9 @@ def json_response
end
render json: response, status: response_status
end

def default_columns(fields, sensivite_fields)
fields.map { |field| { "data": fields.index(field), "visible": !(sensivite_fields.include? field) } }
end
end
end
2 changes: 2 additions & 0 deletions app/models/kaui/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Kaui
class Account < KillBillClient::Model::Account
attr_accessor :phone, :bill_cycle_day_local

SENSIVITE_DATA_FIELDS = %w[name email].freeze

def check_account_details_phone
return true if phone =~ /\A(?:\+?\d{1,3}\s*-?)?\(?(?:\d{3})?\)?[- ]?\d{3}[- ]?\d{4}\z/i

Expand Down
2 changes: 1 addition & 1 deletion app/views/kaui/accounts/_edit_columns.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<% Kaui.account_search_columns.call()[0].each_with_index do |title, index| %>
<li class="list-group-item-manual" data-id="<%= index %>">
<label class="label-group-item-manual">
<input type="checkbox" class="column-toggle" draggable="true" data-column="<%= index %>" checked> <%= title %>
<input type="checkbox" class="column-toggle" draggable="true" data-column="<%= index %>" <%= 'checked' if @dropdown_default[index][:visible] %> > <%= title %>
<span class="glyphicon glyphicon-option-vertical icon-drag" aria-hidden="true"></span>
</label>
</li>
Expand Down
76 changes: 42 additions & 34 deletions app/views/kaui/accounts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,50 @@

<%= javascript_tag do %>
$(document).ready(function() {
$.ajax({
url: "<%= accounts_pagination_path(:ordering => @ordering, :format => :json) %>",
type: "GET",
success: function(response) {
const visibleColumns = response.columns;

var table = $('#accounts-table').DataTable({
"colReorder": {
"enable": false
},
"stateSave": true,
"scrollX": true,
"dom": "<'row'r>t<'row'<'col-md-6'i><'col-md-6'p>>",
"pagingType": <% if @max_nb_records.nil? -%>"simple"<% else -%>"full_numbers"<% end -%>,
"language": {
<!-- See DefaultPaginationSqlDaoHelper.java -->
"info": <% if @max_nb_records.nil? -%>"Showing _START_ to _END_ of <%= number_with_delimiter(Kaui::EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD) -%>+ entries"<% else -%>"Showing _START_ to _END_ of _TOTAL_ entries"<% end -%>
},
"pageLength": <%= @limit %>,
"displayStart": <%= @offset %>,
<% if @search_query.blank? %>
"ordering": false,
<% elsif !@ordering.blank? %>
"order": [[ 1, "<%= @ordering %>" ]],
<% end %>
"processing": true,
"serverSide": true,
"search": {"search": "<%= @search_query %>"},
"ajax": {
url: "<%= accounts_pagination_path(:ordering => @ordering, :format => :json) %>",
dataSrc: function(json) {
var colOrder = $('#accounts-table').DataTable().colReorder.order();
var reorderedData = json.data.map(function(row) {
var newRow = [];
for (var i = 0; i < colOrder.length; i++) {
newRow.push(row[colOrder[i]]);
var table = $('#accounts-table').DataTable({
"colReorder": {
"enable": false
},
"stateSave": true,
"scrollX": true,
"dom": "<'row'r>t<'row'<'col-md-6'i><'col-md-6'p>>",
"pagingType": <% if @max_nb_records.nil? -%>"simple"<% else -%>"full_numbers"<% end -%>,
"language": {
<!-- See DefaultPaginationSqlDaoHelper.java -->
"info": <% if @max_nb_records.nil? -%>"Showing _START_ to _END_ of <%= number_with_delimiter(Kaui::EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD) -%>+ entries"<% else -%>"Showing _START_ to _END_ of _TOTAL_ entries"<% end -%>
},
"pageLength": <%= @limit %>,
"displayStart": <%= @offset %>,
<% if @search_query.blank? %>
"ordering": false,
<% elsif !@ordering.blank? %>
"order": [[ 1, "<%= @ordering %>" ]],
<% end %>
"processing": true,
"serverSide": true,
"search": {"search": "<%= @search_query %>"},
"ajax": {
url: "<%= accounts_pagination_path(:ordering => @ordering, :format => :json) %>",
dataSrc: function(json) {
var colOrder = $('#accounts-table').DataTable().colReorder.order();
var reorderedData = json.data.map(function(row) {
var newRow = [];
for (var i = 0; i < colOrder.length; i++) {
newRow.push(row[colOrder[i]]);
}
return newRow;
});
return reorderedData;
}
return newRow;
});
return reorderedData;
}
},
"columns": visibleColumns
});
}
});

Expand Down
12 changes: 5 additions & 7 deletions lib/kaui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ module Kaui
self.creditcard_plugin_name = -> { '__EXTERNAL_PAYMENT__' }

self.account_search_columns = lambda do |account = nil, view_context = nil|
# fields = KillBillClient::Model::AccountAttributes.instance_variable_get('@json_attributes')
fields = %w[account_id external_key account_balance]
original_fields = KillBillClient::Model::AccountAttributes.instance_variable_get('@json_attributes')
# Add additional fields if needed
fields = original_fields.dup

headers = fields.map { |attr| attr.split('_').join(' ').capitalize }
# Add additional headers if needed
headers << 'Child'
values = fields.map do |attr|
case attr
when 'account_id'
Expand All @@ -77,9 +77,7 @@ module Kaui
account&.send(attr.downcase)
end
end
# Add additional values if needed
values << (account.nil? || account.parent_account_id.nil? ? '' : view_context.content_tag(:span, 'Child', class: %w[label label-info account-child-label]))
[headers, values]
[headers, values, fields]
end

self.invoice_search_columns = lambda do |invoice = nil, view_context = nil, _cached_options_for_klient = nil|
Expand Down

0 comments on commit ef5de57

Please sign in to comment.