I have dataTable and returns correct data, but the UI is my problem meaning it does not have columns and sorting are not looking good, here is my logic below and need some help to make look good. I have also attached the back end code for clarity as to why this code, User Interface does not have rows with data grid columns.
[![enter image description here][1]][1]
<!--Building some table here that filters data from db-->
<div style="width:90%; margin:0 auto;">
<table id="eNtsaCourseLists">
<thead>
<tr>
<th>Id</th>
<th>Course</th>
<th>Nickname</th>
<th>Term</th>
<th>Published</th>
<th>CourseLicence</th>
</tr>
</thead>
</table>
</div>
<style>
tr.even {
background-color: #F5F5F5 !important;
}
</style>
<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
</center>
<!--Javascript functionality for dataTable-->
<script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script>
$(function () {
$("#eNtsaCourseLists").dataTable({
"ajax": {
"url": "/Home/loadTableResults",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "Id", "autowidth": true },
{ "data": "Course", "autowidth": true },
{ "data": "Nickname", "autowidth": true },
{ "data": "Term", "autowidth": true },
{ "data": "Published", "autowidth": true },
{"data": "CourseLicence", "autowidth":true}
]
});
});
</script>
public ActionResult loadTableResults()
{
//RegCoursesViewModel regCourses = new RegCoursesViewModel();
using (eNtsaRegistration_2 vb = new eNtsaRegistration_2())
{
var data = vb.Courses.OrderBy(n => n.Course).ToList();
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
}
[1]: https://i.stack.imgur.com/wF76c.png
Related
Using DataTable API, I am creating a table & adding a button - 'Click' to each table row. Example link below:
https://datatables.net/examples/ajax/null_data_source.html
On the click I am changing text to 'View'. So, after page reload, rather all the buttons displaying default text - 'Click', how to render some buttons with text - 'Click' while the others which were clicked with - 'View'. Experts please share your inputs!!
$(document).ready(function() {
var table = $('#example').DataTable( {
"ajax": {
"type": "get",
"url": "https://jsonplaceholder.typicode.com/todos",
"dataType": "json",
"dataSrc": function (json) {
var return_data = new Array();
for (var i = 0; i < json.length; i++) {
return_data.push({
'userId': json[i].userId,
'id': json[i].id,
'title': "Test Data"
})
}
return return_data;
}
},
"columns": [
{ 'data': 'userId' },
{ 'data': 'id' },
{ 'data': 'title' },
{ 'data': null }
],
"columnDefs": [
{ targets: 0, className: 'dt-body-center'},
{ targets: 1, className: 'dt-body-center'},
{ targets: 2, className: 'dt-body-center'},
{ targets: -1, width: "150px",
className: 'dt-body-center', defaultContent:
"<button id='btnDetails'>Click</buttom>" }
]});
$('#example tbody').on('click', '[id*=btnDetails]', function () {
$(this).text("View");
});
});
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"> </script>
</head>
<body>
<table id="example" class="display cell-border compact stripe" style="width:100%">
<thead>
<tr>
<th>User Id</th>
<th>Id</th>
<th>Title</th>
<th>Status</th>
</tr>
</thead>
</table>
</body>
</html>
You need a database to persist the state of button. Check below link for server-side processing:
https://datatables.net/examples/data_sources/server_side.html
Hello guys i got a problem, i cant display my graph it will become like this
As u can see there i also print console log.Nothing error occur.
Below is my index.php
<!DOCTYPE html>
<html>
<head>
<title>Line</title>
</head>
<body>
<div class="chart-container">
<canvas id="line-chartcanvas"></canvas>
</div>
<script src="js/Chart.bundle.min.js"></script>
<script src="js/jquery.js"></script>
<script src="js/chartmain.js"></script>
</body>
</html>
My chartmain.js
$(function(){
$.ajax({
url:"http://localhost/Media/chart.php",
type: "GET",
success : function (data) {
var count = [];
var displayLink = [];
for(var i in data){
count.push ("Player" + data[i].count);
displayLink.push(data[i].displayLink);
}
var chartdata = {
labels: displayLink,
datasets : [
{
data: displayLink,
label : 'Score',
backgroundColor:
"#F1c40F"
}
]
};
var ctx =$("#line-chartcanvas");
var barGraph = new Chart(ctx,{
type: 'bar',
data: chartdata
});
console.log(data);
},
error : function (data) {
console.log(data);
},
});
});
My Json Array
[
{
"count": "3",
"displayLink": "www.bharian.com.my"
},
{
"count": "1",
"displayLink": "www.hmetro.com.my"
},
{
"count": "6",
"displayLink": "www.nst.com.my"
},
{
"count": "1",
"displayLink": "www.sinarharian.com.my"
},
{
"count": "1",
"displayLink": "www.utusan.com.my"
}
]
I think perhaps it cant read my data from JSON but in console log it read
Thanks for helping me, already found the answer, I cant get data from JSON but i can data from MySQL
I'm trying to use server grouping in a grid.
I'm not sure about the difference between "schema.groups" and "schema.data". I understand that i should use shema.data when data are not grouped, and schema.groups when data are grouped. I tryied to provide a very simple example, with a ajax request to a data.json file to simulate a server response. Just drop the testGrouping.html and data.json files in the root of any http server to reproduce my problem.
When i run the given code, i have no error, but the grid remains empty. I expect the grid to show 1 group with 5 lines, without any aggregate.
Could you help to find what's wrong in the given sample ?
Thank you for your help.
Here is the html page i'm using (testGrouping.html) :
<!DOCTYPE html>
<html>
<head>
<style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
<title></title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.1.223/styles/kendo.common-material.min.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.1.223/styles/kendo.material.min.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.1.223/styles/kendo.material.mobile.min.css" />
<script src="https://kendo.cdn.telerik.com/2017.1.223/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2017.1.223/js/kendo.all.min.js"> </script>
</head>
<body>
<div id="example">
<div id="grid"></div>
<script>
$(document).ready(function() {
$("#grid").kendoGrid({
dataSource: {
transport: {
read: {
url: "data.json?x=1",
cache:false,
type: 'GET',
dataType: 'json',
contentType: "application/json"
},
},
error: function(e) {
console.log(e.errors); // displays "Invalid query"
},
schema:{
// "data":"titi",
"groups":"groups",
"total": "total",
"model": {
"fields": [
{
"field": "m2"
},
{
"field": "m"
},
]
}
},
pageSize: 7
},
sortable: true,
scrollable: false,
pageable: true,
serverPaging: true,
serverAggregates: true,
serverFiltering: true,
serverGrouping: true,
serverSorting: true,
columns: [
{
"field": "m2",
"title": "Group odd / even"
},
{
"field": "m",
"title": "Month"
}
]
});
});
</script>
</div>
And data used here to simulate server response (data.json) :
{
"total":1,
"groups":
[ {
"aggregates": [],
"value": "rrr",
"hasSubgroups": false,
"field": "m2",
"items": [
{
"m2": "rrr",
"m": 1
},
{
"m2": "rrr",
"m": 1
},
{
"m2": "rrr",
"m": 1
},
{
"m2": "rrr",
"m": 1
},
{
"m2": "rrr",
"m": 1
}
],
"aggregates": {}
}]
}
I've been fighting with this for a couple of days now but I got it working eventually using https://docs.telerik.com/kendo-ui/knowledge-base/grid-format-of-the-response-with-server-grouping and https://docs.telerik.com/kendo-ui/framework/datasource/basic-usage#server-grouping for reference
Set the schema.groups property of the datasource to a function returning the response property that contains the groups
schema: {
groups: function (e) {
return e.Groups;
}
}
Return the following format from your server
Groups = [
{
field: "GroupField",
value: "Group1",
items: [],
aggregates: {},
hasSubgroup: false
},
{
field: "GroupField",
value: "Group2",
items: [],
aggregates: {},
hasSubgroup: false
}
]
<html>
<head>
<meta charset="utf-8">
<title>React component</title>
<script src="build/react.js"></script>
<script src="build/JSXTransformer.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<div id="myContent"></div>
<script type="text/jsx">
var ChatMessagesLists = React.createClass({
getInitialState: function() {
return {data: {chat_messages:[]}};
},
render: function(){
return
(
<div className="chatLstsWrapper">
<h1>Chat Messages</h1>
<ChatMessagesData data={this.state.data} />
</div>
)
},
componentDidMount: function(){
$.ajax({
url: 'jsontextfile.json',
dataType: 'json',
success: function(data){
this.setState({data: {chat_messages:[]}});
}
});
}
});
var ChatMessagesData = React.createClass({
render:function(){
console.log(this.props.data.chat_messages);
return(
<ul className="chatLsts">
{
this.props.data.chat_messages.map(function(chatmessages){
return <li>{chatmessages.from_account_id}</li>
})
}
</ul>
)
}
})
React.render(<ChatMessagesLists />,document.getElementById("myContent"));
</script>
</body>
</html>
JSON File
{
"message": "List of chat messages",
"data": {
"since_index": 1,
"before_index": 2,
"chat_messages": [
{
"text": "Load more",
"type": "text",
"key": "8ff134e7-e302-445b-903e-0038097c8a29"
},
{
"text": "my test data",
"type": "text",
"key": "3c7c3065-3701-4350-a64a-8a52f9fe1578"
},
{
"text": "My message",
"type": "text",
"key": "40f7c342-a019-44c3-ad2b-8b2ae018972b"
}
],
"max_results": 20
},
}
While loading the data getting below error. Can anyone please suggest where am i wrong
Error: Invariant Violation: ChatMessagesLists.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.
Hy,
Have you tried:
var ChatMessagesData = React.createClass({
render:function(){
var items = this.props.data.chat_messages.map(function(chatmessages){
return <li>{chatmessages.from_account_id}</li>;
})
return(
<ul className="chatLsts">
{
items
}
</ul>
);
});
PS: In Ajax callback, you not update the state:
success: function(data){
this.setState({data: {chat_messages: data}});
}
I have setup a datatables based on the following html
<script>
$('#tableListing').dataTable({
"bAutoWidth" : true,
"bProcessing" : true,
"bServerSide" : true,
"sAjaxSource" : '${actionUrl}',
"aoColumns":[
{ "sTitle": "ID", "mData": "id" },
{ "sTitle": "Username", "mData": "userName" },
{ "sTitle": "Password", "mData": "password" }
]
}
});
</script>
<table id="tableListing" class="display datatable">
<thead>
<tr>
<th><spring:message code="columnLabel.id" /></th>
<th><spring:message code="columnLabel.username" /></th>
<th><spring:message code="columnLabel.enabled" /></th>
</tr>
</thead>
</table>
And the JSON response from server is [1,"abiieez",true] and it works nicely. However I have changed the response from server into something like:
*Edit (updated JSON response) *
{
"iTotalDisplayRecords": 11,
"iTotalRecords": 11,
"aaData": "[{\"creationTime\":0,\"enabled\":true,\"id\":1,\"loginDuration\":0,\"online\":false,\"password\":\"asdasd\",\"userName\":\"abiieez\"},{\"creationTime\":0,\"enabled\":false,\"id\":105,\"loginDuration\":0,\"online\":false,\"password\":\"asdasd\",\"userName\":\"asd1212123\"},{\"creationTime\":0,\"enabled\":false,\"id\":106,\"loginDuration\":0,\"online\":false,\"password\":\"asdsdasd\",\"userName\":\"asdasd23123\"},{\"creationTime\":0,\"enabled\":false,\"id\":107,\"loginDuration\":0,\"online\":false,\"password\":\"asdsdasd\",\"userName\":\"asdasd23123\"},{\"creationTime\":0,\"enabled\":false,\"id\":108,\"loginDuration\":0,\"online\":false,\"password\":\"anak jalanan\",\"userName\":\"alibaba90\"},{\"creationTime\":1351338218227,\"enabled\":false,\"id\":109,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338263527,\"enabled\":false,\"id\":110,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338265078,\"enabled\":false,\"id\":111,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338266329,\"enabled\":false,\"id\":112,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338267247,\"enabled\":false,\"id\":113,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"}]",
"sEcho": "1"
}
The above json is not accepted by the datatables, I believe because the number of columns do not match.
Is there a way for me to retrieve this json object and pick which columns I would like to show on the tables on the client side ?
You can use mData property in aoColumns like this:
$(document).ready(function() {
$('#tableListing').dataTable({
"aoColumns":[
{ "sTitle": "user", "mData": "userName" },
{ "sTitle": "pass", "mData": "password" },
...
]
"bAutoWidth" : true,
"bProcessing" : true,
"bServerSide" : true,
"sAjaxSource" : '${actionUrl}'
});
});