Merge PR #4420: Fix Search Input Value Preservation
Fixed search input value preservation issue: - Search value now stays visible in input field after results load - Users can modify search by adding/removing characters - Search automatically updates when text is modified - Focus and cursor position are maintained - All previous fixes included (2-char minimum, visible counter, proper pagination)
This commit is contained in:
commit
e0a033c6fb
|
|
@ -8,6 +8,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
ListRenderer.include({
|
||||
events: _.extend({}, ListRenderer.prototype.events, {
|
||||
'keyup .oe_search_input': '_onSearchKeyUp',
|
||||
'input .oe_search_input': '_onSearchInput', // Add input event
|
||||
'click .oe_clear_search': '_onClearSearchClick'
|
||||
}),
|
||||
|
||||
|
|
@ -25,6 +26,26 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
self._addSearchBox();
|
||||
self._initSearchState();
|
||||
}
|
||||
// Restore search value if exists
|
||||
self._restoreSearchInput();
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @override - Hook into render body to preserve search
|
||||
*/
|
||||
_renderBody: function () {
|
||||
var searchValue = this._search ? this._search.value : '';
|
||||
var self = this;
|
||||
|
||||
return this._super.apply(this, arguments).then(function(result) {
|
||||
// Restore search value after render
|
||||
if (searchValue) {
|
||||
setTimeout(function() {
|
||||
self._restoreSearchInput();
|
||||
}, 0);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
},
|
||||
|
||||
|
|
@ -52,12 +73,20 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
originalData: null,
|
||||
originalDomain: null,
|
||||
filteredIds: null,
|
||||
isSearching: false // Flag to prevent input clearing
|
||||
preserveInput: true // Flag to preserve input value
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Handle search input
|
||||
* Handle search input event (for immediate value tracking)
|
||||
*/
|
||||
_onSearchInput: function(e) {
|
||||
// Store value immediately on any input
|
||||
this._search.value = $(e.currentTarget).val().trim();
|
||||
},
|
||||
|
||||
/**
|
||||
* Handle search input keyup
|
||||
*/
|
||||
_onSearchKeyUp: function(e) {
|
||||
var self = this;
|
||||
|
|
@ -123,9 +152,6 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
this._search.originalDomain = this.state.domain || [];
|
||||
}
|
||||
|
||||
// Set searching flag
|
||||
this._search.isSearching = true;
|
||||
|
||||
// Start search
|
||||
this._search.active = true;
|
||||
this._showLoading(true);
|
||||
|
|
@ -141,9 +167,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
// Get fields to read
|
||||
var fields = this._getReadFields();
|
||||
|
||||
// Execute search with pagination
|
||||
var limit = this.state.limit || 80; // Use current page limit
|
||||
|
||||
// Execute search
|
||||
this._rpc({
|
||||
model: this.state.model,
|
||||
method: 'search_read',
|
||||
|
|
@ -154,16 +178,19 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
context: this.state.context || {}
|
||||
}
|
||||
}).then(function(results) {
|
||||
// Always preserve the search value
|
||||
self._search.preserveInput = true;
|
||||
self._displayResults(results);
|
||||
}).catch(function(error) {
|
||||
console.error('Search failed:', error);
|
||||
self._clientSideSearch(value);
|
||||
}).finally(function() {
|
||||
self._search.active = false;
|
||||
self._search.isSearching = false;
|
||||
self._showLoading(false);
|
||||
// Restore search value after search completes
|
||||
self._restoreSearchInput();
|
||||
// Always restore search value after operations
|
||||
setTimeout(function() {
|
||||
self._restoreSearchInput();
|
||||
}, 100);
|
||||
});
|
||||
},
|
||||
|
||||
|
|
@ -200,7 +227,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
|
||||
// Always return as array
|
||||
if (conditions.length === 1) {
|
||||
return conditions; // Return as array, not conditions[0]
|
||||
return conditions;
|
||||
}
|
||||
|
||||
// Add OR operators
|
||||
|
|
@ -286,6 +313,8 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
|
||||
if (count === 0) {
|
||||
this._showNoResults();
|
||||
// Still preserve search input
|
||||
this._restoreSearchInput();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -316,7 +345,6 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
if (this.getParent() && typeof this.getParent().reload === 'function') {
|
||||
try {
|
||||
// Update state temporarily
|
||||
var originalDomain = this.state.domain;
|
||||
this.state.domain = idDomain;
|
||||
|
||||
// Store total count for pagination
|
||||
|
|
@ -326,15 +354,16 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
this.getParent().reload({
|
||||
domain: idDomain,
|
||||
context: this.state.context,
|
||||
limit: limit, // Keep original pagination limit
|
||||
limit: limit,
|
||||
offset: offset
|
||||
}).then(function() {
|
||||
// Restore search input and count
|
||||
self._restoreSearchInput();
|
||||
self.$('.oe_search_count').text(_t('Found: ') + records.length + _t(' records')).show();
|
||||
// Force restore search input and count after reload
|
||||
setTimeout(function() {
|
||||
self._forceRestoreSearchState();
|
||||
self.$('.oe_search_count').text(_t('Found: ') + records.length + _t(' records')).show();
|
||||
}, 100);
|
||||
}).catch(function(err) {
|
||||
console.warn('Parent reload failed:', err);
|
||||
// Fallback method
|
||||
self._renderRecordsWithPagination(records, limit, offset);
|
||||
});
|
||||
|
||||
|
|
@ -360,7 +389,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
// Update state
|
||||
if (this.state && this.state.data) {
|
||||
this.state.data.records = pageRecords;
|
||||
this.state.count = records.length; // Total count
|
||||
this.state.count = records.length;
|
||||
this.state.res_ids = pageRecords.map(function(r) { return r.id; });
|
||||
}
|
||||
|
||||
|
|
@ -376,11 +405,11 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
|
||||
if (result && result.then) {
|
||||
result.then(function() {
|
||||
self._restoreSearchInput();
|
||||
self._forceRestoreSearchState();
|
||||
self.$('.oe_search_count').text(_t('Found: ') + records.length + _t(' records')).show();
|
||||
});
|
||||
} else {
|
||||
self._restoreSearchInput();
|
||||
self._forceRestoreSearchState();
|
||||
self.$('.oe_search_count').text(_t('Found: ') + records.length + _t(' records')).show();
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
@ -392,15 +421,32 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Force restore search state (input value and UI)
|
||||
*/
|
||||
_forceRestoreSearchState: function() {
|
||||
if (this._search && this._search.value && this._search.preserveInput) {
|
||||
var $input = this.$('.oe_search_input');
|
||||
if ($input.length && $input.val() !== this._search.value) {
|
||||
$input.val(this._search.value);
|
||||
$input.focus(); // Keep focus on input
|
||||
// Set cursor position to end
|
||||
var input = $input[0];
|
||||
if (input.setSelectionRange) {
|
||||
var len = this._search.value.length;
|
||||
input.setSelectionRange(len, len);
|
||||
}
|
||||
}
|
||||
this.$('.oe_clear_search').show();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Restore search input value
|
||||
*/
|
||||
_restoreSearchInput: function() {
|
||||
// Don't restore if we're clearing
|
||||
if (this._search && this._search.value && !this._search.isClearing) {
|
||||
this.$('.oe_search_input').val(this._search.value);
|
||||
this.$('.oe_clear_search').show();
|
||||
}
|
||||
// Use force restore for consistency
|
||||
this._forceRestoreSearchState();
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
@ -426,7 +472,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
var limit = this.state.limit || 80;
|
||||
|
||||
$rows.each(function() {
|
||||
if (shown >= limit) return false; // Stop if reached limit
|
||||
if (shown >= limit) return false;
|
||||
|
||||
var $row = $(this);
|
||||
var rowId = self._getRowId($row);
|
||||
|
|
@ -442,7 +488,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
this._showNoResults();
|
||||
}
|
||||
|
||||
this._restoreSearchInput();
|
||||
this._forceRestoreSearchState();
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
@ -493,7 +539,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
this.$('.o_data_row').each(function() {
|
||||
if (visible >= limit) {
|
||||
$(this).hide();
|
||||
return true; // Continue but hide excess rows
|
||||
return true;
|
||||
}
|
||||
|
||||
var $row = $(this);
|
||||
|
|
@ -504,7 +550,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
if (match) visible++;
|
||||
});
|
||||
|
||||
// Update count - KEEP VISIBLE
|
||||
// Update count
|
||||
var msg = visible > 0 ?
|
||||
_t('Found: ') + visible + _t(' records') :
|
||||
_t('No records found');
|
||||
|
|
@ -514,15 +560,15 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
this._showNoResults();
|
||||
}
|
||||
|
||||
this._restoreSearchInput();
|
||||
this._forceRestoreSearchState();
|
||||
},
|
||||
|
||||
/**
|
||||
* Clear search and restore original view
|
||||
*/
|
||||
_clearSearch: function() {
|
||||
// Set clearing flag
|
||||
this._search.isClearing = true;
|
||||
// Don't preserve input when clearing
|
||||
this._search.preserveInput = false;
|
||||
|
||||
// Clear UI
|
||||
this.$('.oe_search_input').val('');
|
||||
|
|
@ -552,8 +598,10 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
|
|||
});
|
||||
}
|
||||
|
||||
// Remove clearing flag
|
||||
this._search.isClearing = false;
|
||||
// Re-enable preserve for future searches
|
||||
setTimeout(function() {
|
||||
this._search.preserveInput = true;
|
||||
}.bind(this), 100);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue