我扩展了类WP_List_Table以显示自定义数据库表的列表记录.列表是成功的,但我对如何实现一个下拉过滤器来根据其类别过滤我的自定义db表记录感到生气.
请共享任何代码以添加下拉过滤器以过滤我的自定义数据库表记录.字段名称是cat_id.
Mia*_*jid 16
在这里发布问题后3个小时的斗争后,我探索了课程,并找到了解决方案,所以我在这里分享信息.
有一个函数函数extra_tablenav($ which),我用我的函数覆盖了这个函数,
function extra_tablenav( $which ) {
global $wpdb, $testiURL, $tablename, $tablet;
$move_on_url = '&cat-filter=';
if ( $which == "top" ){
?>
<div class="alignleft actions bulkactions">
<?php
$cats = $wpdb->get_results('select * from '.$tablename.' order by title asc', ARRAY_A);
if( $cats ){
?>
<select name="cat-filter" class="ewc-filter-cat">
<option value="">Filter by Category</option>
<?php
foreach( $cats as $cat ){
$selected = '';
if( $_GET['cat-filter'] == $cat['id'] ){
$selected = ' selected = "selected"';
}
$has_testis = false;
$chk_testis = $wpdb->get_row("select id from ".$tablet." where banner_id=".$cat['id'], ARRAY_A);
if( $chk_testis['id'] > 0 ){
?>
<option value="<?php echo $move_on_url . $cat['id']; ?>" <?php echo $selected; ?>><?php echo $cat['title']; ?></option>
<?php
}
}
?>
</select>
<?php
}
?>
</div>
<?php
}
if ( $which == "bottom" ){
//The code that goes after the table is there
}
}
Run Code Online (Sandbox Code Playgroud)
然后我跳进了函数prepare_items()并在查询字符串后添加了一行,
if( $_GET['cat-filter'] > 0 ){
$query = $query . ' where cat_id=' . $_GET['cat-filter'];
}
Run Code Online (Sandbox Code Playgroud)
没有在这里完成,我添加了一些javascript行来执行下拉,
$('.ewc-filter-cat').live('change', function(){
var catFilter = $(this).val();
if( catFilter != '' ){
document.location.href = 'admin.php?page=ewc-testimonial'+catFilter;
}
});
Run Code Online (Sandbox Code Playgroud)
如果有人需要更多帮助,那么它的工作很酷很好.
谢谢你的时间.
| 归档时间: |
|
| 查看次数: |
12071 次 |
| 最近记录: |