Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved logic for Tables min/max rows Fixed #2864 #2871

Merged
merged 4 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Fixed warnings about missing SVG files that were logged by Control Panel requests.
- Fixed a bug where the `|date` filter would ignore date formatting characters that don’t have ICU counterparts. ([#2867](https://github.com/craftcms/cms/issues/2867))
- Fixed a bug where the global `currentUser` Twig variable could be set to `null` and global sets and could be missing some custom field values when a user was logged-in, if a plugin was loading Twig during or immediately after plugin instantiation. ([#2866](https://github.com/craftcms/cms/issues/2866))
- Fixed a bug where Table fields could add the wrong number of default rows if the Min Rows setting was set, and the Default Values setting had something other than one row. ([#2864](https://github.com/craftcms/cms/issues/2864))

## 3.0.6 - 2018-05-08

Expand Down
31 changes: 13 additions & 18 deletions src/web/assets/cp/dist/js/Craft.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! - 2018-05-05 */
/*! - 2018-05-10 */
(function($){

/** global: Craft */
Expand Down Expand Up @@ -12709,7 +12709,7 @@ Craft.EditableTable = Garnish.Base.extend(
}

if (this.settings.minRows && this.rowCount < this.settings.minRows) {
for (var i = 1; i < this.settings.minRows; i++) {
for (var i = this.rowCount; i < this.settings.minRows; i++) {
this.addRow()
}
}
Expand Down Expand Up @@ -12747,10 +12747,7 @@ Craft.EditableTable = Garnish.Base.extend(
}
},
updateAddRowButton: function() {
if (
(this.settings.maxRows && this.rowCount >= this.settings.maxRows) ||
(this.settings.minRows && this.rowCount < this.settings.minRows)
) {
if (this.settings.maxRows && this.rowCount >= this.settings.maxRows) {
this.$addRowBtn.css('opacity', '0.2');
this.$addRowBtn.css('pointer-events', 'none');
} else {
Expand All @@ -12762,31 +12759,29 @@ Craft.EditableTable = Garnish.Base.extend(
return (this.rowCount > this.settings.minRows);
},
deleteRow: function(row) {
if (this.settings.maxRows && this.settings.minRows) {
if (!this.canDeleteRow()) {
return;
}
if (!this.canDeleteRow()) {
return;
}

this.sorter.removeItems(row.$tr);
row.$tr.remove();

if (this.settings.minRows && this.settings.maxRows) {
this.rowCount--;
}
this.rowCount--;

this.updateAddRowButton();
// onDeleteRow callback
this.settings.onDeleteRow(row.$tr);
},
canAddRow: function() {
return (this.rowCount < this.settings.maxRows);
if (this.settings.maxRows) {
return (this.rowCount < this.settings.maxRows);
}

return true;
},
addRow: function() {
if (this.settings.minRows && this.settings.maxRows) {
if (!this.canAddRow()) {
return;
}
if (!this.canAddRow()) {
return;
}

var rowId = this.settings.rowIdPrefix + (this.biggestId + 1),
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/js/Craft.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/js/Craft.min.js.map

Large diffs are not rendered by default.

29 changes: 12 additions & 17 deletions src/web/assets/cp/src/js/EditableTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Craft.EditableTable = Garnish.Base.extend(
}

if (this.settings.minRows && this.rowCount < this.settings.minRows) {
for (var i = 1; i < this.settings.minRows; i++) {
for (var i = this.rowCount; i < this.settings.minRows; i++) {
this.addRow()
}
}
Expand Down Expand Up @@ -93,10 +93,7 @@ Craft.EditableTable = Garnish.Base.extend(
}
},
updateAddRowButton: function() {
if (
(this.settings.maxRows && this.rowCount >= this.settings.maxRows) ||
(this.settings.minRows && this.rowCount < this.settings.minRows)
) {
if (this.settings.maxRows && this.rowCount >= this.settings.maxRows) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s change this to

if (!this.canAddRow()) {

this.$addRowBtn.css('opacity', '0.2');
this.$addRowBtn.css('pointer-events', 'none');
} else {
Expand All @@ -108,31 +105,29 @@ Craft.EditableTable = Garnish.Base.extend(
return (this.rowCount > this.settings.minRows);
},
deleteRow: function(row) {
if (this.settings.maxRows && this.settings.minRows) {
if (!this.canDeleteRow()) {
return;
}
if (!this.canDeleteRow()) {
return;
}

this.sorter.removeItems(row.$tr);
row.$tr.remove();

if (this.settings.minRows && this.settings.maxRows) {
this.rowCount--;
}
this.rowCount--;

this.updateAddRowButton();
// onDeleteRow callback
this.settings.onDeleteRow(row.$tr);
},
canAddRow: function() {
return (this.rowCount < this.settings.maxRows);
if (this.settings.maxRows) {
return (this.rowCount < this.settings.maxRows);
}

return true;
},
addRow: function() {
if (this.settings.minRows && this.settings.maxRows) {
if (!this.canAddRow()) {
return;
}
if (!this.canAddRow()) {
return;
}

var rowId = this.settings.rowIdPrefix + (this.biggestId + 1),
Expand Down