gpt4 book ai didi

javascript - Salesforce-使用 jQuery 使用复选框删除表行

转载 作者:行者123 更新时间:2023-11-28 01:56:24 24 4
gpt4 key购买 nike

现在我收到的错误是 sObject People_Language__c 检查的字段无效。

不确定它是否不喜欢使用包装类或什么,但我没有看到问题。

这是 Controller

public with sharing class myClass {

public String pId {get; set;}
public list<Wrapper>pLanguages {get; set;}




//CONSTRUCTOR
public myClass(){
pId = ApexPages.CurrentPage().getparameters().get('id');

pLanguages = new List<Wrapper>();
for ( People_Language__c pl : [SELECT Id, Language__c, Fluency__c FROM People_Language__c WHERE Person__c=:pId] ) {
pLanguages.add(new Wrapper(pl));
}
}


public void deleteLanguage(){
List<People_Language__c> langsToDelete = new List<People_Language__c>();
for ( Integer i = 0; i < pLanguages.size(); i++ ) {
if ( pLanguages[i].checked ) {
langsToDelete.add(pLanguages.remove(i--).pl);
}
}
delete langsToDelete;
}

//WRAPPER
public class Wrapper {
public boolean checked {get; set;}
public People_Language__c pl {get; set;}
public Wrapper(People_Language__c pl) {
this.pl = pl;
this.checked = false;
}
}
}

视觉力量

<apex:pageBlock title="Language" id="language"> 
<apex:inputHidden id="delLanguage" value="{!languagesToDelete}"></apex:inputHidden>
<apex:pageBlockButtons location="top">
<apex:commandButton id="langNewBtn" value="{!$Label.New}" />
<apex:commandButton id="LangDel" value="{!$Label.del2}" action="{!deleteLanguage}" rerender="language"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!pLanguages}" var="lang" title="People Language">
<apex:column width = "25px">
<apex:inputCheckbox value="{!lang.checked}" />
</apex:column>
<apex:column styleClass="actionColumn" width = "25px">
<apex:facet name="header"> <apex:outputText value="Action" /> </apex:facet>
<apex:outputLink styleClass="actionLink" target="_top">Del</apex:outputLink>
</apex:column>
<apex:column >
<apex:facet name="header"> <apex:outputText value="Language" /> </apex:facet>
<apex:outputField value="{!lang.pl.Language__c}" />
</apex:column>
<apex:column >
<apex:facet name="header"> <apex:outputText value="Fluency" /> </apex:facet>
<apex:outputField value="{!lang.pl.Fluency__c}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>

最佳答案

如果您想隐藏页面中的行,jQuery 可能会很有用,但实际上删除它们需要由您的 Apex Controller 来处理。也许最简单的方法是使用 wrapper class在您的 Controller 中,然后您的 deleteLanguage() 方法将能够循环并删除已选中复选框的任何记录。此方法不需要 jQuery。

使用这种方法的简单 Apex Controller :

public with sharing class PeopleLanguagesController {
public String pId {get; set;}
public List<Wrapper> pLanguages {get; set;}

//CONSTRUCTOR
public PeopleLanguagesController() {
pId = ApexPages.CurrentPage().getparameters().get('id');
pLanguages = new List<Wrapper>();
for ( People_Language__c pl : [SELECT Id, Language__c, Fluency__c FROM People_Language__c WHERE Person__c = :pId] ) {
pLanguages.add(new Wrapper(pl));
}
}

public PageReference deleteLanguage() {
List<People_Language__c> langsToDelete = new List<People_Language__c>();
for ( Integer i = 0; i < pLanguages.size(); i++ ) {
if ( pLanguages[i].checked ) {
langsToDelete.add((pLanguages.remove(i--)).pl);
}
}
delete langsToDelete;
return null;
}

//WRAPPER
public class Wrapper {
public boolean checked {get; set;}
public People_Language__c pl {get; set;}
public Wrapper(People_Language__c pl) {
this.pl = pl;
this.checked = false;
}
}
}

然后你的 VF 标记就变成:

<apex:page controller="PeopleLanguagesController">
<apex:form>
<apex:pageBlock title="Language" id="language">
<apex:pageBlockButtons location="top">
<!--<apex:commandButton id="langNewBtn" value="{!$Label.New}" />-->
<apex:commandButton id="LangDel" value="Delete Languages" action="{!deleteLanguage}" rerender="language"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!pLanguages}" var="lang" title="People Language">
<apex:column width = "25px">
<apex:inputCheckbox value="{!lang.checked}" />
</apex:column>
<apex:column styleClass="actionColumn" width = "25px">
<apex:facet name="header"> <apex:outputText value="Action" /> </apex:facet>
<apex:outputLink styleClass="actionLink" target="_top">Del</apex:outputLink>
</apex:column>
<apex:column >
<apex:facet name="header"> <apex:outputText value="Language" /> </apex:facet>
<apex:outputField value="{!lang.pl.Language__c}" />
</apex:column>
<apex:column >
<apex:facet name="header"> <apex:outputText value="Fluency" /> </apex:facet>
<apex:outputField value="{!lang.pl.Fluency__c}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

免责声明:这些代码都没有经过测试,这只是我在 Notepad++ 中编写的一个示例。希望这能让您走上正轨!

更新:好的,我将像您这样的数据模型放在一起,更新后的代码可以编译并且似乎可以按预期运行。您不应该在 VF 页面上收到 Invalid field Checked for sObject People_Language__c 错误,因为该字段是包装器的一部分 - 确保您有 {!lang.checked}作为 apex:inputCheckbox 标记的值,而不是 {!lang.pl.checked}

关于javascript - Salesforce-使用 jQuery 使用复选框删除表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19033266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com