wicket 如何在单击另一列的 ajax 链接时更新 table 中的列值
wicket how to update a column value in a table when clicking on an ajaxlink of another column
我有一个 table,它有一列 links 和另一列布尔值,当我单击 link 时,另一列的布尔值应该改变到相反的值,即,如果它是假的,反之亦然。--> 这是我的目标。
这是一个简化的描述,table还有更多的列在这里没有描述,我有大量的class。
在主要 class 中,我添加了 table,并制作了列,如下面的摘录
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;
}
}
下一个 class 是定义 AjaxLink 的 class,用于列和 table 的每一行。
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"));
}
}
需要更新的class
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 class 是什么,下面是它的 html
<wicket:panel>
<h4><span wicket:id="labelX"></span> <span wicket:id="labelY"></span> <span wicket:id="labelZ"></span></h4>
</wicket:panel>
总结:
我想在单击 row1 column2 的 link 时更新 table.
的 row1 column3 中的 labelX 值
labelX 在 table 列(在 labelY 和 labelZ 旁边)表示为灰色方块内的 X,如果它的值为 false,如果它的值为 true,则为红色。
有人知道怎么做吗?
我认为我应该使用事件,但我不知道如何使用,因为我有这么多 classes.
最简单、最干净的方法是使用 Wicket 事件。
在 onClick()
中广播带有负载的事件,例如:
send(findParent(DataTable.class), Broadcast.BREADTH, new YourPayload(guid, target));
guid
是 getPaymentGUID()
而 target
是 AjaxRequestTarget。
该事件将被发送到父数据 table,并从那里广播到其所有子数据。
在 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
}
}
}
我有一个 table,它有一列 links 和另一列布尔值,当我单击 link 时,另一列的布尔值应该改变到相反的值,即,如果它是假的,反之亦然。--> 这是我的目标。
这是一个简化的描述,table还有更多的列在这里没有描述,我有大量的class。
在主要 class 中,我添加了 table,并制作了列,如下面的摘录
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;
}
}
下一个 class 是定义 AjaxLink 的 class,用于列和 table 的每一行。
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"));
}
}
需要更新的class
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 class 是什么,下面是它的 html
<wicket:panel>
<h4><span wicket:id="labelX"></span> <span wicket:id="labelY"></span> <span wicket:id="labelZ"></span></h4>
</wicket:panel>
总结: 我想在单击 row1 column2 的 link 时更新 table.
的 row1 column3 中的 labelX 值labelX 在 table 列(在 labelY 和 labelZ 旁边)表示为灰色方块内的 X,如果它的值为 false,如果它的值为 true,则为红色。
有人知道怎么做吗? 我认为我应该使用事件,但我不知道如何使用,因为我有这么多 classes.
最简单、最干净的方法是使用 Wicket 事件。
在 onClick()
中广播带有负载的事件,例如:
send(findParent(DataTable.class), Broadcast.BREADTH, new YourPayload(guid, target));
guid
是 getPaymentGUID()
而 target
是 AjaxRequestTarget。
该事件将被发送到父数据 table,并从那里广播到其所有子数据。
在 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
}
}
}