feat: refs #7793 sortByWeight
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Javier Segarra 2024-09-25 15:25:43 +02:00
parent 8c13ceb3fe
commit 3476b4807f
1 changed files with 18 additions and 1 deletions

View File

@ -86,6 +86,10 @@ const $props = defineProps({
type: Boolean, type: Boolean,
default: false, default: false,
}, },
sortByWeight: {
type: Boolean,
default: false,
},
}); });
const { validations } = useValidator(); const { validations } = useValidator();
const requiredFieldRule = (val) => validations().required($attrs.required, val); const requiredFieldRule = (val) => validations().required($attrs.required, val);
@ -186,11 +190,24 @@ async function fetchFilter(val) {
$props.exprBuilder && Object.assign(where, $props.exprBuilder(key, val)); $props.exprBuilder && Object.assign(where, $props.exprBuilder(key, val));
const fetchOptions = { where, include, limit }; const fetchOptions = { where, include, limit };
if (fields) fetchOptions.fields = fields; if (fields) fetchOptions.fields = fields;
if (sortBy) fetchOptions.order = sortBy; if (sortBy) {
let sort = sortBy;
if (typeof sort === 'string') sort = [getOrderCaseString(key, val), sortBy];
else sort = [getOrderCaseString(key, val), ...sortBy];
fetchOptions.order = sort;
}
return dataRef.value.fetch(fetchOptions); return dataRef.value.fetch(fetchOptions);
} }
function getOrderCaseString(prop, value) {
return `CASE
WHEN ${prop} LIKE '${value}%' THEN 1
WHEN ${prop} LIKE '%${value}%' THEN 2
ELSE 3
END, ${prop} DESC`;
}
async function filterHandler(val, update) { async function filterHandler(val, update) {
if (!val && lastVal.value === val) { if (!val && lastVal.value === val) {
lastVal.value = val; lastVal.value = val;