1

我想将名为“videoarchives”的数据库中的 id-key 映射到名为 DT_RowId 的 DataTables-id。创建新行时,我希望它使用唯一的主键,并在编辑它以维护这个唯一的主键时。

我想知道我应该如何修改下面videos.php中的代码来完成这个。我在文档中找到了这个例子,使用了另一种语法。我怎样才能调整下面的代码来完成同样的事情?(http://www.datatables.net/examples/server_side/ids.html ):

// 应读取并发送回 DataTables 的数据库列数组。//db参数代表数据库中的列名,而dt // 参数代表 DataTables 列标识符 - 在本例中是 object // 参数名

$columns = array(
    array(
        'db' => 'id',
        'dt' => 'DT_RowId',
        'formatter' => function( $d, $row ) {
            // Technically a DOM id cannot start with an integer, so we prefix
            // a string. This can also be useful if you have multiple tables
            // to ensure that the id is unique with a different prefix
            return 'row_'.$d;
        }
    ),

videoadmin.html 中的一些代码:

$(document).ready(function() { //fields we can add or edit ?
    editor = new $.fn.dataTable.Editor( {
        ajax: "../php/videos.php",
        table: "#example",
        idSrc: "DT_RowId",
        fields: [ 

            {
                label: "Title:",
                name: "videoarchives.Title"
            }, {
                label: "Date:",
                name: "videoarchives.Date",
                type: "date"
            }, {
                label: "VideoDescription:",
                name: "videoarchives.VideoDescription"
            }, {
                label: "Language:",
                name: "videoarchives.Language"
            }, {
                label: "Category:",
                name: "videoarchives.Category"
            }, {
                label: "VideoLink:",
                name: "videoarchives.VideoLink",

            }, {
                label: "HDLink:",
                name: "videoarchives.HDLink"
            },
            {
            label: "German title:",
            name: "videoarchives_german.TitleGerman"
            },
            {
            label: "German subtitle:",
            name: "videoarchives_german.SubtitleGerman"
            },
            {
            label: "German description:",
            name: "videoarchives_german.DescriptionGerman"
            }

        ]
    } );

    //oTable.fnSetColumnVis(0, false);
    //http://stackoverflow.com/questions/15892995/assign-id-to-datatable-row-from-json-data
    var dataTable = $('#example').DataTable( {
        dom: "Tfrtip",
        ajax: "../php/videos.php",

        columns: [

            { data: "DT_RowId" },
            { data: "videoarchives.Title" },

            { data: "videoarchives.Date" },
            { data: "videoarchives.VideoDescription" },
            { data: "videoarchives.Language" },
            { data: "videoarchives.Category" },
            { data: "videoarchives.VideoLink" },
            { data: "videoarchives.HDLink" },
            { data: "videoarchives_german.TitleGerman" },
            { data: "videoarchives_german.SubtitleGerman" },
            { data:  "videoarchives_german.DescriptionGerman"} 

        ],
        tableTools: {
            sRowSelect: "os",
            aButtons: [
                { sExtends: "editor_create", editor: editor },
                { sExtends: "editor_edit",   editor: editor },
                { sExtends: "editor_remove", editor: editor }
            ]
        } //table Tools

    } ); //end of init datatable...

    dataTable.fnSetColumnVis(0, false)

    /* Click event handler */
     $('#example tbody').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        }
        else {
            table.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    } );

    $('#button').click( function () {
        table.row('.selected').remove().draw( false );
    } );


} );

视频.php:

include( "../../php/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\Validate;
            $pkey = "id";

    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'videoarchives', $pkey )
        ->fields(
            Field::inst( 'videoarchives.id' ),
            Field::inst( 'videoarchives.Title' )->validator( 'Validate::notEmpty' ),
            Field::inst( 'videoarchives.Date' ) ->validator( 'Validate::dateFormat', 
            array( "format"  => Format::DATE_ISO_8601,
                    "message" => "Please enter a date in the format yyyy-mm-dd" ) ) ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
                ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
            Field::inst( 'videoarchives.VideoDescription' ),
            Field::inst( 'videoarchives.Language' ),
            Field::inst( 'videoarchives.Category' ),
            Field::inst( 'videoarchives.VideoLink' ),
            Field::inst( 'videoarchives.HDLink' ),
            Field::inst( 'videoarchives_german.TitleGerman' ),
            Field::inst( 'videoarchives_german.SubtitleGerman' ),
            Field::inst( 'videoarchives_german.DescriptionGerman' )

        )
        ->leftJoin( 'videoarchives_german', 'videoarchives_german.id', '=', 'videoarchives.id' )
        ->process( $_POST )
        ->json();
4

1 回答 1

0

前端:

var dataTable = $('#example').DataTable( {
    dom: "Tfrtip",
    ajax: "../php/videos.php",

    columns: [
        { data: "DT_RowId", <!-- this is the primary key.. -->
            render: function ( data ) 
            {
              return data.replace( 'row_', '' );
            } 
        },
        { data: "videoarchives.Title" },

        { data: "videoarchives.Date" },
        { data: "videoarchives.VideoDescription" },
        { data: "videoarchives.Language" },
        { data: "videoarchives.Category" },
        { data: "videoarchives.VideoLink" },
        { data: "videoarchives.HDLink" },
        { data: "videoarchives_german.TitleGerman" },
        { data: "videoarchives_german.SubtitleGerman" },
        { data:  "videoarchives_german.DescriptionGerman"} 

    ],

后端:

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'videoarchives' )
    ->fields(
//      Field::inst( 'videoarchives.id' ), //The primary key should not be initiated here.
        Field::inst( 'videoarchives.Title' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'videoarchives.Date' ) ->validator( 'Validate::dateFormat', 
        array( "format"  => Format::DATE_ISO_8601,
                "message" => "Please enter a date in the format yyyy-mm-dd" ) ) ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
            ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
        Field::inst( 'videoarchives.VideoDescription' ),
        Field::inst( 'videoarchives.Language' ),
        Field::inst( 'videoarchives.Category' ),
        Field::inst( 'videoarchives.VideoLink' ),
        Field::inst( 'videoarchives.HDLink' ),
        Field::inst( 'videoarchives_german.TitleGerman' ),
        Field::inst( 'videoarchives_german.SubtitleGerman' ),
        Field::inst( 'videoarchives_german.DescriptionGerman' )

    )
    ->leftJoin( 'videoarchives_german', 'videoarchives_german.id', '=', 'videoarchives.id' )
    ->process( $_POST )
    ->json();
于 2014-10-09T08:55:49.740 回答