mirror of
https://github.com/strapi/strapi.git
synced 2025-12-13 16:08:11 +00:00
Handle boolean and numbers in search bar (bookshelf)
This commit is contained in:
parent
ddc1258429
commit
7d6e7b9d0a
@ -78,6 +78,7 @@ TableHeader.defaultProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TableHeader.propTypes = {
|
TableHeader.propTypes = {
|
||||||
|
entriesToDelete: PropTypes.array.isRequired,
|
||||||
headers: PropTypes.array.isRequired,
|
headers: PropTypes.array.isRequired,
|
||||||
onChangeSort: PropTypes.func.isRequired,
|
onChangeSort: PropTypes.func.isRequired,
|
||||||
onClickSelectAll: PropTypes.func.isRequired,
|
onClickSelectAll: PropTypes.func.isRequired,
|
||||||
|
|||||||
@ -50,18 +50,40 @@ module.exports = {
|
|||||||
const associations = this.associations.map(x => x.alias);
|
const associations = this.associations.map(x => x.alias);
|
||||||
const searchText = Object.keys(this._attributes)
|
const searchText = Object.keys(this._attributes)
|
||||||
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
||||||
.filter(attribute => ['string', 'type'].includes(this._attributes[attribute].type));
|
.filter(attribute => ['string', 'text'].includes(this._attributes[attribute].type));
|
||||||
|
|
||||||
const searchNoText = Object.keys(this._attributes)
|
const searchNoText = Object.keys(this._attributes)
|
||||||
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
||||||
.filter(attribute => !['string', 'type'].includes(this._attributes[attribute].type));
|
.filter(attribute => !['string', 'text', 'boolean', 'integer', 'decimal', 'float'].includes(this._attributes[attribute].type));
|
||||||
|
|
||||||
|
const searchInt = Object.keys(this._attributes)
|
||||||
|
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
||||||
|
.filter(attribute => ['integer', 'decimal', 'float'].includes(this._attributes[attribute].type));
|
||||||
|
|
||||||
|
const searchBool = Object.keys(this._attributes)
|
||||||
|
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
||||||
|
.filter(attribute => ['boolean'].includes(this._attributes[attribute].type));
|
||||||
|
|
||||||
|
const query = (params.search || '').replace(/[^a-zA-Z0-9.-\s]+/g, '');
|
||||||
|
|
||||||
return this.query(qb => {
|
return this.query(qb => {
|
||||||
// Search in columns which are not text value.
|
// Search in columns which are not text value.
|
||||||
searchNoText.forEach(attribute => {
|
searchNoText.forEach(attribute => {
|
||||||
qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(params.search).replace(/[^a-zA-Z. ]/g, '')}%'`);
|
qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(query)}%'`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!_.isNaN(_.toNumber(query))) {
|
||||||
|
searchInt.forEach(attribute => {
|
||||||
|
qb.orWhereRaw(`${attribute} = ${_.toNumber(query)}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query === 'true' || query === 'false') {
|
||||||
|
searchBool.forEach(attribute => {
|
||||||
|
qb.orWhereRaw(`${attribute} = ${_.toNumber(query === 'true')}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Search in columns with text using index.
|
// Search in columns with text using index.
|
||||||
switch (this.client) {
|
switch (this.client) {
|
||||||
case 'pg': {
|
case 'pg': {
|
||||||
@ -71,11 +93,11 @@ module.exports = {
|
|||||||
: `to_tsvector('${attribute}')`
|
: `to_tsvector('${attribute}')`
|
||||||
);
|
);
|
||||||
|
|
||||||
qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, params.search.replace(/[^a-zA-Z. ]/g, ''));
|
qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, query);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`);
|
qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${query}*`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,18 +121,41 @@ module.exports = {
|
|||||||
const associations = this.associations.map(x => x.alias);
|
const associations = this.associations.map(x => x.alias);
|
||||||
const searchText = Object.keys(this._attributes)
|
const searchText = Object.keys(this._attributes)
|
||||||
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
||||||
.filter(attribute => ['string', 'type'].includes(this._attributes[attribute].type));
|
.filter(attribute => ['string', 'text'].includes(this._attributes[attribute].type));
|
||||||
|
|
||||||
const searchNoText = Object.keys(this._attributes)
|
const searchNoText = Object.keys(this._attributes)
|
||||||
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
||||||
.filter(attribute => !['string', 'type'].includes(this._attributes[attribute].type));
|
.filter(attribute => !['string', 'text', 'boolean', 'integer', 'decimal', 'float'].includes(this._attributes[attribute].type));
|
||||||
|
|
||||||
|
const searchInt = Object.keys(this._attributes)
|
||||||
|
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
||||||
|
.filter(attribute => ['integer', 'decimal', 'float'].includes(this._attributes[attribute].type));
|
||||||
|
|
||||||
|
const searchBool = Object.keys(this._attributes)
|
||||||
|
.filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute))
|
||||||
|
.filter(attribute => ['boolean'].includes(this._attributes[attribute].type));
|
||||||
|
|
||||||
|
const query = (params.search || '').replace(/[^a-zA-Z0-9.-\s]+/g, '');
|
||||||
|
|
||||||
|
|
||||||
return this.query(qb => {
|
return this.query(qb => {
|
||||||
// Search in columns which are not text value.
|
// Search in columns which are not text value.
|
||||||
searchNoText.forEach(attribute => {
|
searchNoText.forEach(attribute => {
|
||||||
qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(params.search).replace(/[^a-zA-Z. ]/g, '')}%'`);
|
qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(query)}%'`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!_.isNaN(_.toNumber(query))) {
|
||||||
|
searchInt.forEach(attribute => {
|
||||||
|
qb.orWhereRaw(`${attribute} = ${_.toNumber(query)}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query === 'true' || query === 'false') {
|
||||||
|
searchBool.forEach(attribute => {
|
||||||
|
qb.orWhereRaw(`${attribute} = ${_.toNumber(query === 'true')}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Search in columns with text using index.
|
// Search in columns with text using index.
|
||||||
switch (this.client) {
|
switch (this.client) {
|
||||||
case 'pg': {
|
case 'pg': {
|
||||||
@ -120,11 +165,11 @@ module.exports = {
|
|||||||
: `to_tsvector('${attribute}')`
|
: `to_tsvector('${attribute}')`
|
||||||
);
|
);
|
||||||
|
|
||||||
qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, params.search.replace(/[^a-zA-Z. ]/g, ''));
|
qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, query);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`);
|
qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${query}*`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}).count();
|
}).count();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user