Skip to content

Commit

Permalink
#436 fixed a bug in the generated JavaScript code
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Jun 25, 2016
1 parent 9720cc5 commit 60f8c1f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected enum PropertyKeys {
colSm,
colXs,
customLangUrl,
customOptions,
disabled,
display,
fixedHeader,
Expand Down Expand Up @@ -215,6 +216,22 @@ public void setCustomLangUrl(String _customLangUrl) {
getStateHelper().put(PropertyKeys.customLangUrl, _customLangUrl);
}

/**
* Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk. <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
*/
public String getCustomOptions() {
return (String) getStateHelper().eval(PropertyKeys.customOptions);
}

/**
* Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk. <P>
* Usually this method is called internally by the JSF engine.
*/
public void setCustomOptions(String _customOptions) {
getStateHelper().put(PropertyKeys.customOptions, _customOptions);
}

/**
* Boolean value to specify if the button is disabled. <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ private void generateHeader(FacesContext context, DataTable dataTable, ResponseW
List<String> infos = dataTable.getColumnInfo();
String s = infos.get(index);
if (s == null) {
infos.set(index, "'orderDataType': '" + orderBy + "', type: 'string'");
infos.set(index, "'orderDataType': '" + orderBy+"'");
} else {
infos.set(index, s + ",'orderDataType': '" + orderBy + "', type: 'string'");
infos.set(index, s + ",'orderDataType': '" + orderBy+"'");
}

}
Expand Down Expand Up @@ -355,6 +355,24 @@ private void generateHeader(FacesContext context, DataTable dataTable, ResponseW
infos.set(index, s + ",'orderable': false");
}
}
if (column.getAttributes().get("customOptions") != null) {
String customOptions = column.getAttributes().get("customOptions").toString();
if (customOptions !=null && customOptions.length()>0) {
if (dataTable.getColumnInfo() == null) {
List<String> infos = new ArrayList<String>(dataTable.getChildren().size());
for (int k = 0; k < dataTable.getChildren().size(); k++) {
infos.add(null);
}
dataTable.setColumnInfo(infos);
}
List<String> infos = dataTable.getColumnInfo();
String s = infos.get(index);
if (s == null) {
infos.set(index, customOptions);
} else
infos.set(index, s + "," +customOptions);
}
}
rw.endElement("th");
index++;
}
Expand Down Expand Up @@ -426,23 +444,26 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
// # Start enclosure
rw.writeText("$(document).ready(function() {", null);
// # Enclosure-scoped variable initialization
String options = "";
options = addOptions(" fixedHeader: " + dataTable.isFixedHeader(), options);
options = addOptions( " responsive: " + dataTable.isResponsive(), options);
options = addOptions( " paging: " + dataTable.isPaginated(), options);
options = addOptions( " pageLength: " + pageLength, options);
options = addOptions( " lengthMenu: " + getPageLengthMenu(dataTable), options);
options = addOptions( " searching: " + dataTable.isSearching() , options);
options = addOptions( " order: " + orderString, options);
options = addOptions( " stateSave: " + dataTable.isSaveState(), options);
options = addOptions( " select: true", options);
options = addOptions( generateScrollOptions(dataTable), options);
options = addOptions( (BsfUtils.isStringValued(lang) ? " language: { url: '" + lang + "' } " : null), options);
options = addOptions( generateColumnInfos(dataTable.getColumnInfo()), options);
options = addOptions( dataTable.getCustomOptions(), options);
rw.writeText(widgetVar + " = $('." + clientId + "Table" + "');" +
// # Get instance of wrapper, and replace it with the unwrapped table.
"var wrapper = $('#" + clientIdRaw.replace(":", "\\\\:") + "_wrapper');" + "wrapper.replaceWith("
+ widgetVar + ");"
+ "var table = " + widgetVar + ".DataTable({"
+ " fixedHeader: " + dataTable.isFixedHeader() + ","
+ " responsive: " + dataTable.isResponsive() + ", "
+ " paging: " + dataTable.isPaginated() + ", "
+ " pageLength: " + pageLength + ", "
+ " lengthMenu: " + getPageLengthMenu(dataTable) + ", "
+ " searching: " + dataTable.isSearching() + ", "
+ " order: " + orderString + ", "
+ " stateSave: " + dataTable.isSaveState() + ", "
+ " select: true,"
+ generateScrollOptions(dataTable)
+ (BsfUtils.isStringValued(lang) ? " language: { url: '" + lang + "' } " : "")
+ generateColumnInfos(dataTable.getColumnInfo()) + "});", null);
+ options + "});", null);

if (dataTable.isMultiColumnSearch()) {
// # Footer stuff:
Expand All @@ -452,10 +473,9 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
+ "$(this).html('<input class=\"input-sm\" type=\"text\" placeholder=\"Search ' + title + '\" />');"
+ "});\n", null);
// # Add event listeners for each multisearch input
rw.writeText("table.columns().every( function (col) {" + "var that = this;\ndebugger;\n" + "var inputs = $("
rw.writeText("table.columns().every( function (col) {" + "var that = this;\n" + "var inputs = $("
+ widgetVar + ".find('.bf-multisearch input'));\n"
+ "$(inputs[col]).on( 'keyup change', function () {" + " if ( that.search() !== this.value ) {"
+ "console.log(this.value);" + "console.log(that);"
+ " that.search( this.value ).draw('page');" + " }" + "} );" +

"} );", null);
Expand All @@ -465,6 +485,15 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
rw.endElement("script");
}

private String addOptions(String newOption, String options) {
if (newOption!=null && newOption.length()>0) {
if (options.length()>0)
options += ",";
options += newOption;
}
return options;
}

private String generateScrollOptions(DataTable dataTable) {
String scrollY = dataTable.getScrollSize();
boolean scrollX = dataTable.isScrollX();
Expand All @@ -486,7 +515,7 @@ private String generateScrollOptions(DataTable dataTable) {
result += "scrollX: true,";
}

return result + "scrollCollapse: " + dataTable.isScrollCollapse() + ",";
return result + "scrollCollapse: " + dataTable.isScrollCollapse();
}

private String generateColumnInfos(List<String> columnInfo) {
Expand All @@ -509,7 +538,7 @@ private String generateColumnInfos(List<String> columnInfo) {
}
result = result.substring(0, result.length() - 1); // remove the
// trailing comma
result += "],";
result += "]";
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public abstract class DataTableColumnCore extends UIOutput {
protected enum PropertyKeys {
contentStyle,
contentStyleClass,
customOptions,
dataType,
footerStyle,
footerStyleClass,
Expand Down Expand Up @@ -85,6 +86,22 @@ public void setContentStyleClass(String _contentStyleClass) {
getStateHelper().put(PropertyKeys.contentStyleClass, _contentStyleClass);
}

/**
* Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk. <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
*/
public String getCustomOptions() {
return (String) getStateHelper().eval(PropertyKeys.customOptions);
}

/**
* Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk. <P>
* Usually this method is called internally by the JSF engine.
*/
public void setCustomOptions(String _customOptions) {
getStateHelper().put(PropertyKeys.customOptions, _customOptions);
}

/**
* Specifies order-by more precisely. Is also used by the filtering methods. Legal values are 'string', 'date', 'numeric'. <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
Expand Down
24 changes: 24 additions & 0 deletions src/main/meta/META-INF/bootsfaces-b.taglib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4878,6 +4878,18 @@
<required>false</required>
<type>String</type>
</attribute>
<attribute>
<description><![CDATA[Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk.]]></description>
<name>custom-options</name>
<required>false</required>
<type>String</type>
</attribute>
<attribute>
<description><![CDATA[Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk.]]></description>
<name>customOptions</name>
<required>false</required>
<type>String</type>
</attribute>
<attribute>
<description><![CDATA[Boolean value to specify if the button is disabled.]]></description>
<name>disabled</name>
Expand Down Expand Up @@ -5390,6 +5402,18 @@
<required>false</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<description><![CDATA[Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk.]]></description>
<name>custom-options</name>
<required>false</required>
<type>String</type>
</attribute>
<attribute>
<description><![CDATA[Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk.]]></description>
<name>customOptions</name>
<required>false</required>
<type>String</type>
</attribute>
<attribute>
<description><![CDATA[Specifies order-by more precisely. Is also used by the filtering methods. Legal values are 'string', 'date', 'numeric'.]]></description>
<name>data-type</name>
Expand Down
2 changes: 2 additions & 0 deletions xtext/BootsFaces.jsfdsl
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ widget dataTable
ajax Boolean "Activates AJAX. The default value is false (no AJAX)."
border Boolean default "true" "If set, this will surround the table by a border. Defaults to true."
custom-lang-url String "Defines a custom lang file url for languages BootsFaces doesn't support out-of-the-box."
custom-options String "Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk."
disabled Boolean "Boolean value to specify if the button is disabled."
fixed-header Boolean "Activates the fixed header plugin of the dataTable."
id inherited "Unique identifier of the component in a namingContainer."
Expand Down Expand Up @@ -508,6 +509,7 @@ widget dataTable
}

widget dataTableColumn {
custom-options String "Allows you to pass an arbitrary option to the datatable widget. Separate the options by a comma if you pass more than one. Note that this may cause incompatibilities when the next version of BootsFaces is released. Use at own risk."
label "Label in the header of the colum."
order "Is the table to be sorted by this column? Legal values are 'asc' and 'desc'."
order-by "Allows you to sort input field. Legal values are dom-text, dom-text-numeric, dom-select and dom-checkbox."
Expand Down

0 comments on commit 60f8c1f

Please sign in to comment.