Merge pull request 'feat(VnSelect): order data equal salix' (!918) from warmFix_order_equalSalix into dev
gitea/salix-front/pipeline/head This commit looks good
Details
gitea/salix-front/pipeline/head This commit looks good
Details
Reviewed-on: #918 Reviewed-by: Javier Segarra <jsegarra@verdnatura.es>
This commit is contained in:
commit
1f8ead608c
|
@ -3,6 +3,8 @@ import { ref, toRefs, computed, watch, onMounted, useAttrs } from 'vue';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
import FetchData from 'src/components/FetchData.vue';
|
import FetchData from 'src/components/FetchData.vue';
|
||||||
import { useRequired } from 'src/composables/useRequired';
|
import { useRequired } from 'src/composables/useRequired';
|
||||||
|
import dataByOrder from 'src/utils/dataByOrder';
|
||||||
|
|
||||||
const emit = defineEmits(['update:modelValue', 'update:options', 'remove']);
|
const emit = defineEmits(['update:modelValue', 'update:options', 'remove']);
|
||||||
const $attrs = useAttrs();
|
const $attrs = useAttrs();
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
|
@ -138,6 +140,7 @@ function findKeyInOptions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setOptions(data) {
|
function setOptions(data) {
|
||||||
|
data = dataByOrder(data, $props.sortBy);
|
||||||
myOptions.value = JSON.parse(JSON.stringify(data));
|
myOptions.value = JSON.parse(JSON.stringify(data));
|
||||||
myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
|
myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
|
||||||
emit('update:options', data);
|
emit('update:options', data);
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
function orderData(data, order) {
|
||||||
|
if (typeof order === 'function') return data.sort(data);
|
||||||
|
if (typeof order === 'string') order = [order];
|
||||||
|
if (Array.isArray(order)) {
|
||||||
|
let orderComp = [];
|
||||||
|
|
||||||
|
for (let field of order) {
|
||||||
|
let split = field.split(/\s+/);
|
||||||
|
orderComp.push({
|
||||||
|
field: split[0],
|
||||||
|
way: split[1] === 'DESC' ? -1 : 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return data.sort((a, b) => sortFunc(a, b, orderComp));
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortFunc(a, b, order) {
|
||||||
|
for (let { field, way } of order) {
|
||||||
|
let compRes = compareFunc(a[field], b[field]) * way;
|
||||||
|
if (compRes !== 0) return compRes;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function compareFunc(a, b) {
|
||||||
|
if (a === b) return 0;
|
||||||
|
let aType = typeof a;
|
||||||
|
if (aType === typeof b) {
|
||||||
|
switch (aType) {
|
||||||
|
case 'string':
|
||||||
|
return a.localeCompare(b);
|
||||||
|
case 'number':
|
||||||
|
return a - b;
|
||||||
|
case 'boolean':
|
||||||
|
return a ? 1 : -1;
|
||||||
|
case 'object':
|
||||||
|
if (a instanceof Date && b instanceof Date)
|
||||||
|
return a.getTime() - b.getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a === undefined) return -1;
|
||||||
|
if (b === undefined) return 1;
|
||||||
|
if (a === null) return -1;
|
||||||
|
if (b === null) return 1;
|
||||||
|
|
||||||
|
return a > b ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default orderData;
|
Loading…
Reference in New Issue