Skip to content

Commit cd00906

Browse files
feat(#6503): persist column visibility across page reloads
Adds DataTables stateSave with server-side callbacks posting to PreferenceSet#table_state under key "case_contacts_table". The initComplete callback syncs toggle switch state and the badge count from the loaded state, since stateLoadCallback is async. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 34133a2 commit cd00906

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

app/javascript/src/dashboard.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@ function buildExpandedContent (data) {
3737
const defineCaseContactsTable = function () {
3838
const table = $('table#case_contacts').DataTable({
3939
autoWidth: false,
40+
stateSave: true,
41+
stateSaveCallback: function (settings, data) {
42+
$.ajax({
43+
url: '/preference_sets/table_state_update/' + settings.nTable.id + '_table',
44+
data: { table_state: JSON.stringify(data) },
45+
dataType: 'json',
46+
type: 'POST',
47+
error: function (jqXHR, textStatus, errorMessage) {
48+
console.error(errorMessage)
49+
}
50+
})
51+
},
52+
stateSaveParams: function (settings, data) {
53+
data.search.search = ''
54+
return data
55+
},
56+
stateLoadCallback: function (settings, callback) {
57+
$.ajax({
58+
url: '/preference_sets/table_state/' + settings.nTable.id + '_table',
59+
dataType: 'json',
60+
type: 'GET',
61+
success: function (json) { callback(json) }
62+
})
63+
},
64+
initComplete: function () {
65+
$('.cc-column-toggle').each(function () {
66+
$(this).prop('checked', table.column($(this).data('column-index')).visible())
67+
})
68+
updateColumnsButtonBadge()
69+
},
4070
searching: true,
4171
processing: true,
4272
serverSide: true,
@@ -259,7 +289,7 @@ const defineCaseContactsTable = function () {
259289
function updateColumnsButtonBadge () {
260290
const total = $('.cc-column-toggle').length
261291
const visible = $('.cc-column-toggle:checked').length
262-
$('#cc-columns-count').text(`(${visible}/${total})`)
292+
$('#cc-columns-count').text(`(${visible}/${total})`).css('visibility', 'visible')
263293
}
264294

265295
$('#cc-columns-toggle').on('click', function () {

app/views/case_contacts/case_contacts_new_design/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<button type="button" id="cc-columns-toggle" class="main-btn secondary-btn btn-sm btn-hover">
1212
<i class="lni lni-list mr-5" aria-hidden="true"></i>
13-
Columns <span id="cc-columns-count">(<%= CaseContact::TOGGLEABLE_COLUMNS.length %>/<%= CaseContact::TOGGLEABLE_COLUMNS.length %>)</span>
13+
Columns <span id="cc-columns-count" style="visibility: hidden">(<%= CaseContact::TOGGLEABLE_COLUMNS.length %>/<%= CaseContact::TOGGLEABLE_COLUMNS.length %>)</span>
1414
</button>
1515
</div>
1616

spec/system/case_contacts/case_contacts_new_design_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@
9494
expect(page).not_to have_css("#cc-columns-panel", visible: true)
9595
end
9696

97+
it "persists hidden columns across page reloads" do
98+
click_button "Columns"
99+
uncheck "Medium"
100+
click_button "Update View"
101+
102+
visit case_contacts_new_design_path
103+
104+
expect(page).not_to have_css("th", text: "Medium")
105+
expect(page).to have_button(text: /Columns\s*\(5\/6\)/)
106+
end
107+
97108
it "lists all 6 toggleable columns with toggle switches, all on by default" do
98109
%w[Relationship Medium Contacted Topics Draft].each do |label|
99110
expect(page).to have_css("#cc-columns-panel .form-switch", text: label, visible: :all)

0 commit comments

Comments
 (0)