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

时间:2013-09-26 16:08:17

标签: javascript jquery salesforce apex-code visualforce

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

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

这是控制器

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

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

1 个答案:

答案 0 :(得分:1)

如果你想隐藏页面中的行,jQuery可能会很有用,但实际上删除它们需要由你的Apex控制器处理。可能最简单的方法是在控制器中使用wrapper class,然后您的deleteLanguage()方法就可以循环并删除已选中复选框的所有记录。这种方法不需要jQuery。

使用此方法的简单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; 
        }
    }
}

然后您的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标记的值,而不是{{ 1}}。