You need to sign in to do that
Don't have an account?

search functionality on a visual force page
i have a requirement to display custom object attachments on a visualforce page with a search functioanlity(able to search by the document name) i can able to develope the visualforce page to display the attachments in a visualforce page. please help me in writing the search functionality.
below is the code
VF page
<apex:page controller="KnowledgeBaseAttachment" showheader="false" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="2">
<apex:repeat value="{!listAttachment}" var="att">
<apex:pageBlockSectionItem >
<apex:outputLink value="{!URLFOR($Action.Attachment.Download, att.id)}" target="_blank">{!att.name}
</apex:outputLink>
</apex:pageBlockSectionItem>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
controller code
public class KnowledgeBaseAttachment {
public List<Attachment> listAttachment {get; set;}
public KnowledgeBaseAttachment() {
listAttachment = new List<Attachment>();
List<aeturu__Knowlegde_Base__c> lstKnowledgeBase = [Select Id, (Select Id, Name from Attachments)
from aeturu__Knowlegde_Base__c];
for (aeturu__Knowlegde_Base__c obj : lstKnowledgeBase) {
listAttachment.addAll(obj.Attachments);
}
}
}
any help is appriciated
thanks
sarvam
below is the code
VF page
<apex:page controller="KnowledgeBaseAttachment" showheader="false" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="2">
<apex:repeat value="{!listAttachment}" var="att">
<apex:pageBlockSectionItem >
<apex:outputLink value="{!URLFOR($Action.Attachment.Download, att.id)}" target="_blank">{!att.name}
</apex:outputLink>
</apex:pageBlockSectionItem>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
controller code
public class KnowledgeBaseAttachment {
public List<Attachment> listAttachment {get; set;}
public KnowledgeBaseAttachment() {
listAttachment = new List<Attachment>();
List<aeturu__Knowlegde_Base__c> lstKnowledgeBase = [Select Id, (Select Id, Name from Attachments)
from aeturu__Knowlegde_Base__c];
for (aeturu__Knowlegde_Base__c obj : lstKnowledgeBase) {
listAttachment.addAll(obj.Attachments);
}
}
}
any help is appriciated
thanks
sarvam
Can u please check with below code it will works for you,
<apex:page controller="Attachmentsviewcontroller" showHeader="false">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$.noConflict();
jQuery(document).ready(function() {
jQuery.extend(jQuery.expr[":"], {
"containsIN": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
jQuery(document).on("keyup", ".searchbynameautsearch", function() {
jQuery('.list tbody tr').each(function() {
jQuery(this).css("display", "table-row");
})
var searchterm = jQuery(this).val();
if (searchterm.length > 0) {
var lmatch = jQuery('.list tbody tr').text();
var match = jQuery('.list tbody tr:containsIN("' + searchterm + '")');
var nomatch = jQuery('.list tbody tr:not(:containsIN("' + searchterm + '"))');
match.addClass('selected');
nomatch.css("display", "none");
} else {
jQuery('.list tbody tr').css("display", "");
jQuery('.list tbody tr').removeClass('selected');
}
});
});
</script>
<apex:form ><br/><br/>
Search records :<apex:inputText value="{!recnamesearch}" styleClass="searchbynameautsearch serchfld" style="padding-left:5px;" />
<apex:pageblock id="account" title="Attachments" >
<apex:pageblockTable value="{!att}" var="a">
<apex:column value="{!a.Name}" headerValue="File Name"/>
<apex:column value="{!a.BodyLength }" headerValue="Size"/>
</apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>
public class Attachmentsviewcontroller{
public List<Attachment> att{get;set;}
Public string recnamesearch{get;set;}
public Attachmentsviewcontroller()
{
att=[Select a.Id,a.a.Name,a.BodyLength From Attachment a];
}
}
Above the code is an Global Search if you want to only name based search you have add id on name column.
Thanks,
Raghavendra Reddy.D
All Answers
Can u please check with below code it will works for you,
<apex:page controller="Attachmentsviewcontroller" showHeader="false">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$.noConflict();
jQuery(document).ready(function() {
jQuery.extend(jQuery.expr[":"], {
"containsIN": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
jQuery(document).on("keyup", ".searchbynameautsearch", function() {
jQuery('.list tbody tr').each(function() {
jQuery(this).css("display", "table-row");
})
var searchterm = jQuery(this).val();
if (searchterm.length > 0) {
var lmatch = jQuery('.list tbody tr').text();
var match = jQuery('.list tbody tr:containsIN("' + searchterm + '")');
var nomatch = jQuery('.list tbody tr:not(:containsIN("' + searchterm + '"))');
match.addClass('selected');
nomatch.css("display", "none");
} else {
jQuery('.list tbody tr').css("display", "");
jQuery('.list tbody tr').removeClass('selected');
}
});
});
</script>
<apex:form ><br/><br/>
Search records :<apex:inputText value="{!recnamesearch}" styleClass="searchbynameautsearch serchfld" style="padding-left:5px;" />
<apex:pageblock id="account" title="Attachments" >
<apex:pageblockTable value="{!att}" var="a">
<apex:column value="{!a.Name}" headerValue="File Name"/>
<apex:column value="{!a.BodyLength }" headerValue="Size"/>
</apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>
public class Attachmentsviewcontroller{
public List<Attachment> att{get;set;}
Public string recnamesearch{get;set;}
public Attachmentsviewcontroller()
{
att=[Select a.Id,a.a.Name,a.BodyLength From Attachment a];
}
}
Above the code is an Global Search if you want to only name based search you have add id on name column.
Thanks,
Raghavendra Reddy.D