org.beangle.struts2.view.component.Checkboxes Maven / Gradle / Ivy
/*
* Beangle, Agile Development Scaffold and Toolkit
*
* Copyright (c) 2005-2014, Beangle Software.
*
* Beangle is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Beangle is distributed in the hope that it will be useful.
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Beangle. If not, see .
*/
package org.beangle.struts2.view.component;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.beangle.commons.bean.PropertyUtils;
import org.beangle.commons.collection.CollectUtils;
import org.beangle.commons.lang.Strings;
import com.opensymphony.xwork2.util.ValueStack;
public class Checkboxes extends UIBean {
private String name;
private String label;
private Object items;
private Checkbox[] checkboxes;
private Object value;
private String comment;
private String required;
private Object min;
private Object max;
private String valueName = "name";
public Checkboxes(ValueStack stack) {
super(stack);
}
@Override
protected void evaluateParams() {
if (null == this.id) generateIdIfEmpty();
if (null != label) label = getText(label);
List> keys = convertItems();
List> values = convertValue();
checkboxes = new Checkbox[keys.size()];
int i = 0;
Form myform = findAncestor(Form.class);
Integer minValue = getValidateNum(min);
Integer maxValue = getValidateNum(max);
if (null != myform) {
if ("true".equals(required)) {
myform.addCheck(id, "assert($(\"input[name='" + name + "']:checked\").length != 0,'必须勾选一项')");
}
}
if (minValue != null && maxValue != null) {
if (minValue > maxValue) {
Integer temp = maxValue;
maxValue = minValue;
minValue = temp;
}
}
if (minValue != null && minValue > 0 && minValue <= checkboxes.length) {
myform.addCheck(id, "assert($(\"input[name='" + name + "']:checked\").length >= " + minValue + ",'至少勾选"
+ minValue + "项')");
}
if (maxValue != null && maxValue < checkboxes.length && maxValue > 0) {
myform.addCheck(id, "assert($(\"input[name='" + name + "']:checked\").length <= " + maxValue + ",'最多勾选"
+ maxValue + "项')");
}
for (Object key : keys) {
checkboxes[i] = new Checkbox(stack);
checkboxes[i].setTitle(String.valueOf(((Map, ?>) items).get(key)));
checkboxes[i].setValue(key);
checkboxes[i].setId(Strings.concat(this.id + "_" + String.valueOf(i)));
if (CollectUtils.isNotEmpty(values) && values.contains(key)) {
checkboxes[i].setChecked(true);
}
checkboxes[i].evaluateParams();
i++;
}
}
@SuppressWarnings("unchecked")
private List> convertItems() {
if (items instanceof Map) return CollectUtils.newArrayList(((Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy