我想知道如何在 DataTables Editor 项目中使用对象而不是数组。该插件的作者建议使用对象而不是数组,但我找不到关于如何配置插件以使用对象的详细文章。下面是我迄今为止在数组中使用的代码。
JS:
var editor = new $.fn.dataTable.Editor({
ajax: "../stack/plugins/datatables/php/table.qpidvulh_to-do_list.php",
table: "#qpidvulh_to-do_list",
fields: [
{
label: "Item",
name: "item",
type: "textarea"
},
{
def: "(none)",
label: "Office",
name: "office",
type: "select",
ipOpts: [
{
label: "(none)",
value: "(none)"
},
{
label: "Alaina",
value: "Alaina"
},
{
label: "Felicita",
value: "Felicita"
},
{
label: "Lee",
value: "Lee"
},
{
label: "Luciana",
value: "Luciana"
},
{
label: "Rachel",
value: "Rachel"
}
]
},
{
def: "Office",
label: "Expeditor",
name: "expeditor",
type: "select",
ipOpts: [
{
label: "Office",
value: "Office"
},
{
label: "Carlos",
value: "Carlos"
},
{
label: "Danny",
value: "Danny"
},
{
label: "Steve",
value: "Steve"
}
]
},
{
label: "Deadline",
name: "deadline",
type: "date",
dateFormat: "mm\/dd\/y",
dateImage: "https://www.dcturano.com/stack/images/calendar.png"
}
]
});
$('#qpidvulh_to-do_list').dataTable({
ajax: "../stack/plugins/datatables/php/table.qpidvulh_to-do_list.php",
autoWidth: "false",
dom: "<'H'lfr>t<'F'ip>",
jQueryUI: "true",
pagingType: "full_numbers",
stateSave: "true",
type: "post",
columns: [
{
data: "item",
width: "54%"
},
{
data: "office",
width: "10%"
},
{
className: "p_hide",
data: "expeditor",
width: "10%"
},
{
className: "p_hide",
data: "deadline",
width: "9%"
},
{
className: "p_hide",
data: "created",
width: "9%"
},
{
className: "center p_hide",
data: null,
defaultContent: "<a href=''class='editor_edit'>Edit</a> | <a href='' class='editor_remove'>Delete</a>",
orderable: false,
width: "8%"
}
]
})
服务器脚本:
<?php
/*
* Editor server script for DB table qpidvulh_to-do_list
* Automatically generated by http://editor.datatables.net/generator
*/
// DataTables PHP library
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\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'qpidvulh_to-do_list' )
->fields(
Field::inst( 'item' )
->validator( 'Validate::minLen_required', 5 ),
Field::inst( 'office' ),
Field::inst( 'expeditor' ),
Field::inst( 'deadline' )
->validator( 'Validate::dateFormat', 'm/d/y' )
->getFormatter( 'Format::date_sql_to_format', 'm/d/y' )
->setFormatter( 'Format::date_format_to_sql', 'm/d/y' ),
Field::inst( 'created' )
->set( isset($_POST['action']) && $_POST['action'] === 'create' ? true : false )
->getFormatter( 'Format::date_sql_to_format', 'm/d/y' )
)
->process( $_POST )
->json();