0

kindly let me know how to display list tabres on vf page . actually in alert box its displaying all the 5 values. but when i am calling it in div tag having id mytable1 its displaying undefined. i think i am very close. kindly help. apex class code:-

    public class javascriptremoting {
  @remoteAction
    public static List<Account> loadaccs(){

        return [select Id,Name,Phone from Account where Phone !=null];


        }

vf page code:-

    <apex:page controller="javascriptremoting" >
     <script>
          Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.javascriptremoting.loadaccs}',
        function(result, event){
            var tabres= [];
    alert(result);
            for(i=0;i<5;i++){
            tabres.push(result[i].Name);
            }
             alert(tabres);

 for(i in tabres){
        firstnames = firstnames + tabres[i].Name;
        }
     document.getElementById('mytable1').innerHTML = firstnames;

      }, 
        {escape: true}
    );
    </script>

      <div  id="mytable1" >
                </div>

2 Answers 2

0

You can't show the JavaScript array directly in HTML. You have to create the HTML table String then insert inside the div element.

var firstnames = "<table>"
for(i in tabres){
    firstnames = firstnames + "<tr><td>" +tabres[i]+ "</td></tr>";
}
firstnames = firstnames+"</table>";
document.getElementById('mytable1').innerHTML = firstnames;
5
  • yeah the thing that worked for me is this that i have to remove .Name from tabres[i].
    – vineet
    Commented May 23, 2017 at 15:26
  • thnx a lot. one small issue i am getting some useless thing like 'function (d){d=this.indexOf(d);-1!=d&&this.splice(d,1);return this}' in the last line,kindly let me know how to remove this.
    – vineet
    Commented May 23, 2017 at 15:27
  • I can also see the issue. I think Salesforce is injecting something in the array. You can resolve this by using classic for loop syntax for(i=0;i<tabres.length;i++){
    – Saroj Bera
    Commented May 24, 2017 at 6:11
  • thnks again sir. Really appreciate ur help. Actually my requirement was of pagination. So, i think i am moving in the right direction,if possible u can help me on that also.
    – vineet
    Commented May 24, 2017 at 8:15
  • Sure, if you face any issue post a new Question. You will definitely get help from the forum.
    – Saroj Bera
    Commented May 24, 2017 at 9:19
0

Make your controller Global. Please check modified code below:

VF:

<apex:page controller="javascriptremoting" >
     <script>
          Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.javascriptremoting.loadaccs}',
            function(result, event){
                var l = result.length;

                if(l === 0) {
                    document.getElementById('mytable1').innerHTML = "<p> No result </p>";
                } else {
                    var markUp = "<table border=1><tr><th>Id</th><th>Name</th><th>Phone</th></tr> ";
                    for(var i=0; i < l; i++) {
                        markUp += "<tr>" +
                                        "<td>" + result[i].Id + "</td>" +
                                        "<td>" + result[i].Name + "</td>" +
                                        "<td>" + result[i].Phone + "</td>" +
                                   "</tr> ";
                    }
                    markUp += "</table>";
                    document.getElementById('mytable1').innerHTML = markUp;
                }
      }, 
        {escape: true}
    );
    </script>

      <div  id="mytable1" >  </div>
</apex:page>

Controller:

global class javascriptremoting {
    @remoteAction
    public static List<Account> loadaccs() {

        return [select Id,Name,Phone from Account where Phone != null];


    }
}
1
  • thnx. actually i dont need to show all the records. i need first 5 records initially and then on the next click i need another 5 rcords,like pagination
    – vineet
    Commented May 23, 2017 at 15:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .