問題描述
我正在使用自定義資料表在 WordPress 儀錶板中顯示。該表填充了我內建的一個外掛的資料庫表。
我一直在使用提供的 WordPress 自定義列表表示例來解決我在這方面的大部分編碼問題,但該示例沒有任何處理大量操作的東西。以下是檔案示例的連結:http://wordpress.org/extend/plugins/custom-list-table-example/
要處理批次操作,該示例僅提供以下內容:
function process_bulk_action() {
//Detect when a bulk action is being triggered...
if( 'delete'===$this->current_action() ) {
wp_die('Items deleted!');
}
}
我想知道如何拉動選擇的專案,以便我可以刪除它們或相應地編輯他們的資料庫條目。
最佳解決方案
假設您正在使用 standard column_cb() 函式,則列表將在 $ _GET 中的陣列中傳遞所選行的 ID,標記為在列表表的建構函式中分配給’singular’ 的任何行。
這是一個典型的 column_cb():
function column_cb($item){
return sprintf(
'<input type="checkbox" name="%1$s[]" value="%2$s" />',
/*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("video")
/*$2%s*/ $item->id //The value of the checkbox should be the record's id
);
}
例如,假設我有一個顯示影片的列表。建構函式將如下所示:
function __construct(){
global $status, $page;
//Set parent defaults
parent::__construct( array(
'singular' => 'video', //singular name of the listed records
'plural' => 'videos', //plural name of the listed records
'ajax' => false //does this table support ajax?
) );
}
因此,如果您在列表表中檢查三行,請從批次操作列表中選擇”Delete”,然後點選 「應用」,您可以使用 $ _GET [‘video’] 訪問所選行。
function process_bulk_action() {
//Detect when a bulk action is being triggered...
if( 'delete'===$this->current_action() ) {
foreach($_GET['video'] as $video) {
//$video will be a string containing the ID of the video
//i.e. $video = "123";
//so you can process the id however you need to.
delete_this_video($video);
}
}
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。