5

I've been playing around with an Apex:DataTable in the past few days and would ideally like to find a way to filter out rows from the table:

  • Without using a controller and SOQL query to feed in a customized list to the table
  • While using visualforce syntax

I know that I can accomplish this via controller/SOQL and via an html table with outputPanels having a conditional render around data rows. But the trade-off is that I don't want a SOQL query slowing down the page, and I want to put the onus of maintenance on Salesforce by using their syntax.

I think I'm being too picky, but my task for the afternoon is to find a way. Is there one?

  • You can use jquery plugin to achieve filtering on datatable – Mohith Shrivastava Nov 29 '13 at 20:04
  • Thanks. Do you have a link to point me in the right direction? – Canadian Coder Nov 29 '13 at 20:18
  • Found some plugins. There is some good stuff there, but it doesn't seem to be what I'm looking for. The plugins I found seem to offer a search on the data-table to filter rows. What I'm interested in is filtering out any rows that meet a certain condition on the loading of the table, and the rest of the data rows would be static. – Canadian Coder Nov 29 '13 at 20:27
  • From what source the information comes into the dataTable? – Sergej Utko Nov 29 '13 at 21:06
9

I don't want a SOQL query slowing down the page

It doesn't matter if it's your query or salesforce's query - a query needs to be run in order to get data to be displayed in your table.

I can accomplish this via controller/SOQL and via an html table with outputPanels having a conditional render around data rows.

If your query includes only the rows which you wish to see in the table, you don't need to do anything fancy in the table. Optimize the query, don't make your VF more complicated.

put the onus of maintenance on Salesforce by using their syntax

This is accomplished using SOQL queries and VF. Using native platform features (i.e. not client-side scripting) is the best path to safe code which will not be prone to break in the future due to platform changes.

  • 1
    But, in all fairness, Visualforce can't use controllers in native code on Professional Ediiton, so an answer that includes how to actually filter the data isn't all bad. – sfdcfox Dec 1 '13 at 8:17
  • @sfdcfox, good point. The question made no mention of the edition being used and I always seem to forget about the Apex limitations in Professional Edition. Thanks for adding that example. – Mark Pond Dec 1 '13 at 20:03
3

If you render the page using salesforce's own query generator, you'll end up querying more data than you're going to actually render. Worse, expressions are going to build up in your view state, so large queries could have a devastating effect on your page's ability to perform at all. Finally, since you want to put the fate of your code on salesforce.com's head, you also have to accept that you will be forced to do things you will not like, such as exchanging apex:dataTable or apex:pageBlockTable for apex:repeat. A trivial example follows.

In controller-driven syntax, we can perform something like this:

<apex:dataTable value="{!contactList}" var="Contact">
    <apex:column value="{!Contact.FirstName}"/>
    <apex:column value="{!Contact.LastName}"/>
</apex:dataTable>

Without a controller, we are forced to determine if each row should be rendered, producing horribly inefficient code:

<table>
<thead>
<tr>
    <th>First Name</th>
    <th>Last Name</th>
</tr>
</thead>
<tbody>
<apex:repeat value="{!Account.Contacts}" var="Contact">
    <apex:outputText rendered="{!Contact.Email<>'asdf@asdf.com'}">
        <tr>
            <td>{!Contact.FirstName}</td>
            <td>{!Contact.LastName}</td>
        </tr>
    </apex:outputText>
</apex:repeat>
</tbody>
</table>

As you can see, we lose all capability to use simple syntax, and instead have to rely on standard HTML design. We also lose the standard salesforce.com formatting, which we have to then emulate ourselves if we are to look like the platform does.

Use this technique if you must, but be aware that you're not filtering the query, only the output, thus greatly degrading performance, ballooning the view state, and generally making a mess of things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.