Skip to content

Commit

Permalink
TheCoder4eu#1101 fix an error in line 612 to get start index
Browse files Browse the repository at this point in the history
  • Loading branch information
geopossachs committed Feb 8, 2020
1 parent c8ca2c3 commit 061dc0f
Showing 1 changed file with 68 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
*
* This file is part of BootsFaces.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.bootsfaces.component.dataTable;

import java.io.IOException;
Expand All @@ -39,7 +38,9 @@
import net.bootsfaces.render.Tooltip;
import net.bootsfaces.utils.BsfUtils;

/** This class generates the HTML code of <b:dataTable />. */
/**
* This class generates the HTML code of <b:dataTable />.
*/
@FacesRenderer(componentFamily = "net.bootsfaces.component", rendererType = "net.bootsfaces.component.dataTable.DataTable")
public class DataTableRenderer extends CoreRenderer {

Expand All @@ -59,18 +60,15 @@ public void decode(FacesContext context, UIComponent component) {
private static final Pattern NUMERIC_PATTERN = Pattern.compile("[0-9]+");

/**
* This methods generates the HTML code of the current b:dataTable.
* <code>encodeBegin</code> generates the start of the component. After the, the
* JSF framework calls <code>encodeChildren()</code> to generate the HTML code
* between the beginning and the end of the component. For instance, in the case
* of a panel component the content of the panel is generated by
* <code>encodeChildren()</code>. After that, <code>encodeEnd()</code> is called
* to generate the rest of the HTML code.
* This methods generates the HTML code of the current b:dataTable. <code>encodeBegin</code> generates the start
* of the component. After the, the JSF framework calls <code>encodeChildren()</code> to generate the HTML code
* between the beginning and the end of the component. For instance, in the case of a panel component the
* content of the panel is generated by <code>encodeChildren()</code>. After that, <code>encodeEnd()</code> is
* called to generate the rest of the HTML code.
*
* @param context the FacesContext.
* @param context the FacesContext.
* @param component the current b:dataTable.
* @throws IOException thrown if something goes wrong when writing the HTML
* code.
* @throws IOException thrown if something goes wrong when writing the HTML code.
*/
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
Expand Down Expand Up @@ -111,20 +109,22 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
rw.startElement("table", dataTable);
if (!idHasBeenRendered) {
rw.writeAttribute("id", clientId, "id");
} else
} else {
rw.writeAttribute("id", clientId + "Inner", "id"); // Table selection needs a valid AJAX id on the table tag

}
String styleClass = "table ";
if (dataTable.isBorder()) {
styleClass += "table-bordered ";
}
if (dataTable.isStriped()) {
styleClass += "table-striped ";
}
if (dataTable.isRowHighlight())
if (dataTable.isRowHighlight()) {
styleClass += "table-hover ";
if (dataTable.getStyleClass() != null)
}
if (dataTable.getStyleClass() != null) {
styleClass += dataTable.getStyleClass();
}
styleClass += " " + clientId.replace(":", "") + "Table";
rw.writeAttribute("class", styleClass, "class");
Tooltip.generateTooltip(context, dataTable, rw);
Expand All @@ -137,7 +137,7 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
generateBody(context, dataTable, rw);
generateFooter(context, dataTable, rw);
new AJAXRenderer().generateBootsFacesAJAXAndJavaScriptForJQuery(context, component, rw,
"." + clientId.replace(":", "") + "Table", null);
"." + clientId.replace(":", "") + "Table", null);
}

private void generateFooter(FacesContext context, DataTable dataTable, ResponseWriter rw) throws IOException {
Expand Down Expand Up @@ -186,7 +186,7 @@ private void generateFooter(FacesContext context, DataTable dataTable, ResponseW
}

private void generateMultiColumnSearchRow(FacesContext context, DataTable dataTable, ResponseWriter rw)
throws IOException {
throws IOException {
rw.startElement("tr", dataTable);
List<UIComponent> columns = dataTable.getChildren();
for (UIComponent column : columns) {
Expand All @@ -200,7 +200,7 @@ private void generateMultiColumnSearchRow(FacesContext context, DataTable dataTa
}
Object searchable = column.getAttributes().get("searchable");
if (searchable == null || ((searchable instanceof Boolean) && ((Boolean) searchable).equals(Boolean.TRUE))
|| ((searchable instanceof String) && ((String) searchable).equalsIgnoreCase("true"))) {
|| ((searchable instanceof String) && ((String) searchable).equalsIgnoreCase("true"))) {
Object footerStyleClass = column.getAttributes().get("footerStyleClass");
if (footerStyleClass != null) {
rw.writeAttribute("class", "bf-multisearch " + footerStyleClass, null);
Expand Down Expand Up @@ -276,17 +276,19 @@ private void generateBody(FacesContext context, DataTable dataTable, ResponseWri
}

Object dataOrder = column.getAttributes().get("dataOrder");
if (dataOrder != null)
if (dataOrder != null) {
rw.writeAttribute("data-order", dataOrder, null);
}
Object dataSearch = column.getAttributes().get("dataSearch");
if (dataSearch != null)
if (dataSearch != null) {
rw.writeAttribute("data-search", dataSearch, null);
}

Object value = column.getAttributes().get("value");
if (value != null) {
rw.writeText(value, null);
} else if (column.getAttributes().get("selectionMode") != null) {
rw.writeText((1+row), null); // unique ID required by the checkbox plugin
rw.writeText((1 + row), null); // unique ID required by the checkbox plugin
}
renderChildrenOfColumn(column, context);
rw.endElement("td");
Expand Down Expand Up @@ -509,18 +511,15 @@ private void generateHeader(FacesContext context, DataTable dataTable, ResponseW
}

/**
* This methods generates the HTML code of the current b:dataTable.
* <code>encodeBegin</code> generates the start of the component. After the, the
* JSF framework calls <code>encodeChildren()</code> to generate the HTML code
* between the beginning and the end of the component. For instance, in the case
* of a panel component the content of the panel is generated by
* <code>encodeChildren()</code>. After that, <code>encodeEnd()</code> is called
* to generate the rest of the HTML code.
* This methods generates the HTML code of the current b:dataTable. <code>encodeBegin</code> generates the start
* of the component. After the, the JSF framework calls <code>encodeChildren()</code> to generate the HTML code
* between the beginning and the end of the component. For instance, in the case of a panel component the
* content of the panel is generated by <code>encodeChildren()</code>. After that, <code>encodeEnd()</code> is
* called to generate the rest of the HTML code.
*
* @param context the FacesContext.
* @param context the FacesContext.
* @param component the current b:dataTable.
* @throws IOException thrown if something goes wrong when writing the HTML
* code.
* @throws IOException thrown if something goes wrong when writing the HTML code.
*/
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
Expand All @@ -537,7 +536,7 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
for (Map.Entry<Integer, String> entry : columnSortOrder.entrySet()) {
String separator = (i > 0) ? "," : "";
sb.append(separator).append("[").append(entry.getKey()).append(",").append("'").append(entry.getValue())
.append("'").append("]");
.append("'").append("]");
i++;
}
orderString = sb.toString();
Expand Down Expand Up @@ -610,8 +609,8 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
if (BsfUtils.isStringValued(lang)) {
boolean languageAdded = false;
if (customOptions != null && customOptions.contains("language")) {
int start = customOptions.indexOf("language" + "language".length());
start = customOptions.indexOf("{", start)+1;
int start = customOptions.indexOf("language") + "language".length();
start = customOptions.indexOf("{", start) + 1;
if (start > 0) {
customOptions = customOptions.substring(0, start) + "url: '" + lang + "'," + customOptions.substring(start);
languageAdded = true;
Expand Down Expand Up @@ -672,10 +671,10 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
options = addOptions(rowGroup, 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({" + options + "});", null);
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({" + options + "});", null);

if (dataTable.isMultiColumnSearch()) {
// # Footer stuff:
Expand All @@ -688,13 +687,13 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
filter += "</div>";

rw.writeText(widgetVar + ".find('.bf-multisearch').each(function(){" + "var title=$(this).text();"
+ "$(this).html('" + filter + "');" + "});", null);
+ "$(this).html('" + filter + "');" + "});", null);
// # Add event listeners for each multisearch input
rw.writeText("var inputs=$(" + widgetVar + ".find('.bf-multisearch input'));", null);
rw.writeText("table.columns().every( function(col) {" + "var that=this;if(col<inputs.length){"
+ "inputs[col].value=table.columns(col).search()[0];"
+ "$(inputs[col]).on('keyup change', function(){if(that.search()!==this.value){"
+ "that.search(this.value).draw('page');}});}", null);
+ "inputs[col].value=table.columns(col).search()[0];"
+ "$(inputs[col]).on('keyup change', function(){if(that.search()!==this.value){"
+ "that.search(this.value).draw('page');}});}", null);
rw.writeText("});", null);
int col = 0;
for (UIComponent column : dataTable.getChildren()) {
Expand Down Expand Up @@ -725,7 +724,6 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
rw.endElement("script");
}


private String getButtons(DataTable dataTable) {
StringBuilder b = new StringBuilder();
if (dataTable.isColumnVisibility()) {
Expand Down Expand Up @@ -754,8 +752,9 @@ private String getButtons(DataTable dataTable) {

private String addOptions(String newOption, String options) {
if (newOption != null && newOption.length() > 0) {
if (options.length() > 0)
if (options.length() > 0) {
options += ",";
}
options += newOption;
}
return options;
Expand Down Expand Up @@ -796,12 +795,11 @@ private String generateColumnDefs(DataTable dataTable) {
result += "{" + col + "},";
}
}
result = result.substring(0, result.length() - 1); // remove the
// trailing comma
result = result.substring(0, result.length() - 1); // remove the trailing comma
if (null == dataTable.getSelectionMode2()) {
result += "]";
} else {
result += "],'select':"+ dataTable.getSelectionMode2();
result += "],'select':" + dataTable.getSelectionMode2();
}
return result;
}
Expand All @@ -819,13 +817,12 @@ private String generateColumnInfos(List<String> columnInfo) {
if (col.contains("dom-text")) {
if (!col.contains("type")) {
throw new FacesException(
"You have to specify the data type of the column if you want to sort it using order-by.");
"You have to specify the data type of the column if you want to sort it using order-by.");
}
}
}
}
result = result.substring(0, result.length() - 1); // remove the
// trailing comma
result = result.substring(0, result.length() - 1); // remove the trailing comma
result += "]";
return result;
}
Expand All @@ -845,16 +842,14 @@ private String getPageLengthMenu(DataTable dataTable) {
}

/**
* Determine if the user specify a lang Otherwise return null to avoid language
* settings.
* Determine if the user specify a lang Otherwise return null to avoid language settings.
*
* @param fc
* @param dataTable
* @return
*/
private String determineLanguage(FacesContext fc, DataTable dataTable) {
final List<String> availableLanguages = Arrays.asList("de", "en", "es", "fr", "hu", "it", "nl", "pl", "pt",
"ru");
final List<String> availableLanguages = Arrays.asList("de", "en", "es", "fr", "hu", "it", "nl", "pl", "pt", "ru");
if (BsfUtils.isStringValued(dataTable.getCustomLangUrl())) {
return dataTable.getCustomLangUrl();
} else if (BsfUtils.isStringValued(dataTable.getLang())) {
Expand All @@ -872,10 +867,9 @@ private String determineLanguage(FacesContext fc, DataTable dataTable) {
}

/**
* Determine the locale to set-up to dataTable component. The locale is
* determined in this order: - if customLangUrl is specified, it is the value
* set up - otherwise, the system check if locale is explicit specified -
* otherwise it takes from the ViewRoot
* Determine the locale to set-up to dataTable component. The locale is determined in this order: - if
* customLangUrl is specified, it is the value set up - otherwise, the system check if locale is explicit
* specified - otherwise it takes from the ViewRoot
*
* @param fc
* @param dataTable
Expand All @@ -884,7 +878,7 @@ private String determineLanguage(FacesContext fc, DataTable dataTable) {
private String determineLanguageUrl(FacesContext fc, String lang) {
// Build resource url
return fc.getApplication().getResourceHandler()
.createResource("jq/ui/i18n/dt/datatable-" + lang + ".json", C.BSF_LIBRARY).getRequestPath();
.createResource("jq/ui/i18n/dt/datatable-" + lang + ".json", C.BSF_LIBRARY).getRequestPath();
}

@Override
Expand All @@ -907,7 +901,7 @@ protected void initColumnInfos(DataTable dataTable) {
dataTable.setColumnInfo(infos);
}
}

protected void initColumnDefinitions(DataTable dataTable) {
if (dataTable.getColumnDefinition() == null) {
List<String> infos = new ArrayList<String>();
Expand Down Expand Up @@ -956,8 +950,9 @@ protected void updateColumnInfo(DataTable dataTable, int index, String value) {

public static boolean isNumeric(String str) {
for (char c : str.toCharArray()) {
if (!Character.isDigit(c))
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
Expand Down

0 comments on commit 061dc0f

Please sign in to comment.