假设您使用的是标准column_cb()函数,列表表将在$ _GET数组中传递选定行的ID,标记为您在列表表的构造函数中分配给“单数”的内容。
这是一个典型的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?
) );
}
因此,如果您检查列表中的三行,然后从批量操作列表中选择“删除”,然后点击“应用”,则可以使用$ _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);
}
}
}