0

I wrote the following code to load json file and convert it to table,I am getting error roleList is not defined.What I did wrong?
Is my code correct?

    $(document).ready(function(){
                // var roleList;
    		    $.getJSON('a.json', function(data) {
    		    	var roleList=data;
    		    	// console.log(rolelist);
    		        empRoles();  
    		    });
    });
    function empRoles(){
		for(var i=0;i<roleList.length;i++)
	    {
	    var table='<tr id="row'+i+'"><td>'+roleList[i].sNo+'</td><td class="roleName" id="name'+i+'">'+roleList[i].roleName+'</td><td><button class="btn edit btn-info" id="edit'+i+'"><i class="fa fa-pencil"></i>Edit</button><button class="btn update btn-success" id="update'+i+'"><i class="fa fa-floppy-o"></i>Update</button><button class="btn dlt btn-danger" data-toggle="modal" data-target="#confirm"><i class="fa fa-trash-o"></i>Delete</button></td><tr>';
	   	$('#roleListTable').append(table)
	    }
}

This is a.json file:

var data=[{
"sNo"     :"1",
"roleName":"Designer"
},
{
"sNo"     :"2",
"roleName":"Developer"
},
{
"sNo"     :"3",
"roleName":"HR Dept"
},
{
"sNo"     :"4",
"roleName":"Project Manager"
}
];

Html part:

                <table class="table">
                <thead class="roleListTableHead">
                <tr>
                <td>S.NO</td>
                <td>ROLE NAME</td>
                <td>ACTION</td>
                </tr>
                </thead>
                <tbody id="roleListTable">
                </tbody>            
                </table>

Can anyone give idea?

2
  • tried parseJSON? Commented Feb 17, 2017 at 9:00
  • @Vishal kumar No I did't try. Commented Feb 17, 2017 at 9:05

2 Answers 2

3

Pass roleList as an argument to empRoles() function.

var roleList = [{
    "sNo": "1",
    "roleName": "Designer"
  },
  {
    "sNo": "2",
    "roleName": "Developer"
  },
  {
    "sNo": "3",
    "roleName": "HR Dept"
  },
  {
    "sNo": "4",
    "roleName": "Project Manager"
  }
];

empRoles(roleList);

function empRoles(roleList) {
  for (var i = 0; i < roleList.length; i++) {
    var table = '<tr id="row' + i + '"><td>' + roleList[i].sNo + '</td><td class="roleName" id="name' + i + '">' + roleList[i].roleName + '</td><td><button class="btn edit btn-info" id="edit' + i + '"><i class="fa fa-pencil"></i>Edit</button><button class="btn update btn-success" id="update' + i + '"><i class="fa fa-floppy-o"></i>Update</button><button class="btn dlt btn-danger" data-toggle="modal" data-target="#confirm"><i class="fa fa-trash-o"></i>Delete</button></td><tr>';
    $('#roleListTable').append(table)
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
  <thead class="roleListTableHead">
    <tr>
      <td>S.NO</td>
      <td>ROLE NAME</td>
      <td>ACTION</td>
    </tr>
  </thead>
  <tbody id="roleListTable">
  </tbody>
</table>


You need to remove var data= from a.json file, the data structure for the JSON file not correct. See working Plunker

Sign up to request clarification or add additional context in comments.

5 Comments

@user7397787, there nothing wrong in the other code
@user7397787, Are you getting the data? check console.log(roleList)
please check this with above json file.
@user7397787 You need to remove var data= from a.json file. see plnkr.co/edit/eyyJPK?p=preview
yah yah thanks.Now I am getting data but table only not displaying
1

Use a each() loop, pass your data to the function as a argument

     $.getJSON('a.json', function(data) {
        empRoles(data);  
     });
     function empRoles(data) {
       $.each(data,function(i,roleList) {
        var table = '<tr id="row' + i + '"><td>' + roleList.sNo + '</td><td class="roleName" id="name' + i + '">' + roleList.roleName + '</td><td><button class="btn edit btn-info" id="edit' + i + '"><i class="fa fa-pencil"></i>Edit</button><button class="btn update btn-success" id="update' + i + '"><i class="fa fa-floppy-o"></i>Update</button><button class="btn dlt btn-danger" data-toggle="modal" data-target="#confirm"><i class="fa fa-trash-o"></i>Delete</button></td><tr>';
        $('#roleListTable').append(table)
       });
      }

var data = [{
    "sNo": "1",
    "roleName": "Designer"
  },
  {
    "sNo": "2",
    "roleName": "Developer"
  },
  {
    "sNo": "3",
    "roleName": "HR Dept"
  },
  {
    "sNo": "4",
    "roleName": "Project Manager"
  }
];

function empRoles(data) {
  $.each(data,function(i,roleList) {
    var table = '<tr id="row' + i + '"><td>' + roleList.sNo + '</td><td class="roleName" id="name' + i + '">' + roleList.roleName + '</td><td><button class="btn edit btn-info" id="edit' + i + '"><i class="fa fa-pencil"></i>Edit</button><button class="btn update btn-success" id="update' + i + '"><i class="fa fa-floppy-o"></i>Update</button><button class="btn dlt btn-danger" data-toggle="modal" data-target="#confirm"><i class="fa fa-trash-o"></i>Delete</button></td><tr>';
    $('#roleListTable').append(table)
  });
}

empRoles(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="roleListTable"></div>

2 Comments

I want to load separate json file.
you can do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.