Skip to content

Commit

Permalink
Merge pull request #784 from nextcloud/backport/710/stable0.6
Browse files Browse the repository at this point in the history
[stable0.6] Fix number column issues
  • Loading branch information
blizzz authored Jan 19, 2024
2 parents 927e41c + a3a51f7 commit 0c746a4
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 39 deletions.
40 changes: 11 additions & 29 deletions lib/Service/ColumnService.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function find(int $id, string $userId = null): Column {
* @param string|null $selectionOptions
* @param string|null $selectionDefault
* @param string|null $datetimeDefault
* @param array|null $selectedViewIds
* @param array $selectedViewIds
* @return Column
*
* @throws DoesNotExistException
Expand Down Expand Up @@ -185,7 +185,7 @@ public function create(
?string $selectionDefault,

?string $datetimeDefault,
?array $selectedViewIds
array $selectedViewIds = []
):Column {
// security
if ($viewId) {
Expand Down Expand Up @@ -321,39 +321,21 @@ public function update(
if ($mandatory !== null) {
$item->setMandatory($mandatory);
}
if ($description !== null) {
$item->setDescription($description);
}
if ($textDefault !== null) {
$item->setTextDefault($textDefault);
}
if ($textAllowedPattern !== null) {
$item->setTextAllowedPattern($textAllowedPattern);
}
if ($textMaxLength !== null) {
$item->setTextMaxLength($textMaxLength);
}
if ($numberDefault !== null) {
$item->setNumberDefault($numberDefault);
}
if ($numberMin !== null) {
$item->setNumberMin($numberMin);
}
if ($numberMax !== null) {
$item->setNumberMax($numberMax);
}
if ($numberDecimals !== null) {
$item->setNumberDecimals($numberDecimals);
}
$item->setDescription($description);
$item->setTextDefault($textDefault);
$item->setTextAllowedPattern($textAllowedPattern);
$item->setTextMaxLength($textMaxLength);
$item->setNumberDefault($numberDefault);
$item->setNumberMin($numberMin);
$item->setNumberMax($numberMax);
$item->setNumberDecimals($numberDecimals);
if ($selectionOptions !== null) {
$item->setSelectionOptions($selectionOptions);
}
if ($selectionDefault !== null) {
$item->setSelectionDefault($selectionDefault);
}
if ($datetimeDefault !== null) {
$item->setDatetimeDefault($datetimeDefault);
}
$item->setDatetimeDefault($datetimeDefault);

$time = new DateTime();
$item->setLastEditAt($time->format('Y-m-d H:i:s'));
Expand Down
3 changes: 3 additions & 0 deletions lib/Service/ColumnTypes/NumberBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class NumberBusiness extends SuperBusiness implements IColumnTypeBusiness {
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
if ($value === null) {
return '';
}
return json_encode(floatval($value));
}

Expand Down
5 changes: 5 additions & 0 deletions src/modules/modals/EditColumn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,18 @@ export default {
async updateColumn() {
const data = Object.assign({}, this.editColumn)
if ((this.column.type === ColumnTypes.SelectionMulti || this.column.type === ColumnTypes.SelectionCheck) && data.selectionDefault !== null) data.selectionDefault = JSON.stringify(data.selectionDefault)
data.numberDefault = data.numberDefault === '' ? null : data.numberDefault
data.numberDecimals = data.numberDecimals === '' ? null : data.numberDecimals
data.numberMin = data.numberMin === '' ? null : data.numberMin
data.numberMax = data.numberMax === '' ? null : data.numberMax
delete data.type
delete data.id
delete data.tableId
delete data.createdAt
delete data.createdBy
delete data.lastEditAt
delete data.lastEditBy
console.debug('this column data will be send', data)
const res = await this.$store.dispatch('updateColumn', { id: this.editColumn.id, data })
if (res) {
showSuccess(t('tables', 'The column "{column}" was updated.', { column: this.editColumn.title }))
Expand Down
10 changes: 10 additions & 0 deletions src/shared/components/ncTable/mixins/columnClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ export class AbstractColumn {
return valueObject.value
}

/**
* parse an input value
*
* @param value
* @return {*}
*/
parseValue(value) {
return value
}

}

export class AbstractNumberColumn extends AbstractColumn {
Expand Down
4 changes: 4 additions & 0 deletions src/shared/components/ncTable/mixins/columnsTypes/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ export default class NumberColumn extends AbstractNumberColumn {
return super.isFilterFound(filterMethod, cell)
}

parseValue(value) {
return value === null ? null : parseFloat(value)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ export default class NumberProgressColumn extends AbstractNumberColumn {
return super.isFilterFound(filterMethod, cell)
}

parseValue(value) {
return parseInt(value)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ export default class SelectionColumn extends AbstractSelectionColumn {
return super.isFilterFound(filterMethod, cell)
}

parseValue(value) {
return parseInt(value)
}

}
3 changes: 3 additions & 0 deletions src/shared/components/ncTable/partials/TableCellNumber.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default {
},
computed: {
getValue() {
if (this.value === null) {
return null
}
return this.value.toFixed(this.column?.numberDecimals)
},
},
Expand Down
8 changes: 1 addition & 7 deletions src/shared/components/ncTable/partials/TableRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,7 @@ export default {
value = column.default()
}
if ([ColumnTypes.NumberProgress, ColumnTypes.Selection].includes(column.type)) {
return parseInt(value)
}
if ([ColumnTypes.Number].includes(column.type)) {
return parseFloat(value)
}
return value
return column.parseValue(value)
},
truncate(text) {
if (text.length >= 400) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<div class="fix-col-4 title">
{{ t('tables', 'Default value') }}
</div>
<div class="fix-col-4">
<input v-model="mutableColumn.numberDefault" type="number">
<div class="fix-col-4" :class="{error: defaultValueErrorHint !== ''}">
<input v-model="mutableColumn.numberDefault" type="number" @input="event => defaultValue = event.target.value">
</div>
</div>
<div class="col-2 space-R" style="display: block">
Expand All @@ -18,6 +18,11 @@
<input v-model="mutableColumn.numberDecimals" type="number">
</div>
</div>
<div v-if="defaultValueErrorHint !== ''" class="col-4">
<NcNoteCard type="warning">
<p>{{ defaultValueErrorHint }}</p>
</NcNoteCard>
</div>
</div>

<div class="row space-T">
Expand Down Expand Up @@ -67,9 +72,15 @@
<script>
import { translate as t } from '@nextcloud/l10n'
import NcNoteCard from '@nextcloud/vue/dist/Components/NcNoteCard.js'
export default {
name: 'NumberForm',
components: {
NcNoteCard,
},
props: {
column: {
type: Object,
Expand All @@ -80,11 +91,32 @@ export default {
default: true,
},
},
data() {
return {
mutableColumn: this.column,
defaultValue: null,
}
},
computed: {
defaultValueErrorHint() {
if (this.defaultValue === null || this.defaultValue === '') {
return ''
}
if (this.mutableColumn.numberMin !== null && this.mutableColumn.numberMin !== '' && this.defaultValue < this.mutableColumn.numberMin) {
return t('tables', 'The default value is lower than the minimum allowed value.')
}
if (this.mutableColumn.numberMax !== null && this.mutableColumn.numberMax !== '' && this.defaultValue > this.mutableColumn.numberMax) {
return t('tables', 'The default value is greater than the maximum allowed value.')
}
return ''
},
},
watch: {
column() {
this.mutableColumn = this.column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ export default {
methods: {
parseValue(inputValue) {
const parsedValue = parseFloat(inputValue.replace(',', '.'))
if (inputValue === null || inputValue === '') {
return null
}
let parsedValue
if (typeof inputValue === 'string' || inputValue instanceof String) {
parsedValue = parseFloat(inputValue.replace(',', '.'))
} else {
parsedValue = inputValue
}
const roundedValue = parsedValue.toFixed(this.column?.numberDecimals)
let value = parseFloat(roundedValue)
if (this.column?.numberMin && value < this.column?.numberMin) {
Expand Down

0 comments on commit 0c746a4

Please sign in to comment.