My DataTable is loaded almost entirely from the server side and I would like to keep as much processing as possible on the server to reduce the workload on the client.
By formatting I mean changing color, size, font, font-weight, adding icons and html tags... For example when a row or a few cells need to be highlighted or displayed in a specific way.
I'm thinking about using the render parameter, but I didn't find a way to set it in JSON:
render: $.fn.dataTable.render.number( ',', '.', 0, '$' ) }
An example would also be the following snippet, but it does not provide enough separation between the client and server:
{
"doc": "<strong>546546545<strong>",
"nothing": 0.0,
"order": "<div class="shipped">98745</div>"
}
While I was able to find resources on how to fetch data from the server, I can't find information on how to pass formatting data as part of the json.
Is there a way to paste formatting options in my current DataTable format to make it also optimal? What alterations need to be done?
http://jsfiddle.net/ebRXw/1004/
JSON:
{
"columns": [
{
"data": "doc",
"title": "Doc."
},
{
"data": "order",
"title": "Order no."
},
{
"data": "nothing",
"title": "Nothing"
}
],
"data": [
{
"doc": "564251422",
"nothing": 0.0,
"order": "56421"
},
{
"doc": "546546545",
"nothing": 0.0,
"order": "98745"
}
]
}
JS:
this.table = $('#example').DataTable({
data: json.rows,
columns: json.columns,
select: true,
responsive: true,
rowReorder: true,
colReorder: true,
scrollY: 680,
deferRender: true,
scroller: true
});
HTML:
<table id="example" cellspacing="0" width="100%" />
Related
In Azure Data Factory, I'm attempting to add params to the body of a copy task (connected to a REST API post request as the source). I'm wanting to use dynamic content to do so, but I'm struggling trying to find the real solution for the proper nomenclature. Here's what I have so far.
copy task
dynamic content
{
"datatable":
{
"start":0,
"length": 10000,
"filters": [
{
"name": "Arrival Dates",
"start": "pipeline().parameters.pDate1",
"end": "pipeline().parameters.pDate2"
}
],
"sort": [
{
"name": "start_date",
"order": "ASC"
}
]
}
}
You'll notice that I've added params for dates. Is this the correct nomenclature for trying to add dynamic content? The autocorrect tried to add the # sign in the beginning of the code block, which will cause the entire thing to error out. I've tried adding it before each parameter, but that isn't actually reading the dynamic values either.
This is not correct. You need to use concat to concatenate the different variables. Something like this :
#concat('{ "datatable": { "start":0, "length": 10000, "filters": [ { "name": "Arrival Dates", "start": "',pipeline().parameters.pDate1,'", "end": "',pipeline().parameters.pDate2,'" } ], "sort": [ { "name": "start_date", "order": "ASC" } ] } }')
This is also documented in the SO question.
I’m using amcharts stock chart. When using the following code, my chart loads fine and everything works:
var chart = AmCharts.makeChart("chartdiv", {
"type": "stock",
"theme": "light",
"dataSets": [{
"title": "Values",
"color": "#b0de09",
"fieldMappings": [{
"fromField": "value",
"toField": "value"
}, {
"fromField": "average",
"toField": "average"
}],
"dataProvider": #Html.Raw(ViewBag.ChartData),
"categoryField": "date"
}],
...
And my controller:
public class ChartController : Controller
{
public IActionResult Index()
{
ViewBag.ChartData = JsonConvert.SerializeObject(GetChartData ());
return View();
}
}
However, when I try to add stock events by adding the following code, two things happen: 1) the chart no longer displays, and 2) the GetChartEvents action in the controller is never called, so obviously never returns any data.
var chart = AmCharts.makeChart("chartdiv", {
"type": "stock",
"theme": "light",
"dataSets": [{
"title": "Values",
"color": "#b0de09",
"fieldMappings": [{
"fromField": "value",
"toField": "value"
}, {
"fromField": "average",
"toField": "average"
}],
"dataProvider": #Html.Raw(ViewBag.ChartData),
"categoryField": "date",
"eventDataLoader": {
"url": #Url.Action("GetChartEvents", "ChartController"),
"format": "json",
"showCurtain": true,
"showErrors": true,
"async": true
}
}],
...
and in the controller
public class ChartController : Controller
{
…
[HttpGet]
public IActionResult GetChartEvents()
{
ChartEvent[] events = new ChartEvent[3];
events[0] = new ChartEvent(new DateTime(2007, 12, 17));
events[1] = new ChartEvent(new DateTime(2008, 10, 27));
events[2] = new ChartEvent(new DateTime(2009, 07, 06));
return Json(events);
}
}
ChartEvent is my own class, but because GetChartEvents is never called, I don’t think the issue is how the returned data is formatted.
In the view, if I replace
"url": #Url.Action("GetChartEvents", "ChartController"),
With
"url": "/Chart/GetChartEvents",
GetChartEvents is still never called, but the chart does display itself. I even tried placing the events in the ViewBag, but that doesn’t work either
"dataProvider": #Html.Raw(ViewBag.ChartData),
"stockEvents": #Html.Raw(ViewBag.ChartEvents)
As a final note (and sanity check!), if I output to the page the contents of #Html.Raw(ViewBag.ChartEvents), I get the following that appears fine:
[{"date":"Date(2007,12,17)","type":"arrowUp","backgroundColor":"#00CC00","graph":"g1","description":" "},
{"date":"Date(2008,10,27)","type":"arrowUp","backgroundColor":"#00CC00","graph":"g1","description":" "},
{"date":"Date(2009,07,06)","type":"arrowUp","backgroundColor":"#00CC00","graph":"g1","description":" "}]
Any help or insight would be much appreciated.
Following their guide here, there are few things that might cause problem.
First, quotes:
"url": '#Url.Action("GetChartEvents", "ChartController")',
Notice single quotes around the URL. Without them your js is just invalid, and that's why the graph does not display.
Second, they use "dataLoader" and not "eventDataLoader" property.
Third, make sure to include dataloader plugin:
<script src="amcharts/plugins/dataloader/dataloader.min.js" type="text/javascript"></script>
I am currently developping a map, using Openlayers2.
This map has to show several information
I already managed ton integrate images and canvas using
OpenLayers.Layer.Vector -> for basic images and vectors
OpenLayers.StyleMap and OpenLayers.Style -> for canvas and text
I need now to insert several boxes, integrating text.
The boxes have to adapt themself to the texts, each having different length.
I would like now to insert customized HTML tags, like a div, instead of a canvas (difficult to adapt and quite heavy to handle)
The information are integrated from a json:
Example of Json
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
589200.0,
2402900.0
]
},
"properties": {
"activation": "true",
"info": "one for example short text"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
603700.0,
2408300.0
]
},
"properties": {
"activation": "true",
"info": My other very long text \n with lines \r different messages and so on\n and it has to fit in.."
}
}]
}
It is a legacy map, and we are stucked with Openlayer 2, so the solution wouldn't be with the version 3.
I am desesperately searching on the net a solution that would suit, without success, but I am loosing a precious amount of time with it.
The information are loaded this way :
new OpenLayers.Layer.Vector("Vector", {
layername: "myText",
resolutions: [100, 50, 25, 10],
styleMap: textStyleMap,
isBaseLayer: false,
visibility: false,
strategies: [new OpenLayers.Strategy.Fixed({
preload: true
})],
protocol: new OpenLayers.Protocol.HTTP({
url: "myText.json",
format: new OpenLayers.Format.GeoJSON()
})
})
What kind of styleMap could I use?
Has anybody a solution to the problem?
Thank you!
I finally skiped the problem using dragable popups instead.
It is an update you can find here:
http://aamirafridi.com/openlayers/openlayers-draggable-popups.
I've been working with datatables and I'm able to load the datatable using getJson with strongly typed classes etc and it works just great. Until I hit one snag.
There are times I want to populate a datatable with data that "I don't know about" but I always know that it will be one row of data - it is simply a json string with dynamic content.
Now with datatables you can simply populate the table with aaData and aaCol by assigning a json string to it but my json string contains a column and data IE:
First_name:bob and so on.
A column - on Datatables would be populated with sTitle:Column1 etc and assigned to aaCol.
Does anyone know of a plug in that parses a json string into aaCol and aaData for use with datatables?
I believe you can solve your problem using this approach:
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "scripts/objects.php",
"columns": [
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "start_date" },
{ "data": "salary" }
]
} );
} );
In the example above the dataTables uses a serverSide processing, the ajax return a object like this:
{
"draw": 1,
"recordsTotal": 57,
"recordsFiltered": 57,
"data": [
{
"first_name": "Airi",
"last_name": "Satou",
"position": "Accountant",
"office": "Tokyo",
"start_date": "28th Nov 08",
"salary": "$162,700"
}, ...
You can also set the column name using the "name" property inside the specification of each column.
You can the full example in the following link. If you need more assistance I can make a code on my own later today =)
You can check this JsFiddle to understand how to set the columns names
EDIT 2014-05-01: I tried fromJSON first (as suggested below), but that only parsed the first line. I found out that there were commas missing between the brackets of each JSON line so I changed that in TextEdit and saved the file. I also added [ at the beginning of the file and ] at the end and then it worked with JSON. Now the next step: from a list (with embedded lists) to a dataframe (or csv).
I get a data package from edX every now and then on the courses we are evaluating. Some of these are just plain .csv files which are quite easy to handle, others are more difficult for me (not having a CS or programming background).
I have 2 files I want to open and parse into csv files for analysis in R. I have tried many many json2csv tools out there, but to no avail. I also tried the simple methods described here to turn json into csv.
The data is confidential, so I cannot share the entire data set, but will share the first two lines of the file, maybe that helps. The problem is that nowhere I find anything about .mongo files, which to me seems quite strange, do they even exist? Or is this just a JSON file that may be corrupted (which could explain the errors)?
Any suggestions are welcome.
The first 2 lines in one of the .mongo files:
{
"_id": {
"$oid": "52d1e62c350e7a3156000009"
},
"votes": {
"up": [
],
"down": [
],
"up_count": 0,
"down_count": 0,
"count": 0,
"point": 0
},
"visible": true,
"abuse_flaggers": [
],
"historical_abuse_flaggers": [
],
"parent_ids": [
],
"at_position_list": [
],
"body": "the delft university accredited course with the scholarship (fundamentals of water treatment) is supposed to start in about a month's time. But have the scholarship list been published? Any tentative date??",
"course_id": "DelftX/CTB3365x/2013_Fall",
"_type": "Comment",
"endorsed": false,
"anonymous": false,
"anonymous_to_peers": false,
"author_id": "269835",
"comment_thread_id": {
"$oid": "52cd40c5ab40cf347e00008d"
},
"author_username": "tachak59",
"sk": "52d1e62c350e7a3156000009",
"updated_at": {
"$date": 1389487660636
},
"created_at": {
"$date": 1389487660636
}
}{
"_id": {
"$oid": "52d0a66bcb3eee318d000012"
},
"votes": {
"up": [
],
"down": [
],
"up_count": 0,
"down_count": 0,
"count": 0,
"point": 0
},
"visible": true,
"abuse_flaggers": [
],
"historical_abuse_flaggers": [
],
"parent_ids": [
{
"$oid": "52c63278100c07c0d1000028"
}
],
"at_position_list": [
],
"body": "I got it. Thank you!",
"course_id": "DelftX/CTB3365x/2013_Fall",
"_type": "Comment",
"endorsed": false,
"anonymous": false,
"anonymous_to_peers": false,
"parent_id": {
"$oid": "52c63278100c07c0d1000028"
},
"author_id": "2655027",
"comment_thread_id": {
"$oid": "52c4f303b03c4aba51000013"
},
"author_username": "dmoronta",
"sk": "52c63278100c07c0d1000028-52d0a66bcb3eee318d000012",
"updated_at": {
"$date": 1389405803386
},
"created_at": {
"$date": 1389405803386
}
}{
"_id": {
"$oid": "52ceea0cada002b72c000059"
},
"votes": {
"up": [
],
"down": [
],
"up_count": 0,
"down_count": 0,
"count": 0,
"point": 0
},
"visible": true,
"abuse_flaggers": [
],
"historical_abuse_flaggers": [
],
"parent_ids": [
{
"$oid": "5287e8d5906c42f5aa000013"
}
],
"at_position_list": [
],
"body": "if u please send by mail \n",
"course_id": "DelftX/CTB3365x/2013_Fall",
"_type": "Comment",
"endorsed": false,
"anonymous": false,
"anonymous_to_peers": false,
"parent_id": {
"$oid": "5287e8d5906c42f5aa000013"
},
"author_id": "2276302",
"comment_thread_id": {
"$oid": "528674d784179607d0000011"
},
"author_username": "totah1993",
"sk": "5287e8d5906c42f5aa000013-52ceea0cada002b72c000059",
"updated_at": {
"$date": 1389292044203
},
"created_at": {
"$date": 1389292044203
}
}
R doesn't have "native" support for these files but there is a JSON parser with the rjson package. So I might load my .mongo file with:
myfile <- "path/to/myfile.mongo"
myJSON <- readLines(myfile)
myNiceData <- fromJSON(myJSON)
Since RJson converts into a data structure that fits the object being read, you'll have to do some additional snooping but once you have an R data type you shouldn't have any trouble working with it from there.
Another package to consider when parsing JSON data is jsonlite. It will make data frames for you so you can write them to a csv format with write.table or some other applicable method for writing objects.
NOTE: if it is easier to connect to the MongoDB and get the data from a request, then RMongo may be a good bet. The R-Bloggers also made a post about using RMongo that has a nice little walkthrough.
I used RJSON as suggested by #theWanderer and with the help of a colleague wrote the following code to parse the data into columns, choosing the specific columns that are needed, and checking each of the instances if they return the right variables.
Entire workflow:
Checked some of the data in jsonlint - corrected the errors → },{ instead of }{ between each line and [ and ] at the beginning and end of the file
Made a smaller file to play with, containing about 11 JSON lines
Used the code below to parse the datafile - however, checking the different listItems first if they are not lists themselves (that gives problems) // as you will see, I also removed things like \n because that gave errors and added an empty value for parent_id if there is none in the data (otherwise it would mix up the data)
The code to import the .mongo file into R and then parse it into CSV:
library(rjson)
###### set working directory to write out the data file
setwd("/your/favourite/dir/json to csv/")
#never ever convert strings to factors
options(stringsAsFactors = FALSE)
#import the .mongo file to R
temp.data = fromJSON(file="temp.mongo", method="C", unexpected.escape="error")
file.remove("temp.csv") ## removes the old datafile if there is one
## (so the data is not appended to the file,
## but a new file is created)
listItem = temp.data[[1]] ## prepare the listItem the first time
for (listItem in temp.data){
parent_id = ""
if (length(listItem$parent_id)>0){
parent_id = listItem$parent_id
}
write.table(t(c(
listItem$votes$up_count, listItem$visible, parent_id,
gsub("\n", "", listItem$body), listItem$course_id, unlist(listItem["_type"]),
listItem$endorsed, listItem$anonymous, listItem$author_id,
unlist(listItem$comment_thread_id), listItem$author_username,
as.POSIXct(unlist(listItem$created_at)/1000, origin="1970-01-01"))), # end t(), c()
file="temp.csv", sep="\t", append=TRUE, row.names=FALSE, col.names=FALSE)
}