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