Server Side Processing - aColumn Array vs Output Array

Server Side Processing - aColumn Array vs Output Array

CircaCirca Posts: 4Questions: 0Answers: 0
edited July 2012 in General
Two simple questions that I can not find an answer to.

1. Why does the aColumn array have to match the length of Output array?

2. How can I change this.

Thanks!

Replies

  • korukyukorukyu Posts: 11Questions: 0Answers: 0
    I assume you mean aoColumns?

    1. Because it needs to know how to handle every column, therefore it needs definitions for each one.

    2. Use aoColumnDefs instead.

    http://datatables.net/usage/columns
  • CircaCirca Posts: 4Questions: 0Answers: 0
    edited July 2012
    No, i'm referring to the aColumns Array in PHP for Server Side Processing. I'll look into the aoColumnsDefs to see if it helps. But for now here's examples of what i'm trying to accomplish.

    $aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );

    Found in the example here:

    http://datatables.net/release-datatables/examples/server_side/server_side.html

    Here's a brief example of what i'm doing.
    [code]

    // THIS WORKS

    $aColumns = array('one', 'two', 'three');

    // Other stuff happens normally

    while ($aRow = mysql_fetch_array($rResult)) {

    $row = array();

    $row[] = "Custom data output";
    $row[] = "Custom data output";
    $row[] = "Custom data output";

    $output['aaData'][] = $row;

    }

    echo json_encode( $output );


    // THIS DOES NOT WORK

    $aColumns = array('one', 'two', 'three', 'four');

    // Other stuff happens normally

    while ($aRow = mysql_fetch_array($rResult)) {

    $row = array();

    $row[] = "Custom data output";
    $row[] = "Custom data output";
    $row[] = "Custom data output";

    $output['aaData'][] = $row;

    }

    echo json_encode( $output );

    [/code]
  • korukyukorukyu Posts: 11Questions: 0Answers: 0
    Oh. My bad. I have no idea, then. The documentation on this site seriously leaves something to be desired.
    :(
  • allanallan Posts: 63,540Questions: 1Answers: 10,476 Site admin
    @Circa:

    aColumns is simply an array of the column names from the database that you want to return, so it isn't so much that $aColumns needs to match the length of the output array, but rather the length of $aColumns that _defines_ the output array - i.e. for every item that you add or remove from the $aColumns array, that will be automatically reflected in the output array.

    So I guess the question is, what is it you are looking to do? If simply adding an extra column to the output array, then $row[] inside the while loop looks correct to me.

    @korukyu:

    > The documentation on this site seriously leaves something to be desired.

    Please tell me how I can improve it! :-). I'm here and listening, and trying to build the best OSS software product I possibly can, so feedback on why you find something difficult to do is important to me, and ultimately to the community, so I can make the required changes.

    Allan
  • korukyukorukyu Posts: 11Questions: 0Answers: 0
    @allan This is a great product, don't get me wrong. DataTables is really handy.

    But whenever I want to do something, I never know where to start. Like if I want to change the background color of a row after a refresh if a value in it changed. I guess I'd do that with fnRowCallback? But I don't even know if I can call it outside of the initialization, because apparently most things that are called at init can't be edited later?

    The majority of the variables and functions covered in the usage section are initialized variables. And then things like table default values aren't even in the full reference, so I can't figure out how to use them. Everything feels a little disjointed :(
  • CircaCirca Posts: 4Questions: 0Answers: 0
    edited July 2012
    @allan

    Thank you! My issue is that I will almost always have more items in the aColumns array then the output array. I'd like to avoid necessary configuration if possible. Is there any way to accomplish this without adding the empty/hidden columns like i did below?

    [code]

    $aColumns = array('id', 'status', 'description', 'title');

    // Other stuff happens normally

    while ($aRow = mysql_fetch_array($rResult)) {

    $row = array();

    $row[] = "INSERT DATA";
    $row[] = "INSERT DATA";
    $row[] = "INSERT DATA";
    $row[] = ""; // empty unused output column

    $output['aaData'][] = $row;

    }

    $('#listProducts').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "process.php",
    "aoColumnDefs": [
    { "sClass": "center", "aTargets": [ 0 ] },
    { "sClass": "center", "aTargets": [ 1 ] },
    { "sClass": "center", "aTargets": [ 2 ] },
    { "bVisible": false, "aTargets": [ 3 ] } // empty unused output column
    ]
    });

    [/code]
  • allanallan Posts: 63,540Questions: 1Answers: 10,476 Site admin
    @Circa - I think I understand what you are looking for based on your DataTables code - and in fact your code looks okay for that. In my server-processing script I have a loop over $aColumns on the output, but in your pseudo code above you don't and that's absolutely fine.

    [code]
    $row[] = $aRow['id'];
    $row[] = $aRow['status'];
    $row[] = $aRow['description'];
    $row[] = $aRow['title'];
    $row[] = '';
    [/code]

    is perfectly valid.

    However, I would suggest you might want to consider using objects in your server-side return, rather than arrays, if you are going to start "missing parts out" :-). See this post: http://datatables.net/blog/Extended_data_source_options_with_DataTables

    @korukyu

    > Like if I want to change the background color of a row after a refresh if a value in it changed

    Short of an example, I'm not entirely sure how I would document that. Where would you expect to find it in the documentation?

    There are also a lot of variables just in that line:

    1. Are you refreshing the whole table with fnReloadAjax?
    2. Are you using server-side processing?
    3. Are you using fnUpdate to update the value?

    The answer is going to depend upon how you are actually using DataTables - which is part of the reason why to documentation can feel disjointed - I've documented the API and the initialisation options, and you will need to put the parts that you want together. My examples try to bring things together, but to provide examples of everything would be impossible - heh - in just your one liner, there are at least three questions that I thought of off the top of my head!

    Allan
  • allanallan Posts: 63,540Questions: 1Answers: 10,476 Site admin
    @korukyu - I should also say that I'm absolutely not saying my documentation is perfect! heh - far from it :-). I completely acknowledge that there are areas where it leaves a lot to be desired and I do want to spend a good bit of time redesigning it to be more accessible. I'm pleased with how the Editor documentation has worked for example ( http://editor.datatables.net ), with its break down of init options, events and API + tutorials. I want to move the DataTables documentation to a model a bit more like that.

    Allan
  • CircaCirca Posts: 4Questions: 0Answers: 0
    edited July 2012
    @allan

    Thanks again! I'm really impressed with datatables and plan to start using it on many projects if I can overcome a few of the obstacles within it.

    This is a better example of what i'm trying to accomplish. As you see there are multiple aColumns combined into 1 row of the output. I want to avoid adding 2 additional empty rows to the output array if possible. (there will be far more than 2 in more detailed pages) Is there any way to accomplish this?
    [code]

    $aColumns = array('id', 'firstname', 'lastname', 'title', 'date');

    // Other stuff happens normally

    while ($aRow = mysql_fetch_array($rResult)) {

    $row = array();

    $row[] = "".$aRow['firstname']." ".$aRow['lastname']."";
    $row[] = $aRow['title'];
    $row[] = date("m/d/Y", $aRow['date']);

    $output['aaData'][] = $row;

    }

    $('#listProducts').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "process.php",
    "aoColumnDefs": [
    { "sClass": "center", "aTargets": [ 0 ] },
    { "sClass": "center", "aTargets": [ 1 ] },
    { "sClass": "center", "aTargets": [ 2 ] }
    ]
    });

    [/code]
This discussion has been closed.