0
0
Fork 0

feat: addRow withour dialog

This commit is contained in:
Javier Segarra 2024-07-17 20:33:55 +02:00
parent 0e5e7c3fda
commit ebf329bfc6
2 changed files with 66 additions and 37 deletions

View File

@ -45,6 +45,10 @@ const $props = defineProps({
type: Object, type: Object,
default: null, default: null,
}, },
createAsDialog: {
type: Boolean,
default: true,
},
cardClass: { cardClass: {
type: String, type: String,
default: 'flex-one', default: 'flex-one',
@ -239,6 +243,11 @@ defineExpose({
redirect: redirectFn, redirect: redirectFn,
selected, selected,
}); });
function handleOnDataSaved(_, res) {
if (_.onDataSaved) _.onDataSaved(this);
else $props.create.onDataSaved(_);
}
</script> </script>
<template> <template>
<QDrawer <QDrawer
@ -556,8 +565,16 @@ defineExpose({
</template> </template>
</CrudModel> </CrudModel>
</div> </div>
<QPageSticky v-if="create" :offset="[20, 20]" style="z-index: 2"> <QPageSticky :offset="[20, 20]" style="z-index: 2">
<QBtn @click="showForm = !showForm" color="primary" fab icon="add" /> <QBtn
@click="
() =>
createAsDialog ? (showForm = !showForm) : handleOnDataSaved(create)
"
color="primary"
fab
icon="add"
/>
<QTooltip> <QTooltip>
{{ create.title }} {{ create.title }}
</QTooltip> </QTooltip>
@ -566,7 +583,7 @@ defineExpose({
<FormModelPopup <FormModelPopup
v-bind="create" v-bind="create"
:model="$attrs['data-key'] + 'Create'" :model="$attrs['data-key'] + 'Create'"
@on-data-saved="(_, res) => create.onDataSaved(res)" @on-data-saved="handleOnDataSaved"
> >
<template #form-inputs="{ data }"> <template #form-inputs="{ data }">
<div class="grid-create"> <div class="grid-create">

View File

@ -192,6 +192,10 @@ const columns = computed(() => [
field: 'warehouseFk', field: 'warehouseFk',
name: 'warehouseFk', name: 'warehouseFk',
...defaultColumnAttrs, ...defaultColumnAttrs,
component: 'select',
attrs: {
options: warehousesOptions,
},
}, },
{ {
align: 'right', align: 'right',
@ -230,16 +234,19 @@ const validations = (row, rowIndex, col) => {
); );
}; };
async function upsertFixedPrice(row) { const updateMinPrice = async (value, props) => {
try { // El checkbox hasMinPrice se encuentra en la misma columna que el input hasMinPrice
const { data } = await axios.patch('FixedPrices/upsertFixedPrice', row); // Por lo tanto le mandamos otro objeto con las mismas propiedades pero con el campo 'field' cambiado
return data; props.row.hasMinPrice = value;
} catch (err) { await upsertPrice({
console.error('Error editing price', err); row: props.row,
} col: { field: 'hasMinPrice' },
} rowIndex: props.rowIndex,
});
};
const upsertPrice = async ({ row, col, rowIndex }, resetMinPrice = false) => { const upsertPrice = async ({ row, col, rowIndex }, resetMinPrice = false) => {
if (!validations(row, rowIndex, col)) return; // if (!validations(row, rowIndex, col)) return;
try { try {
if (resetMinPrice) row.hasMinPrice = 0; if (resetMinPrice) row.hasMinPrice = 0;
@ -249,6 +256,14 @@ const upsertPrice = async ({ row, col, rowIndex }, resetMinPrice = false) => {
console.error('Error editing price', err); console.error('Error editing price', err);
} }
}; };
async function upsertFixedPrice(row) {
try {
const { data } = await axios.patch('FixedPrices/upsertFixedPrice', row);
return data;
} catch (err) {
console.error('Error editing price', err);
}
}
async function saveOnRowChange(row) { async function saveOnRowChange(row) {
if (rowsSelected.value.length > 1) return; if (rowsSelected.value.length > 1) return;
if (rowsSelected.value[0]?.id === row.id) return; if (rowsSelected.value[0]?.id === row.id) return;
@ -299,24 +314,24 @@ function checkLastVisibleRow() {
} }
const addRow = (fixedPrice = null) => { const addRow = (fixedPrice = null) => {
const lastvisible = checkLastVisibleRow(); const lastvisible = checkLastVisibleRow();
if (!fixedPrices.value || fixedPrices.value.length === 0) { // if (!fixedPrices.value || fixedPrices.value.length === 0) {
fixedPrices.value = []; // fixedPrices.value = [];
const today = Date.vnNew(); // const today = Date.vnNew();
const millisecsInDay = 86400000; // const millisecsInDay = 86400000;
const daysInWeek = 7; // const daysInWeek = 7;
const nextWeek = new Date(today.getTime() + daysInWeek * millisecsInDay); // const nextWeek = new Date(today.getTime() + daysInWeek * millisecsInDay);
const newPrice = { // const newPrice = {
started: today, // started: today,
ended: nextWeek, // ended: nextWeek,
hasMinPrice: 0, // hasMinPrice: 0,
}; // };
fixedPricesOriginalData.value.push({ ...newPrice }); // fixedPricesOriginalData.value.push({ ...newPrice });
fixedPrices.value.push({ ...newPrice }); // fixedPrices.value.push({ ...newPrice });
return; // return;
} // }
if (fixedPrice) { if (fixedPrice) {
lastItem.value = fixedPrice; lastItem.value = fixedPrice;
return; return;
@ -326,7 +341,7 @@ const addRow = (fixedPrice = null) => {
JSON.stringify(fixedPrices.value[fixedPrices.value.length - 1]) JSON.stringify(fixedPrices.value[fixedPrices.value.length - 1])
); );
lastItem.value = lastItemCopy; lastItem.value = lastItemCopy;
// tableRef.value.create.formInitialData = lastItem; tableRef.value.create.formInitialData = lastItem;
delete lastItemCopy.id; delete lastItemCopy.id;
fixedPricesOriginalData.value.splice(lastvisible, 0, lastItemCopy); fixedPricesOriginalData.value.splice(lastvisible, 0, lastItemCopy);
fixedPrices.value.unshift(lastItemCopy); fixedPrices.value.unshift(lastItemCopy);
@ -523,7 +538,7 @@ onUnmounted(() => (stateStore.rightDrawer = false));
{{ row.name }} {{ row.name }}
</span> </span>
<ItemDescriptorProxy :id="row.itemFk" /> <ItemDescriptorProxy :id="row.itemFk" />
<FetchedTags style="width: 200px" :item="row" :max-length="6" /> <FetchedTags style="max-width: 220px" :item="row" :max-length="6" />
</QTd> </QTd>
</template> </template>
<template #column-rate2="props"> <template #column-rate2="props">
@ -556,8 +571,8 @@ onUnmounted(() => (stateStore.rightDrawer = false));
<QCheckbox <QCheckbox
:model-value="props.row.hasMinPrice" :model-value="props.row.hasMinPrice"
@update:model-value="updateMinPrice($event, props)" @update:model-value="updateMinPrice($event, props)"
:true-value="'true'" :false-value="0"
:false-value="'false'" :true-value="1"
/> />
<VnInput <VnInput
class="col" class="col"
@ -640,12 +655,6 @@ onUnmounted(() => (stateStore.rightDrawer = false));
</QTd> </QTd>
</template> </template>
</VnTable> </VnTable>
<QPageSticky :offset="[20, 0]">
<QBtn @click="addRow()" color="primary" fab icon="add" />
<QTooltip>
{{ t('Add') }}
</QTooltip>
</QPageSticky>
<QDialog ref="editTableCellDialogRef"> <QDialog ref="editTableCellDialogRef">
<EditTableCellValueForm <EditTableCellValueForm
@ -670,6 +679,9 @@ onUnmounted(() => (stateStore.rightDrawer = false));
& .q-checkbox { & .q-checkbox {
& .q-checkbox__inner { & .q-checkbox__inner {
position: relative !important; position: relative !important;
&.q-checkbox__inner--truthy {
color: var(--q-primary);
}
} }
} }
} }