Using other included properties when using Custom data source property via AJAX
Using other included properties when using Custom data source property via AJAX
jLinux
Posts: 981Questions: 73Answers: 75
Im using an AJAX source with a custom data source property, heres the JSON:
{
"partition":{
"partition_id":"1",
"name":"Server Asset Management",
"partition_table":"server_asset_management",
"status":"0",
"description":"Managing servers in the application Network",
"created":"1441293871",
"primary_column":"server_hostname",
"groups":[
""
],
"fields":{
"server_hostname":{
"field_id":"1",
"partition_id":"1",
"name":"Server Hostname",
"column_name":"server_hostname",
"description":"FQDN Of server",
"visible":"1",
"placeholder":"____.application.ad",
"primary":"1",
"order":"0",
"required":"1",
"unique":"1",
"regex":null,
"min":"0",
"max":"0",
"type":"string",
"options":null,
"default":null,
"dependent":null,
"partition_name":"Server Asset Management"
},
"operating_system":{
"field_id":"2",
"partition_id":"1",
"name":"Operating System",
"column_name":"operating_system",
"description":"OS On server",
"visible":"1",
"placeholder":null,
"primary":"0",
"order":"1",
"required":"0",
"unique":"0",
"regex":null,
"min":"0",
"max":"0",
"type":"select",
"options":[
"RHEL 4",
"RHEL 5",
"RHEL 6",
"CentOS 4",
"CentOS 5",
"CentOS 6",
"Other"
],
"default":"RHEL 4",
"dependent":null,
"partition_name":"Server Asset Management"
}
},
"visible_fields":{
"server_hostname":{
"field_id":"1",
"partition_id":"1",
"name":"Server Hostname",
"column_name":"server_hostname",
"description":"FQDN Of server",
"visible":"1",
"placeholder":"____.application.ad",
"primary":"1",
"order":"0",
"required":"1",
"unique":"1",
"regex":null,
"min":"0",
"max":"0",
"type":"string",
"options":null,
"default":null,
"dependent":null,
"partition_name":"Server Asset Management"
},
"operating_system":{
"field_id":"2",
"partition_id":"1",
"name":"Operating System",
"column_name":"operating_system",
"description":"OS On server",
"visible":"1",
"placeholder":null,
"primary":"0",
"order":"1",
"required":"0",
"unique":"0",
"regex":null,
"min":"0",
"max":"0",
"type":"select",
"options":[
"RHEL 4",
"RHEL 5",
"RHEL 6",
"CentOS 4",
"CentOS 5",
"CentOS 6",
"Other"
],
"default":"RHEL 4",
"dependent":null,
"partition_name":"Server Asset Management"
}
}
},
"assets":[
{
"asset_id":"1",
"status":"locked",
"created":"1441294069",
"creator":"1",
"modified":"1444503758",
"modifier":"1",
"server_hostname":"lqdasseat13.application.ad",
"operating_system":"CentOS 5",
"applications":"Apache,Nagios,Chef,Other",
"monitoring":"1",
"notes":"tester",
"comments":"Something Important"
},
{
"asset_id":"2",
"status":null,
"created":"1441942931",
"creator":"1",
"modified":"1444441599",
"modifier":"1",
"server_hostname":"ldsassaet24.application.ad",
"operating_system":"RHEL 6",
"applications":"Jenkins,Tomcat,Nagios,Chef,RHDS",
"monitoring":"",
"notes":"tester",
"comments":""
}
]
}
And heres the HTML/DataTables:
<script language="JavaScript">
$( document ).ready(function(){
$('#example').DataTable( {
"ajax": {
"url": "/REST/partition/asset_list/partition_id/1/format/json",
"dataSrc": "assets"
},
"columns": [
{ "data": "asset_id" },
{ "data": "server_hostname" },
{ "data": "operating_system" },
{ "data": "applications" },
{ "data": "monitoring" }
]
} );
});
</script>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Asset ID</th>
<th>Server Hostname</th>
<th>OS</th>
<th>Apps</th>
<th>Monitoring</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Asset ID</th>
<th>Server Hostname</th>
<th>OS</th>
<th>Apps</th>
<th>Monitoring</th>
</tr>
</tfoot>
</table>
As you can see, im using the assets
property, I was wondering, if there was a way to use whats in the partition
property, without having to re-query the source on my own. I need to use the contents inside createdRow
for example.
Thanks!
This discussion has been closed.
Replies
If thats not possible, then I have a different question. But first, heres how the new js is laid out (Notice it has an ajax call, then loads datatable with a different ajax source, the goal is to use the same source)
What I could do, is have all of the data pulled from the first
$.ajax
query, and then pull the data source out of that and pass it to DataTables as JavaScript sourced data, but my main reason for wanting to use the AJAX data source is the ability to use thedeferRender
feature... is it possible to use that if the source is a JSON string handed to it? as opposed to an AJAX sourceSummary of this question: If the answer to the question in post #1 is no, then it possible to use
deferRender
with JavaScript sourced dataHeres an example of what I mean in the above post
It will display the table just fine, but I dont have enough data to tell if the
deferRender
is actually doing anythingdeferRender
says that it should work with ajax sourced data, so I think your last post is headed in the right direction. But I don't think you need it.At line 16 you can insert some code that parses the 'partition' data and sets variables that you use in
createdRow
All you have to do is account for variable scope.@thomD I cant tel if the
deferRender
is working or not, but the loading of data seems to "work", in that the table loads. However I found that theres much more benefit to using the actualajax
source, especially theajax.reload()
functionality.So I guess its back to the original question... If I specify a specific property from the AJAX source to use, is there any way to have access to the other properties?
Did you try something like this
your "data" object should be fully accessible as an javascript object.
@ThomD, im not using the
data
anymore, thats for local JS/JSON sourced data, I was using it, until I realized that theajax.reload()
wont work with it. (I tried it because thedata.assets
array was loaded via ajax, but that doesn't mean that DataTables sees it as an AJAX source, which should have been obvious to me)Heres the current version I have (I tried to shorten it up by taking out any irrelevant code, full version linked below), Look through the comments for
ThomD
for any relevant notes to this thread:And incase you need/want it, heres the FULL version of the script
I think I understand what you are saying. Because you are using the DT "ajax" function (could there be a more confusing name?), DT is diving deep into your JSON data and ignoring the structure above it.
You wrote "I'm not using the data() option anymore", but I see it on line 32. I like how you've made the DT config dynamic, based on the returned data set.
I don't see a way around what you are currently doing since you want to use DT's native "ajax" function.
On line 32? Of the most recent post? Thats the
$('#data-table').DataTable()
line..in the most recent post, theres no
data
, its nowajax
Yeah, the DataTable is basically a reflection of the MySQL table, the partition contains the MySQL table information, and the assets are the rows in the MySQL table.
And I kinda have to use the AJAX source, I want to be able to reload the table from the data source, so if someone else makes an update, then the updates will be reflected in the current table.
If you don't see a way around it, then thats fine, I don't either, was just hoping it was possible. I figure that DataTable must be calling the entire page when it makes the AJAX call, and then just grabs the dataSrc key, and since I use the rest of the content inside the page, it would be useful to use the rest of the AJAX result, as opposed to making another AJAX call for the exact same page.
sorry, line 38: columns. You are passing in an object that defines the data elements
I'm still not sure how using the DT "ajax" data definition gets you much. I suppose that is is a bit easier to have
ajax.reload
handle the new data, but it shouldn't be that much work to use $.getJSON andclear
the data, load a new set and addrows.. OTOH, I haven't done it so it may be harder than I think it is.In your current setup, what does
ajax.json
show during rowCallback?@ThomD, Line 38 isn't specifying the data source.. its defining the columns.. totally different. Im not talking about how the table columns are defined, im talking about how the source of the data is pulled.
And using
ajax.reload()
is a lot easier, not a bit easier. When the table is reloaded viaajax.reload()
, it pulls all of the new data, as well as runs thecreatedRow
again for each row. And if you look at the new version of my JS, theres a lot of code that gets executed in there. The entire row is parsed and each cell is turned into an instance of the jQuery plugin x-editable. This basically accomplishes the same thing as DataTables Editable, except its fully open source, since this is going to be an open source project. (Id totally use DT Editable if the project wasn't OS :( )The end result is a nice combo of x-editable and DataTables, fully ajaxed -> http://d.pr/i/16Wwy
Man, I love DataTables...
I switched from DOM sourced to AJAX sourced.
DOM: http://d.pr/i/1cmCS
AJAX (w/ deferRender): http://d.pr/i/1eCJe
Granted, I did a load test with 5k rows, but thats still awesome! hah
Nice performance improvement. I hope you don't mind if I ask some questions.
In http://d.pr/i/16Wwy line 136, it looks like you are setting the "id" attribute to the column name. That would be the same "id" on all td in the column. "id" should be unique in the DOM. You could add the asset_id to make it unique.
As far as the data load method, I would be seriously disappointed with DT if using
row.add()
didn't invokecreatedRow
.Let me make your life more complicated :) by suggesting that rather redrawing the entire data set to pickup any changes, that you query only for db entries that have been modified since the current page was reloaded. Then you wouldn't be pulling as much data.
Line 88, parseInt. I recently learned that it is strongly recommended to always add the second parameter to define what base the data is in. If the data should start with a zero, some browsers will type it as octal,
It is an unlikely occurrence, but you never know.
did you get a change to see what
ajax.json
shows duringcreatedRow
call back if you use the top level URL for your ajax call? I wonder if that would expose the higher level data and then useajax.dataSrc
to define the data objects.And thanks for guilting me into learning markdown. :)
@ThomD That link you sent is a screenshot of the table, not of the code.. what was in line 136?
I would love to do that instead.. how can I update only selected rows? I asked that here, and allan said it wasnt possible... Maybe he mis-understood me? Or you have a way of doing it?
No, thats a good idea, ill try it out, thank you!
Dude...
ajax.json()
works.... (All data) You just made my fu#kin day!!! Id hug you if you were here right now. Are you within driving range of Phx, AZ? (kidding)P.S. @ThomD - Thanks man, This saved me SOO much work!! lol.
And im glad I guilted you into using the syntax in your posts, it makes reading them soo much easier. When people post code with no syntax highlighting, it just scrambles it all up, and I dont even try to read it. And when you use the proper enclosures for things like
createdRow
, its so much easier to read, IMO@ThomD - Wanted to get your opinion on something..
You can see in HERE, that when I create the row, inside
createdRow
, I parse the type of "field" it is (from the JSON source), then replace the content with ana
so I can initiate the jQuery x-editable plugin, the result is a table filled with the x-editable links (screenshot here)However.... I just found the
columns.render
option, which it says...Heres the DataTables Example page that lead me to it
Do you think I should use
columns.render
to replace the data in each cell with a link? or is it better just how it is?..EDIT: I don't think
ajax.json
is accessible insidecolumns.render
, so that kinda voids the idea, since its requiredWhat is wrong with my copy/paste skills this week?
IN this http://code.linuxdigest.org/view/a8821666 link, this code,
looks like it is setting the DOM element 'id' to a value that is column, but not td specific.
about that single line data refresh, if Allan says it can't be done with the native tools, I will not argue. I'm at home now, so I can't see my code at work, but I think I'm relying on a $getJSON call to get one item and then refresh the data for that one row.
WRT to
columns.render
vscreatedRow
- I'm slowly coming to understand the DT often gives a couple of ways to get to the same goal. If it ain't broke, don't fix it.From a maintenance perspective, I like
createdRow
because it provides one stop shopping for a lot of logic. You could end up with rendering options scattered everywhere.@ThomD
If you look at the x-editable documentation, it requires the ID, but I just took a look at it, and it makes it optional to use
id
ordata-name
, so ill probably switch it todata-name
, since theid
field should always be unique.I figured that even though using the ID was "wrong", (since they wont be unique in this case), id leave it be, since it was working. But im going to try changing it.
Thanks!
EDIT: Updated the code from
id
todata-name
, and it works fine :) I was just too lazy to do it before I guess, lol..Oh, in regard to this statement:
Can you show me the code? :-D
@ThomD, so I just uploaded my code to a server and executed it, and the
ajax.reload
actually does take longer than I thought, even if its just for 5 or 10 rows.I sent you a PM with the login details for my app.
When I had it locally, it was pretty fast, and I guess since the ajax source is from the same location as the app, I figured it would still be decently fast, but it seems to be slow.
I added 5k assets to the table just to get an idea of how it would work under a heavy load.. so imagine if 100+ rows were showing, and
ajax.reload
executed... Probably crash the browser :(So if you can let me check your code out, that would be awesome! hah
Found this, looks interesting, might help a bit. id still like to see your code tho :)
Edit: Hm, I think that link is only for
serverSide
... Not justajax
:(When I edit an entry, I get back a fresh dataset for that item. I refresh the datatables content with this code in my ajax success call.
Every row gets an id with this
It may not be obvious, but that # is the jQuery selector syntax for an element with that 'id' attribute.
Hm, I see... Do you have a demo I can see of this somewhere?
When you edit that item, how do you get the
returnData.d
?Do you know if its possible to re-poll the content from the datatables ajax source, without reloading the whole table? like if I wanted to do something like grab the cached content that DT uses, then get the latest content, then diff them via jQUery and reload only the rows that need to be updated (and find a way to delete/add the deleted/added rows)
Since im using
deferRender
, I guess I wouldnt have to parse all 5k rows... just whatevers showing.. (for some reason, that seems like more work.. lol)Sorry, demo is "safely" locked up behind our firewalls. I lied (OK, I simplified), when I save a change, I get back only an ACK. In the "save" ajax call, I use the success event to trigger a new ajax call that returns only that item that had been updated. I have lots of server side calculated fields, so that works out for me.
This is the more complete version. This is within the definition of my dtEditor setup.
As far as polling for changes, you could use setInterval() to poll. Let's say you go with a 5 minute interval, you would need to be able ot query the database for items created/modified within the last 5 minutes. New items would be a straight up
addRow
, modified items would use the same logic I'm showing around line 25 in my last post.If you get fancy, then you would want to give the user a way to not auto refresh and have a manual refresh button.
Very interesting.. thank you, Few questions if you don't mind :-D
ajax
value, what does that need to return? The JSON for the rows?method, url, submitData, dtSuccessCallback, dtFailureCallback
parameters? I didn't see anything in the references documentation, but I may have missed itsubmitData.data.version
? I see you use it for the headerIf-Match
, ive never used that beforeX-HTTP-Method
something specific to Microsoft? I googled it and everything I see is in the MSDN. I know I can just look for it via PHP thoughmyAjaxURL = submitData.data.uri
and usemyAjaxURL
as the url value, then on line 17, you rundataRefreshURI = myAjaxURL
and usedataRefreshURI
as the new url value.. Just a little confused on that.As far as polling for changes, you could use setInterval() to poll
, thesetInterval
shouldnt be inside of the anonymous function in theajax
value, right? (this might be obvious, just want to be sure)I can create a new RESTful resource that I can pass the current timestamp to, and it will return any "assets" that have been updated/added since that timestamp, im not positive how I should take care of the deleted ones though.. im sure ill figure it out. But is this basically how youd do it?
Definitely planned on doing that already :)
1 - That ajax in the first line is the Editor's ajax option, so it is a little different.
http://editor.datatables.net/reference/option/ajax
It requires that either the dtSuccessCallback or dtFailureCallback functions be called. Those are callbacks to Editor to let it know we are done. Nothing else needs to be returned because the anonymous function makes all the changes and then just signals to Editor the resulting status.
2 - All of that is in the Editor documentation (link above).
3 - Versioning is part of the SharePoint back end. It is how my back end deals with collisions.
4 - More SharePoint magic.
5 - Oddly, yes. I use the same URL for the POST and the GET. Just the way it is for my environment. Note that there is no "type" parameter on the second $ajax.
6 - I'd put the setInterval() in my
initComplete
. that way it only gets started if you've been able to get that initial data load.