Skip to content

Commit

Permalink
w2form.toggleGroup
Browse files Browse the repository at this point in the history
improved toggleGroup to honor the value of show.
execute el.next() only once

added docs and demos for toggleGroup and field.html.groupCollapsible
  • Loading branch information
mpf82 committed May 28, 2021
1 parent 721fdf7 commit 9fe2397
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 11 deletions.
140 changes: 140 additions & 0 deletions demos/examples/form/20.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<div class="content">
<div id="example_title">
<h1>Collapsible Groups</h1>
Groups can be collapsed, expanded, and toggled.
</div>
<div id="example_view"></div>
<div id="example_code"></div>
</div>

<!--CODE-->
<div id="form" style="width: 750px; height: 500px"></div>
<br>
<button class="w2ui-btn" onclick="w2ui.form.toggleGroup('General')">Toggle General</button>
<button class="w2ui-btn" onclick="w2ui.form.toggleGroup('Address Group 2')">Toggle Address Group 2</button>
<button class="w2ui-btn" onclick="w2ui.form.toggleGroup('Address Group 3', false)">Collapse Address Group 3</button>
<button class="w2ui-btn" onclick="w2ui.form.toggleGroup('Address Group 3', true)">Expand Address Group 3</button>
<br>

<!--CODE-->
<script>
$(function () {
$('#form').w2form({
name : 'form',
url : 'server/post',
fields : [
{ field: 'first_name', type: 'text',
html: {
group: 'General',
groupCollapsible: true,
page: 0,
column: 0,
label: 'First Name',
span: 4
}
},
{ field: 'last_name', type: 'text',
html: {
page: 0,
column: 0,
label: 'Last Name',
span: 4
}
},
{ field: 'middle_name', type: 'text',
html: {
page: 0,
column: 0,
label: 'Middle Name',
span: 4
}
},
{ field: 'Other', type: 'textarea',
html: {
page: 0,
column: 1,
group: 'Additional Info',
groupCollapsible: true,
groupStyle: 'min-height: 115px',
label: 'Other',
span: -1,
attr: 'style="width: 100%; resize: vertical"'
}
},
{ field: 'address1', type: 'text',
html: {
page: 0,
column: 0,
group: 'Address Group 1',
groupCollapsible: false,
label: 'Address',
}
},
{ field: 'address2', type: 'text',
html: {
page: 0,
label: 'Line 2',
column: 0
}
},
{ field: 'city', type: 'text',
html: {
page: 0,
group: 'Address Group 2',
groupCollapsible: true,
groupStyle: 'height: 170px',
label: 'City',
column: 1,
}
},
{ field: 'state', type: 'text',
html: {
page: 0,
label: 'State',
column: 1
}
},
{ field: 'zip', type: 'int',
html: {
page: 0,
label: 'Zip',
column: 1
}
},
{ field: 'country', type: 'text',
html: {
page: 0,
group: 'Address Group 3',
groupCollapsible: true,
label: 'Country',
column: 0,
}
}
],
actions: {
Reset: function () {
this.clear();
},
Save: function () {
if (w2ui.form.validate().length == 0) {
w2popup.open({
title: 'Form Data',
with: 600,
height: 550,
body: `<pre>${JSON.stringify(this.getCleanRecord(), null, 4)}</pre>`,
buttons: '<button class="w2ui-btn" onclick="w2popup.close()">Ok</button>'
})
}
},
custom: {
text: '<span style="font-size: 16px">←</span> click to see data',
class: 'custom-class',
style: 'background-image: none; background-color: transparent; border: 0px; margin: 0 0 0 -10px;',
onClick() {
w2alert('Not me!! The other button')
}
}
}
});
});
</script>
1 change: 1 addition & 0 deletions demos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ $(function () {
{ id: 'form/16', text: 'Enable/Disable Fields', icon: 'fa fa-pencil-square-o' },
{ id: 'form/17', text: 'Show/Hide Fields', icon: 'fa fa-pencil-square-o' },
{ id: 'form/18', text: 'Context Messages', icon: 'fa fa-pencil-square-o' },
{ id: 'form/20', text: 'Collapsible Groups', icon: 'fa fa-pencil-square-o' },
]
},
// { id: 'forms-2.0', text: 'Features 2.0+', img: 'icon-folder', group: true, expanded: true, hidden: true,
Expand Down
75 changes: 75 additions & 0 deletions docs/details/w2form.toggleGroup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Toggles the collapsed state of a form's group.

<div class="definition">
toggleGroup(groupName, [show])
</div>

<div class="arguments">
<table>
<tr>
<td>groupName</td>
<td><b>string</b>, name of the group</td>
</tr>
<tr>
<td>show</td>
<td><b>boolean</b>, if true the group will be expanded, if false the group will be collapsed <i>(optional)</i></td>
</tr>
</table>
</div>

Returns <i>undefined</i>.

<h4>Description</h4>

This method will toggle the collapsed/expanded state of a group if <span class="argument">show</span> is not set.
<div style="height: 10px"></div>

If <span class="argument">show</span> is, the group will be collapsed/expanded based on the value of <span class="argument">show</span>.
<div style="height: 10px"></div>


If you have form defined in the following way:
<textarea class="javascript">
$('#form').w2form({
name : 'form',
url : 'server/post',
fields: [
{ field: 'first_name', type: 'text',
html: {
group: 'General',
groupCollapsible: true,
page: 0,
column: 0,
label: 'First Name',
span: 4
}
},
{ field: 'last_name', type: 'text',
html: {
page: 0,
column: 0,
label: 'Last Name',
span: 4
}
},
{ field: 'middle_name', type: 'text',
html: {
page: 0,
column: 0,
label: 'Middle Name',
span: 4
}
}
]
});
</textarea>

You can do:
<textarea class="javascript">
// toggle collapsed/expanded state of the group based on the current state
w2ui.form.toggleGroup('General');
// collapse the group
w2ui.form.toggleGroup('General', false);
// expand the group
w2ui.form.toggleGroup('General', true);
</textarea>
26 changes: 15 additions & 11 deletions src/w2form.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* - added form.pageStyle
* - added html.span -1 - then label is displayed on top
* - added field.options.minLength, min/max for numbers can be done with int/float - min/max
* - field.html.groupCollapsible, form.toggleGroup
* - added showErrors
* - added field.type = 'check'
* - new field type 'map', 'array' - same thing but map has unique keys also html: { key: { text: '111', attr: '222' }, value: {...}}
Expand Down Expand Up @@ -1129,18 +1128,23 @@ class w2form extends w2event {

toggleGroup(groupName, show) {
let el = $(this.box).find('.w2ui-group-title[data-group="' + w2utils.base64encode(groupName) + '"]')
if (el.next().css('display') == 'none' && show !== true) {
el.next().slideDown(300)
el.next().next().remove()
if(!el || !el.length) return
if (typeof show === 'undefined') {
show = ( el_next.css('display') == 'none' )
}
let el_next = el.next()
if (show) {
el_next.slideDown(300)
el_next.next().remove()
el.find('span').addClass('w2ui-icon-collapse').removeClass('w2ui-icon-expand')
} else {
el.next().slideUp(300)
let css = 'width: ' + el.next().css('width') + ';'
+ 'padding-left: ' + el.next().css('padding-left') + ';'
+ 'padding-right: ' + el.next().css('padding-right') + ';'
+ 'margin-left: ' + el.next().css('margin-left') + ';'
+ 'margin-right: ' + el.next().css('margin-right') + ';'
setTimeout(() => { el.next().after('<div style="height: 5px;'+ css +'"></div>') }, 100)
el_next.slideUp(300)
let css = 'width: ' + el_next.css('width') + ';'
+ 'padding-left: ' + el_next.css('padding-left') + ';'
+ 'padding-right: ' + el_next.css('padding-right') + ';'
+ 'margin-left: ' + el_next.css('margin-left') + ';'
+ 'margin-right: ' + el_next.css('margin-right') + ';'
setTimeout(() => { el_next.after('<div style="height: 5px;'+ css +'"></div>') }, 100)
el.find('span').addClass('w2ui-icon-expand').removeClass('w2ui-icon-collapse')
}
}
Expand Down

0 comments on commit 9fe2397

Please sign in to comment.