我有一个表,有一列链接和另一列布尔值,当我点击一个链接时,另一列的布尔值应该改为相反的值,即,如果它的假为真,则为反之亦然.-->这是我的目标。
这是一个简化的描述,该表有更多的列,这里没有描述,我有很多类。
在主要课程中,我添加了表格,并制作了列,如下面的摘录
Main.class
private void onInit(){
table=new BootstrapFallbackDefaultDataTable<PaymentArea,String>("payment-table", makeColumns(), paymentDetailsDataProvide, 10);
table.setOutputMarkupId(true);
add(table)
}
private List<IColumn<PaymentArea,String>> makeColumns(){
List<IColumn<PaymentArea,String>> columns = new ArrayList<>();
//some columns are added as PropertyColumn
//the next column is the one that have the ajaxLink
columns.add(new AbstractColumn<PaymentArea,String>(new StringResourceModel("payment-actions",this,null)){
@Override
public void populateItem(Item<ICellPopulator<PaymentArea>> item, String componentId, IModel<PaymentArea> rowmodel)
{
currentGoal = service.getPollById(rowmodel.getObject().getPaymentGUID()).getVoted();
invoiceId = rowmodel.getObject().getInvoiceGUID();
paymentId = rowmodel.getObject().getPaymentGUID();
item.add(new ContestButtonPayment2(componentId, currentGoal, invoiceId, paymentId));
});
//next column is the column that should be updated
columns.add(new AbstractColumn<PaymentArea, String>(new StringResourceModel("payment-contest",this,null)
{
@Override
public void populateItem(Item<ICellPopulator<PaymentArea>> item, String componentId, IModel<PaymentArea> rowmodel)
{
currentGoal = service.getPollById(rowmodel.getObject().getPaymentGUID()).getVoted();
item.add(new ConstestedColumn2(componentId, currentGoal));
}
});
return columns;
}
}
下一个类是为表和列的每一行定义AjaxLink的类。
ContestButtonPayment2.java
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import nl.riskco.liberobc.client.business.services.IPollService;
import nl.riskco.liberobc.client.business.services.impl.PollServiceImpl;
import nl.riskco.liberobc.web.pages.details.invoices.main.LegendForButtons;
public class ContestButtonPayment2 extends Panel{
private static final long serialVersionUID = 1L;
private IPollService service;
private LegendForButtons text;
private Label label;
public ContestButtonPayment2(String componentId, List<String> currentGoal, String invoiceId, String paymentId){
super(componentId);
service = new PollServiceImpl();
initiateButton(currentGoal);
add(new AjaxLink("voteLink"){
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
if(!currentGoal.contains("X"))
{
service.votePayment(paymentId,invoiceId);
text.setLegend("Revoke");
}//close if
else if(currentGoal.contains("X"))
{
System.out.println("payment: " + paymentId + " invoiceId: " + invoiceId);
service.revokePaymentVote(paymentId);
text.setLegend("Contest");
}
else{
System.out.println("Something went wrong");
}
//Update link
target.add(this);
}//close onclick
}.add(label));//close ajaxLink
}
public void initiateButton(List<String> currentGoal){
text = new LegendForButtons();
if(!currentGoal.contains("X"))
{
text.setLegend("Contest");
}
else
text.setLegend("Revoke");
label = new Label("buttonLabel", new PropertyModel(text,"legend"));
}
}
需要更新的课程
ConstestedColumn2.java
import java.util.List;
import org.apache.wicket.markup.html.panel.Panel;
import nl.riskco.Y.web.pages.utils.VoteBooleanLabelPanel;
public class ContestedColumn2 extends Panel{
private boolean resultX, resultY, resultZ;
private VoteBooleanLabelPanel labelX;
private VoteBooleanLabelPanel labelY;
private VoteBooleanLabelPanel labelZ;
private static final long serialVersionUID = 1L;
public ContestedColumn2(String id, List<String> currentGoal){
super(id);
resultX = currentGoal.contains("X");
labelX = new VoteBooleanLabelPanel("labelX", "X", resultX);
labelX.setOutputMarkupId(true);
add(labelX);
resultY = currentGoal.contains("Y");
labelY = new VoteBooleanLabelPanel("labelY", "Y", resultY);
labelY.setOutputMarkupId(true);
add(labelY);
resultZ= currentGoal.contains("Z");
labelZ = new VoteBooleanLabelPanel("labelZ", "Z", resultZ);
labelZ.setOutputMarkupId(true);
add(labelZ);
}
public ContestedColumn2(String id){
super(id);
}
}
VoteBooleanLabelPanel.java
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
public class VoteBooleanLabelPanel extends Panel{
private static final long serialVersionUID = 1L;
private Label label;
private boolean value;
public VoteBooleanLabelPanel(final String id, String text, boolean value) {
super(id);
String voteLabel = getCssClass(value);
label = new Label("icon", text);
label.add(new AttributeModifier("class",voteLabel));
add(label);
}
public VoteBooleanLabelPanel(final String id, String text){
super(id);
String voteLabel = getCssClass(value);
this.value = false;
label = new Label("icon", text);
label.add(new AttributeModifier("class",voteLabel));
add(label);
}
public void updateLabel(boolean value, AjaxRequestTarget target){
String voteLabel = getCssClass(value);
label.add(new AttributeModifier("class",voteLabel));
label.setOutputMarkupId(true);
target.add(label);
}
private String getCssClass(boolean value){
if(value)
return "label label-danger";
else
return "label label-default";
}
}
为了更好地理解这最后一个java类是什么,下面是它的html
<wicket:panel>
<h4><span wicket:id="labelX"></span> <span wicket:id="labelY"></span> <span wicket:id="labelZ"></span></h4>
</wicket:panel>
要点: 我想点击row1 column2的链接来更新表格row1 column3中的labelX值。
labelX在table列(labelY和labelZ旁边)表示为灰色正方形内的X(如果其值为false),如果其值为true则表示红色。
有谁知道怎么做? 我认为我应该使用事件,但我不知道怎么做,因为我有这么多课程。
答案 0 :(得分:0)
最简单,最干净的方法是使用Wicket事件。
在onClick()
广播带有效载荷的事件,例如:
send(findParent(DataTable.class), Broadcast.BREADTH, new YourPayload(guid, target));
guid
是getPaymentGUID()
,target
是AjaxRequestTarget。
该事件将被发送到父数据表,并从那里将广播给它的所有孩子。
在ConstestedColumn2.java中,您需要覆盖onEvent(IEvent)
,如果guid
相同,则使用有效负载进行自我更新。
@Override public void onEvent(IEvent event) {
Object payload = event.getPayload();
if (payload instanceOf YourPayload) {
YourPayload yourPayload = (YourPayload) payload;
if (myGuid.equals(yourPayload.getGuid()) {
// some business logic
yourPayload.getTarget().add(this);
event.stop(); // no need to visit more children
}
}
}