JQuery Data table is way of present our data at client side and we can show case data and we can perform operations like sorting, update, page nation and many other operations we can perform on data table.
|
JavaScript Files
jquery-1.11.1.min.js
jquery.dataTables.min.js
CSS Files
jquery.dataTables.css
Note:
Above specified files available in direct at CDN point we can use those reference or we directly put in our application then refer them, either way is fine.
|
|
<!DOCTYPE html>
<html>
<head>
<script src= "https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src= "https://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<link rel= "stylesheet" href= "https://cdn.datatables.net/1.10.7/css/jquery.dataTables.css">
<script>
$(document).ready( function() {
$('# userTable').DataTable();
} );
</script>
</head>
<body>
<table id= "userTable" class= "display" cellspacing= "0" width= "100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$162,700</td>
</tr>
<tr>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>$372,000</td>
</tr>
<tr>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012/08/06</td>
<td>$137,500</td>
</tr>
<tr>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010/10/14</td>
<td>$327,900</td>
</tr>
<tr>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009/09/15</td>
<td>$205,500</td>
</tr>
<tr>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td>23</td>
<td>2008/12/13</td>
<td>$103,600</td>
</tr>
</tbody>
</table>
</body>
</html>
|
|
<%@page import= "com.liferay.portal.model.User"%>
<%@page import= "com.liferay.portal.service.UserLocalServiceUtil"%>
<%@include file= "init.jsp" %>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery-1.11.1.min.js"></script>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery.dataTables.min.js"></script>
<link rel= "stylesheet" href= "<%=renderRequest.getContextPath()%> /css/jquery.dataTables.css">
<style>
.message-container{
padding: 10px;
margin: 2px;
display: none;
background: rgba(128, 128, 128, 0.33);
border: 1px solid #0A0A0C;
}
</style>
<h2>Simple Jquery Data Table with Zero Configuration</h2>
<%
List<User> userList=UserLocalServiceUtil.getUsers(-1,-1);
%>
<c:if test= "<%=userList!= null&&!userList.isEmpty() %> ">
<table id= "userTable" class= "display" cellspacing= "0" width= "100%">
<thead>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</tfoot>
<tbody>
<% for(User curUser:userList){ %>
<tr>
<td><%=curUser.getUserId()%></td>
<td><%=curUser.getScreenName()%></td>
<td><%=curUser.getFirstName()%></td>
<td><%=curUser.getLastName()%></td>
<td><%=curUser.getEmailAddress()%></td>
<td><%=curUser.getJobTitle()%></td>
</tr>
<%} %>
</tbody>
</table>
<script>
$(document).ready( function() {
$('# userTable').DataTable();
} );
</script>
</c:if>
<c:if test= "<%=Validator.isNull(userList)||userList.isEmpty()%> ">
<div class= "message-container">No data to Display</div>
</c:if>
|
|
Data Table Java Script
$(document).ready(function() {
$(document).ready(function() {
$('#userTable').dataTable( {
"ajax": "<%=usersArrayURL%>"
} );
} );
} );
Data Format
{
"data":[
[
20199,
"joebloggs",
"Joe",
"Bloggs",
"test@liferay.com",
""
],
[
20536,
"ord1",
"Test",
"ORD 1",
"test.ord.1@liferay.com",
""
],
[
20543,
"ord2",
"Test",
"ORD 2",
"test.ord.2@liferay.com",
""
],
[
20550,
"ord3",
"Test",
"ORD 3",
"test.ord.3@liferay.com",
""
]
]
}
|
|
Data Table Java Script
$(document).ready(function() {
$('# userTable').dataTable( {
"ajax": "<%=usersObjectArrayURL%>",
"columns": [
{ "data": "userId" },
{ "data": "screeName" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "jobTitle" }
]
} );
} );
Data Format
{
"data":[
{
"lastName":"Bloggs",
"email":"test@liferay.com",
"screeName":"joebloggs",
"userId":20199,
"firstName":"Joe",
"jobTitle":""
},
{
"lastName":"ORD 1",
"email":"test.ord.1@liferay.com",
"screeName":"ord1",
"userId":20536,
"firstName":"Test",
"jobTitle":""
},
{
"lastName":"ORD 2",
"email":"test.ord.2@liferay.com",
"screeName":"ord2",
"userId":20543,
"firstName":"Test",
"jobTitle":""
},
{
"lastName":"ORD 3",
"email":"test.ord.3@liferay.com",
"screeName":"ord3",
"userId":20550,
"firstName":"Test",
"jobTitle":""
}
]
}
|
|
Data Table Java Script
$(document).ready(function() {
$('# userTable').dataTable( {
"ajax": {
"url":"<%=usersFlatDataSourceURL%>",
"dataSrc": ""
},
"columns": [
{ "data": "userId" },
{ "data": "screeName" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "jobTitle" }
]
} );
} );
Data Format
[
{
"lastName":"Bloggs",
"email":"test@liferay.com",
"screeName":"joebloggs",
"userId":20199,
"firstName":"Joe",
"jobTitle":""
},
{
"lastName":"ORD 1",
"email":"test.ord.1@liferay.com",
"screeName":"ord1",
"userId":20536,
"firstName":"Test",
"jobTitle":""
},
{
"lastName":"ORD 2",
"email":"test.ord.2@liferay.com",
"screeName":"ord2",
"userId":20543,
"firstName":"Test",
"jobTitle":""
},
{
"lastName":"ORD 3",
"email":"test.ord.3@liferay.com",
"screeName":"ord3",
"userId":20550,
"firstName":"Test",
"jobTitle":""
}
]
|
|
Data Table Java Script
$(document).ready(function() {
$('# userTable').dataTable( {
"ajax": {
"url":"<%=usersCustomDataSourceURL%>",
"dataSrc": " mytabledata"
},
"columns": [
{ "data": "userId" },
{ "data": "screeName" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "jobTitle" }
]
} );
} );
Data format
{
" mytabledata":[
{
"lastName":"Bloggs",
"email":"test@liferay.com",
"screeName":"joebloggs",
"userId":20199,
"firstName":"Joe",
"jobTitle":""
},
{
"lastName":"ORD 1",
"email":"test.ord.1@liferay.com",
"screeName":"ord1",
"userId":20536,
"firstName":"Test",
"jobTitle":""
},
{
"lastName":"ORD 2",
"email":"test.ord.2@liferay.com",
"screeName":"ord2",
"userId":20543,
"firstName":"Test",
"jobTitle":""
},
{
"lastName":"ORD 3",
"email":"test.ord.3@liferay.com",
"screeName":"ord3",
"userId":20550,
"firstName":"Test",
"jobTitle":""
}
]
}
|
|
Html
<table id=" userTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</tfoot>
</table>
Java Script
$(document).ready(function() {
$('# userTable').dataTable( {
"ajax": "<%=usersObjectArrayURL%>",
"columns": [
{ "data": "userId" },
{ "data": "screeName" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "jobTitle" }
],
"columnDefs": [
{
"targets": [ 0 ],
"visible": false,
"searchable": false
},
{
"targets": [ 5 ],
"visible": false
}
]
} );
} );
|
|
<%@page import= "com.liferay.portal.model.User"%>
<%@page import= "com.liferay.portal.service.UserLocalServiceUtil"%>
<%@include file= "init.jsp" %>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery-1.11.1.min.js"></script>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery.dataTables.min.js"></script>
<link rel= "stylesheet" href= "<%=renderRequest.getContextPath()%> /css/jquery.dataTables.css">
<style>
.message-container{
padding: 10px;
margin: 2px;
display: none;
background: rgba(128, 128, 128, 0.33);
border: 1px solid #0A0A0C;
}
</style>
<h2>Simple Jquery Data Table with Zero Configuration</h2>
<%
List<User> userList=UserLocalServiceUtil.getUsers(-1,-1);
%>
<c:if test= "<%=userList!= null&&!userList.isEmpty() %> ">
<table id= "userTable" class= "display" cellspacing= "0" width= "100%">
<thead>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</tfoot>
<tbody>
<% for(User curUser:userList){ %>
<tr>
<td><%=curUser.getUserId()%></td>
<td><%=curUser.getScreenName()%></td>
<td><%=curUser.getFirstName()%></td>
<td><%=curUser.getLastName()%></td>
<td><%=curUser.getEmailAddress()%></td>
<td><%=curUser.getJobTitle()%></td>
</tr>
<%} %>
</tbody>
</table>
<script>
$(document).ready( function() {
$('# userTable').DataTable();
} );
</script>
</c:if>
<c:if test= "<%=Validator.isNull(userList)||userList.isEmpty()%> ">
<div class= "message-container">No data to Display</div>
</c:if>
|
|
JSP Page
<%@page import= "com.liferay.portal.model.User"%>
<%@page import= "com.liferay.portal.service.UserLocalServiceUtil"%>
<%@include file= "init.jsp" %>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery-1.11.1.min.js"></script>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery.dataTables.min.js"></script>
<link rel= "stylesheet" href= "<%=renderRequest.getContextPath()%> /css/jquery.dataTables.css">
<portlet:resourceURL var= "usersObjectArrayURL">
<portlet:param name= "cmd" value= "jsonUserObjectArray"/>
</portlet:resourceURL>
<style>
.message-container{
padding: 10px;
margin: 2px;
display: none;
background: rgba(128, 128, 128, 0.33);
border: 1px solid #0A0A0C;
}
</style>
<h2> Jquery Data Table with Array Of Ojbects from Server Through Ajax</h2>
<table id= "userTable" class= "display" cellspacing= "0" width= "100%">
<thead>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready( function() {
$('# userTable').dataTable( {
"ajax": "<%=usersObjectArrayURL%>",
"columns": [
{ "data": "userId" },
{ "data": "screeName" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "jobTitle" }
]
} );
} );
</script>
Portlet Serve Resource Code
public void arrayOfJSONUserObjects(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
JSONArray usersJsonArray = JSONFactoryUtil. createJSONArray();
try {
logger.info("====serveResource========");
JSONObject jsonUser = null;
List<User> userList = UserLocalServiceUtil. getUsers(1,UserLocalServiceUtil. getUsersCount());
for (User userObj : userList) {
jsonUser = JSONFactoryUtil. createJSONObject();
jsonUser.put("userId", userObj.getUserId());
jsonUser.put("screeName", userObj.getScreenName());
jsonUser.put("firstName", userObj.getFirstName());
jsonUser.put("lastName", userObj.getLastName());
jsonUser.put("email", userObj.getEmailAddress());
jsonUser.put("jobTitle", userObj.getJobTitle());
usersJsonArray.put(jsonUser);
}
} catch (Exception e) {
e.printStackTrace();
}
JSONObject tableData = JSONFactoryUtil. createJSONObject();
tableData.put("data", usersJsonArray);
logger.info("tableData:"+tableData.toString());
ServletResponseUtil. write(PortalUtil. getHttpServletResponse(resourceResponse),
tableData.toString());
}
|
|
JSP Page
<%@page import= "com.liferay.portal.model.User"%>
<%@page import= "com.liferay.portal.service.UserLocalServiceUtil"%>
<%@include file= "init.jsp" %>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery-1.11.1.min.js"></script>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery.dataTables.min.js"></script>
<link rel= "stylesheet" href= "<%=renderRequest.getContextPath()%> /css/jquery.dataTables.css">
<portlet:resourceURL var= "usersArrayURL">
<portlet:param name= "cmd" value= "jsonUsersArray"/>
</portlet:resourceURL>
<style>
.message-container{
padding: 10px;
margin: 2px;
display: none;
background: rgba(128, 128, 128, 0.33);
border: 1px solid #0A0A0C;
}
</style>
<h2> Jquery Data Table with Array Data from Server Through Ajax</h2>
<table id= "userTable" class= "display" cellspacing= "0" width= "100%">
<thead>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready( function() {
$(document).ready( function() {
$('# userTable').dataTable( {
"ajax": "<%=usersArrayURL%>"
} );
} );
} );
</script>
Portlet Serve Resource Code
public void arrayOfJSONUserData(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
JSONArray allUsersJsonArray = JSONFactoryUtil. createJSONArray();
try {
logger.info("====serveResource========");
JSONArray jsonUserArray = null;
List<User> userList = UserLocalServiceUtil. getUsers(1,UserLocalServiceUtil. getUsersCount());
for (User userObj : userList) {
jsonUserArray = JSONFactoryUtil. createJSONArray();
jsonUserArray.put(userObj.getUserId());
jsonUserArray.put( userObj.getScreenName());
jsonUserArray.put(userObj.getFirstName());
jsonUserArray.put(userObj.getLastName());
jsonUserArray.put(userObj.getEmailAddress());
jsonUserArray.put(userObj.getJobTitle());
allUsersJsonArray.put(jsonUserArray);
}
} catch (Exception e) {
e.printStackTrace();
}
JSONObject tableData = JSONFactoryUtil. createJSONObject();
tableData.put("data", allUsersJsonArray);
logger.info("tableData:"+tableData.toString());
ServletResponseUtil. write(PortalUtil. getHttpServletResponse(resourceResponse),
tableData.toString());
}
|
|
JSP Page
<%@page import= "com.liferay.portal.model.User"%>
<%@page import= "com.liferay.portal.service.UserLocalServiceUtil"%>
<%@include file= "init.jsp" %>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery-1.11.1.min.js"></script>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery.dataTables.min.js"></script>
<link rel= "stylesheet" href= "<%=renderRequest.getContextPath()%> /css/jquery.dataTables.css">
<portlet:resourceURL var= "usersFlatDataSourceURL">
<portlet:param name= "cmd" value= "flatDataSource"/>
</portlet:resourceURL>
<style>
.message-container{
padding: 10px;
margin: 2px;
display: none;
background: rgba(128, 128, 128, 0.33);
border: 1px solid #0A0A0C;
}
</style>
<h2> Jquery Data Table with Flat Array Of Ojbects from Server Through Ajax</h2>
<table id= "userTable" class= "display" cellspacing= "0" width= "100%">
<thead>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready( function() {
$('# userTable').dataTable( {
"ajax": {
"url":"<%=usersFlatDataSourceURL%>",
"dataSrc": ""
},
"columns": [
{ "data": "userId" },
{ "data": "screeName" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "jobTitle" }
]
} );
} );
</script>
Portlet Serve Resource Code
public void flatDataSource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
JSONArray usersJsonArray = JSONFactoryUtil. createJSONArray();
try {
logger.info("====serveResource========");
JSONObject jsonUser = null;
List<User> userList = UserLocalServiceUtil. getUsers(1,UserLocalServiceUtil. getUsersCount());
for (User userObj : userList) {
jsonUser = JSONFactoryUtil. createJSONObject();
jsonUser.put("userId", userObj.getUserId());
jsonUser.put("screeName", userObj.getScreenName());
jsonUser.put("firstName", userObj.getFirstName());
jsonUser.put("lastName", userObj.getLastName());
jsonUser.put("email", userObj.getEmailAddress());
jsonUser.put("jobTitle", userObj.getJobTitle());
usersJsonArray.put(jsonUser);
}
} catch (Exception e) {
e.printStackTrace();
}
logger.info("tableData:"+usersJsonArray.toString());
ServletResponseUtil. write(PortalUtil. getHttpServletResponse(resourceResponse),
usersJsonArray.toString());
}
|
|
JSP Page
<%@page import= "com.liferay.portal.model.User"%>
<%@page import= "com.liferay.portal.service.UserLocalServiceUtil"%>
<%@include file= "init.jsp" %>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery-1.11.1.min.js"></script>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery.dataTables.min.js"></script>
<link rel= "stylesheet" href= "<%=renderRequest.getContextPath()%> /css/jquery.dataTables.css">
<portlet:resourceURL var= "usersCustomDataSourceURL">
<portlet:param name= "cmd" value= "customDataProperty"/>
</portlet:resourceURL>
<style>
.message-container{
padding: 10px;
margin: 2px;
display: none;
background: rgba(128, 128, 128, 0.33);
border: 1px solid #0A0A0C;
}
</style>
<h2> Jquery Data Table with Custom data source property from Server Through Ajax</h2>
<table id= "userTable" class= "display" cellspacing= "0" width= "100%">
<thead>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready( function() {
$('# userTable').dataTable( {
"ajax": {
"url":"<%=usersCustomDataSourceURL%>",
"dataSrc": "mytabledata"
},
"columns": [
{ "data": "userId" },
{ "data": "screeName" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "jobTitle" }
]
} );
} );
</script>
Portlet Serve Resource Code
public void customDataObjects(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
JSONArray usersJsonArray = JSONFactoryUtil. createJSONArray();
try {
logger.info("====serveResource========");
JSONObject jsonUser = null;
List<User> userList =
UserLocalServiceUtil. getUsers(1,UserLocalServiceUtil. getUsersCount());
for (User userObj : userList) {
jsonUser = JSONFactoryUtil. createJSONObject();
jsonUser.put("userId", userObj.getUserId());
jsonUser.put("screeName", userObj.getScreenName());
jsonUser.put("firstName", userObj.getFirstName());
jsonUser.put("lastName", userObj.getLastName());
jsonUser.put("email", userObj.getEmailAddress());
jsonUser.put("jobTitle", userObj.getJobTitle());
usersJsonArray.put(jsonUser);
}
} catch (Exception e) {
e.printStackTrace();
}
JSONObject tableData = JSONFactoryUtil. createJSONObject();
tableData.put("mytabledata", usersJsonArray);
logger.info("tableData:"+tableData.toString());
ServletResponseUtil. write(PortalUtil. getHttpServletResponse(resourceResponse),
tableData.toString());
}
}
|
|
JSP
<%@page import= "com.liferay.portal.model.User"%>
<%@page import= "com.liferay.portal.service.UserLocalServiceUtil"%>
<%@include file= "init.jsp" %>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery-1.11.1.min.js"></script>
<script src= "<%=renderRequest.getContextPath()%> /js/jquery.dataTables.min.js"></script>
<link rel= "stylesheet" href= "<%=renderRequest.getContextPath()%> /css/jquery.dataTables.css">
<portlet:resourceURL var= "usersObjectArrayURL">
<portlet:param name= "cmd" value= "jsonUserObjectArray"/>
</portlet:resourceURL>
<style>
.message-container{
padding: 10px;
margin: 2px;
display: none;
background: rgba(128, 128, 128, 0.33);
border: 1px solid #0A0A0C;
}
</style>
<h2> Jquery Data Table Hidden Columns</h2>
<table id= "userTable" class= "display" cellspacing= "0" width= "100%">
<thead>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th>User Id</th>
<th>Screen Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Job Title</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready( function() {
$('# userTable').dataTable( {
"ajax": "<%=usersObjectArrayURL%>",
"columns": [
{ "data": "userId" },
{ "data": "screeName" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "jobTitle" }
],
"columnDefs": [
{
"targets": [ 0 ],
"visible": false,
"searchable": false
},
{
"targets": [ 5 ],
"visible": false
}
]
} );
} );
</script>
Portlet Serve Resource Code
public void arrayOfJSONUserObjects(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
JSONArray usersJsonArray = JSONFactoryUtil. createJSONArray();
try {
logger.info("====serveResource========");
JSONObject jsonUser = null;
List<User> userList =
UserLocalServiceUtil. getUsers(1,UserLocalServiceUtil. getUsersCount());
for (User userObj : userList) {
jsonUser = JSONFactoryUtil. createJSONObject();
jsonUser.put("userId", userObj.getUserId());
jsonUser.put("screeName", userObj.getScreenName());
jsonUser.put("firstName", userObj.getFirstName());
jsonUser.put("lastName", userObj.getLastName());
jsonUser.put("email", userObj.getEmailAddress());
jsonUser.put("jobTitle", userObj.getJobTitle());
usersJsonArray.put(jsonUser);
}
} catch (Exception e) {
e.printStackTrace();
}
JSONObject tableData = JSONFactoryUtil. createJSONObject();
tableData.put("data", usersJsonArray);
logger.info("tableData:"+tableData.toString());
ServletResponseUtil. write(PortalUtil. getHttpServletResponse(resourceResponse),
tableData.toString());
}
|


