More features with JSON

More features with JSON

fstfst Posts: 3Questions: 0Answers: 0
edited June 2009 in General
Hi

I have made some additions to 1.5.0b9 in order to parse some settings (table header/columns) from the JSON file.

This is my patched dataTables version
http://www.highdefinition.ch/js/jquery.dataTables-1.5.0b9.js

An example is here
https://www.highdefinition.ch/list/Image/LcdTelevision/Sony.html
https://www.highdefinition.ch/dataTables.json?type=list&cat1=Image&cat2=LcdTelevision&manufacturer=Sony

Allan, please consider implementing my changes to _fnInitialise.
Please consider implementing the remaining config options.
Some stuff had be moved around in _fnInitialise.

A loading/processing bar would also be a good thing...

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi fst,

    Thanks for posting your patched version - looks like quite an interesting idea. So if I am reading your code correctly, basically you can now return column information which is stored in the aoColumns array - right?

    There is actually a way of doing this with the current release of DataTables which is a little more flexible:

    [code]
    $.getJSON( sUrl, null, function( json ) {
    $('#example').dataTable( json );
    } );
    [/code]
    With this you can customise the JSON reply to exactly match how you want DataTables to initialise (features, starting parameters etc), so the control of the server-side isn't limited to just the columns. The developer would also have full control over the program to display whatever loading/progress message he/she wants to do.

    How does this sound?

    Allan
  • VolvoxeVolvoxe Posts: 4Questions: 0Answers: 0
    Hi Allan,
    Can u provide an example for getting table headers with json, in a way developer has full control on table header also?

    Say some times table has two columns, other times has three... and no change required to page code!!!

    BTW, txs a lot for datatables.
  • fstfst Posts: 3Questions: 0Answers: 0
    Allan, thanks for your response. Sorry for not replying myself earlier.

    Thanks for the tip of doing it the way you proposed but I think my way is cleaner and already implemented so I'll leave it as it is...
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    @fst: Sounds good. I'll keep this in mind for future versions :-)

    @Volvoxe: If you have a look at the demo for creating a DataTable from a JS array ( http://datatables.net/examples/data_sources/js_array.html ) you can see the full JSON object that is required for DataTables to be initialised. This should give you a good starting point. Anything else can be found in the documentation.

    Regards,
    Allan
  • VolvoxeVolvoxe Posts: 4Questions: 0Answers: 0
    Big thanks Allan!!!
    Works like a charm.
    I did combined two of your samples and got :

    [code]
    /*Create a table in demo DIV*/
    $('#demo').html( '' );
    /*Use json to populate created table*/
    $.getJSON( sUrl, null, function( json ) {
    $('#example').dataTable( json );
    } );

    [/code]
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    edited November 2011
    Hi. I know that the topic is old. But.
    From example I see
    [code]
    "aoColumns": [
    { "sTitle": "Engine" },
    { "sTitle": "Browser" },
    { "sTitle": "Platform" },
    { "sTitle": "Version", "sClass": "center" },
    {
    "sTitle": "Grade",
    "sClass": "center",
    "fnRender": function(obj) {
    var sReturn = obj.aData[ obj.iDataColumn ];
    if ( sReturn == "A" ) {
    sReturn = "A";
    }
    return sReturn;
    }
    }
    ]
    [/code]
    This is not a valid JSON. How can I generate it?
    Thanks.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    It isn't valid JSON because it has a function in it. If you really wanted to load functions from the server you would need to use 'eval()'.

    Allan
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    Thanks for response.
    Could you please provide an example?
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    In definition of aoColumns I want to call a function for each row in specified column. I can do it using following code
    [code]
    aoColumns": [
    { "sTitle": "Engine" },
    ...
    {"fnRender": callToMyFunction}
    ...
    ]
    [/code]
    But I have following problems:
    1. This definition is not a valid JSON object, so I can't generate it in a properly way.
    2. How can I send additional params to my function (callToMyFunction). I want to have something like this
    [code]
    function callToMyFunction( oObj, myObj) {
    //return String used in display using data from myObj
    }
    [/code]
    3. Is there o possibility to add a user defined property to a column in "aoColumns" definition?
    Any help will be appreciated.
    Thanks.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    There is no way to pass a function around as a JSON object/string since it is not valid JSON. As I mentioned you would need to use eval() to do that - although that is considered to be "evil". Can you load your function a different way - or do you need to define it through the JSON string.

    Sure I can write an example for you using eval - hit the support button at the top of the page :-)

    Allan
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    Thanks for response. I'll load my function a different way.
This discussion has been closed.