How can I sort results by primary ID by default?

How can I sort results by primary ID by default?

audioperceptionaudioperception Posts: 3Questions: 3Answers: 0

Ajax requested data from Mysql working ok but, What I need is for the new record post to be at the top of the table.

Sorting by my date/time column is not accurate.
Alternatively I would like to order by the database ID column but the problem there is that when using editor, It won't display the ID column from the DB. id is my primary key.

Again the goal is to have new posts to be at the top of the displayed table.

javascript------

(function($){

$(document).ready(function() {

    var editorC = new $.fn.dataTable.Editor( {
        "processing": true,
        "serverSide": true,
        "ajax": "php/table.vekt6_copo_jchat.php?cn=<? echo $casenumber; ?>&j=<? echo $uid; ?>",
        "table": "#vekt6_copo_jchat",
        "fields": [
            {
                "label": "Message",
                "name": "message",
                "type": "textarea"
            }
        ]

    } );


    var jchattable = $('#vekt6_copo_jchat').DataTable( {
        "dom": "Tfrtip",
        "ajax": "php/table.vekt6_copo_jchat.php?cn=<? echo $casenumber; ?>&j=<? echo $uid; ?>",
        "columns": [
            {    "data": "case_number", 
                 "defaultContent": <? echo $casenumber; ?>, 
                 "visible": false 
            },          
            {    
                 "data": "case_juror", 
                 "defaultContent": <? echo $uid; ?>, 
                 "width": "30px",
            },
            {
                "data": "timestamp", 
                "width": "75px",
            },          
            {
                "data": "message",
            }
        ],
        "order": [ 0, 'desc' ],
        "tableTools": {
            "sRowSelect": "os",
            "sRowSelector": 'td:first-child',
            "aButtons": [
                { "sExtends": "editor_create", "editor": editorC },
            ]
        }

    } );
    
setInterval( function () {
    jchattable.ajax.reload( null, false ); // user paging is not reset on reload;
}, 30000 );     
    
} );

}(jQuery));
php-----

 $date = date_create();
 $newDateTime = date_format($date, 'H:i:s y-m-d');
 $timestamp = date('h:i:sa m-d', strtotime($newDateTime));

 $casenumber = $_GET['cn'];
 $uid = $_GET['j'];

/*
 * Editor server script for DB table vekt6_copo_jnote
 * Created by http://editor.datatables.net/generator
 */

// DataTables PHP library and database connection
include( "lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;


// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'vekt6_copo_jchat', 'id' )
    ->fields(
        Field::inst( 'timestamp' )
            ->setValue( $timestamp ),
        Field::inst( 'case_juror' )
            ->setValue( $uid ),
        Field::inst( 'case_number' )
            ->setValue( $casenumber ),  
        Field::inst( 'message' )
    ) ->where('case_number', $casenumber) ->where('case_juror', $uid)
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    It won't display the ID column from the DB.

    The most direct way to include the primary key in the information for the table is to add it as another field, and set it to be read only. For example:

        Field::inst( 'id' )->set( false )
    

    The primary key column that is specified in the Editor constructor is automatically aliased by the Editor libraries to be called "DT_RowId" in the JSON data for the client-side and also has a prefix added to it to allow it to be a valid HTML and CSS selector. You could potentially use that data point and remove the prefix using a rendering function, but I think just adding the extra field is probably easier.

    You could make that column a hidden column if you want to sort on it, but for users to not see it.

    Regards,
    Allan

This discussion has been closed.