Fix: Handle _renderBody not returning Promise and improve search logic

This commit is contained in:
Mohamed Eltayar 2025-08-29 16:43:27 +03:00
parent 8ade7ca729
commit fa4e0e0090
1 changed files with 53 additions and 32 deletions

View File

@ -70,7 +70,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
_performSearch: function (searchValue) {
var self = this;
var value = (searchValue || '').toLowerCase().trim();
var value = (searchValue || '').trim();
// Prevent concurrent searches
if (this._isSearching) {
@ -91,7 +91,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
this._isSearching = true;
// Store original records if not already stored
if (!this._originalRecords && this.state.data.records) {
if (!this._originalRecords && this.state.data && this.state.data.records) {
this._originalRecords = this.state.data.records.slice();
}
@ -103,10 +103,11 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
this._isSearching = false;
this.$el.find('.oe_search_loading').hide();
console.warn('No searchable fields found for model:', model);
this._showError(_t('No searchable fields available'));
return;
}
// Build search domain
// Build search domain - make it case insensitive
var domain = this._buildSearchDomain(value, fields);
// Add existing domain if any
@ -131,6 +132,9 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
}
}
console.log('Searching with domain:', domain);
console.log('Fields to read:', fieldsToRead);
// Perform RPC search with specific fields only - NO LIMIT
this._rpc({
model: model,
@ -144,6 +148,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
order: orderBy
}
}).then(function(result) {
console.log('Search results:', result.length, 'records found');
self._updateListWithSearchResults(result, value);
}).catch(function(error) {
console.error('Search error:', error);
@ -173,14 +178,8 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
return; // Skip this field
}
// For many2one, we only need the ID and display_name
if (field.type === 'many2one') {
fields.push(fieldName);
}
// For other fields, include if they're stored
else if (field.store !== false) {
fields.push(fieldName);
}
// Include all other fields
fields.push(fieldName);
}
}
});
@ -207,7 +206,8 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
_.each(fields, function(field) {
// For char, text, and html fields
if (['char', 'text', 'html'].includes(field.type)) {
if (['char', 'text', 'html', 'selection'].includes(field.type)) {
// Use ilike for case-insensitive search
orConditions.push([field.name, 'ilike', value]);
// Also search with normalized value if different
if (normalizedValue !== value) {
@ -219,14 +219,14 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
// Use the field name directly with ilike
orConditions.push([field.name, 'ilike', value]);
}
// For selection fields
else if (field.type === 'selection') {
orConditions.push([field.name, 'ilike', value]);
}
// For number fields (if value is numeric)
else if (['integer', 'float', 'monetary'].includes(field.type)) {
if (!isNaN(value) && value !== '') {
orConditions.push([field.name, '=', parseFloat(value)]);
var numValue = parseFloat(value);
orConditions.push([field.name, '=', numValue]);
// Also search for approximate values
orConditions.push([field.name, '>=', numValue - 0.01]);
orConditions.push([field.name, '<=', numValue + 0.01]);
}
}
});
@ -266,7 +266,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
fields.push({
name: fieldName,
type: field.type,
string: field.string
string: field.string || fieldName
});
addedFields[fieldName] = true;
}
@ -286,7 +286,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
fields.push({
name: fieldName,
type: field.type,
string: field.string
string: field.string || fieldName
});
addedFields[fieldName] = true;
}
@ -294,6 +294,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
});
}
console.log('Searchable fields:', fields);
return fields;
},
@ -317,15 +318,24 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
this._searchMode = true;
// Re-render the body with new records
this._renderBody().then(function() {
// Restore search input value
// Wrap in Promise to ensure consistent behavior
var renderPromise = this._renderBody();
if (renderPromise && typeof renderPromise.then === 'function') {
renderPromise.then(function() {
// Restore search input value
self.$el.find('.oe_search_input').val(self._currentSearchValue);
self.$el.find('.oe_clear_search').show();
$countEl.show();
}).catch(function(error) {
console.error('Error rendering search results:', error);
self._showError(_t('Error displaying results'));
});
} else {
// If _renderBody doesn't return a promise, just update UI
self.$el.find('.oe_search_input').val(self._currentSearchValue);
self.$el.find('.oe_clear_search').show();
$countEl.show();
}).catch(function(error) {
console.error('Error rendering search results:', error);
self._showError(_t('Error displaying results'));
});
}
} else {
$countEl.text(_t('No records found')).show();
@ -378,15 +388,25 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
this._searchMode = false;
// If we have original records, restore them
if (this._originalRecords) {
if (this._originalRecords && this._originalRecords.length > 0) {
this.state.data.records = this._originalRecords;
this._renderBody().then(function() {
self._originalRecords = null;
}).catch(function(error) {
console.error('Error restoring original records:', error);
// Just show all rows as fallback
// Wrap _renderBody in Promise for consistent behavior
var renderPromise = this._renderBody();
if (renderPromise && typeof renderPromise.then === 'function') {
renderPromise.then(function() {
self._originalRecords = null;
}).catch(function(error) {
console.error('Error restoring original records:', error);
// Just show all rows as fallback
self.$el.find('.o_data_row').show();
self._originalRecords = null;
});
} else {
// If _renderBody doesn't return a promise
self.$el.find('.o_data_row').show();
});
self._originalRecords = null;
}
} else {
// Just show all rows
this.$el.find('.o_data_row').show();
@ -398,6 +418,7 @@ odoo.define('fims_general_search_tree_view.list_search', function (require) {
// Normalizing Arabic text by removing common variations
return text
.toLowerCase() // Make lowercase for better matching
.replace(/[\u064B-\u065F]/g, '') // Remove diacritics
.replace(/[\u0660-\u0669]/g, (d) => String.fromCharCode(d.charCodeAt(0) - 0x0660 + 0x0030)) // Arabic-Indic digits
.replace(/[\u06F0-\u06F9]/g, (d) => String.fromCharCode(d.charCodeAt(0) - 0x06F0 + 0x0030)) // Extended Arabic-Indic