简体   繁体   English

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

[英]Salesforce- Using jQuery to delete table rows using checkbox

Right now the error I am getting is Invalid field checked for sObject People_Language__c. 现在,我得到的错误是为sObject People_Language__c检查了无效字段。

Not sure if it doesn't like the use of the wrapper class or what but I don't see the problem. 不知道它是否不喜欢使用包装器类或什么,但是我看不到问题。

Here is the 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; 
    }
}
}

visualforce visualforce

<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 might be useful if you wanted to hide the rows from the page, but actually deleting them would need to be handled by your Apex controller. 如果您想隐藏页面中的行,则jQuery可能有用,但是实际上删除它们将需要由Apex控制器处理。 Probably the easiest way of doing this is by using a wrapper class in your controller, and then your deleteLanguage() method would be able to loop through and remove any records where the checkbox has been checked. 可能最简单的方法是在控制器中使用包装器类 ,然后您的deleteLanguage()方法将能够遍历并删除选中了复选框的所有记录。 jQuery isn't necessary for this approach. jQuery对于这种方法不是必需的。

A simple Apex controller using this approach: 使用此方法的简单Apex控制器:

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; 
        }
    }
}

And then your VF markup becomes: 然后,您的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>

Disclaimer: none of this code has been tested, this is just me throwing an example together in Notepad++. 免责声明:这些代码均未经过测试,这只是我在Notepad ++中举了一个例子。 Hopefully this puts you on the right track! 希望这能使您走上正确的轨道!

Update : Okay, I threw together a data model like yours and the updated code compiles and seems to function as intended. 更新 :好的,我整理了一个像您这样的数据模型,并且更新的代码可以编译,并且似乎可以正常运行。 You should not be getting the Invalid field checked for sObject People_Language__c error on the VF page because this field is part of the wrapper - make sure you have {!lang.checked} as the value of the apex:inputCheckbox tag, rather than {!lang.pl.checked} . 您不应在VF页面上Invalid field checked for sObject People_Language__c错误,因为此字段是包装器的一部分-确保您将{!lang.checked}作为apex:inputCheckbox标记的值,而不是{!lang.pl.checked}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM