本质上,我正在显示一个对象数组(从MySQL检索的数据)。我目前正致力于在用户交互时对这些数据进行过滤和排序的功能。分页方面已经解决了。但我觉得我可能会使整合变得复杂。
当用户点击表头时,需要重新加载页面,数据按升序排序。如果用户单击相同的表头,则页面需要重新加载,数据按降序排序。
用户还有一个下拉框和一个输入字段,用于搜索特定列中的特定数据。
现在我遇到的一个大问题是将这一切都集成到一个函数中,而不会让网址疯狂。
这是我当前的功能:
public function tp1s($page = 0, $column = null, $filter = null)
{
$data['location'] = 'tp1s';
//Pagination Config
$config['base_url'] = 'admin/tp1s';
$config['per_page'] = 19;
//Check if filters are in place
if (isset($column) && isset($filter)) {
$totalRows = /*Model that returns total number of rows based on filter selection*/
$config['base_url'] = base_url('admin/tp1s/' . $column . '/' . $filter);
$dataStart = /*Model that returns table data that the user will see. It only returns data that matches up with the filtering. It is offset and limited based on $page and $config['per_page']*/
} else {
$totalRows = /*Model that returns number of rows with no filtering*/
$dataStart = /*Model that returns all table data that the user will see. It is offset and limited based on $page and $config['per_page']*/
}
//Pagination
$this->load->library('pagination');
$config['total_rows'] = $totalRows;
$this->pagination->initialize($config);
//Get TP1s
$data['data']['tp1s'] = $dataStart;
//Get schools (This little section is not important)
$data['schools'] = $this->Admin->getTableList('schools');
foreach($data['schools'] as $school){
preg_match_all('/[A-Z]/', $school->name, $abrev);
$abrev = implode($abrev[0]);
$school->name = $school->zip.'-'.$abrev;
}
//Set variable for view then load the view
$data['path'] = 'panels/admin/tp1s';
$this->load->view('pages/admin', $data);
}
当没有过滤器或分页时,网址显示为https://site/admin/tp1s
当存在过滤器且没有分页时,网址显示为https://site/admin/tp1s/column/filter
。
任何分页都会显示在网址的末尾。
现在我的问题是弄清楚如何实现排序。我想在所有这些中使用AJAX,但是显示敏感数据,我不想创建一个回显敏感数据的控制器。
The Big Hold Up
我可以通过允许url传递另外两个变量来进行排序来解决这个问题。但我真的想避免这种情况。那时网址看起来很草率。就目前而言,这将是我必须要做的事情。我再次通常只使用Ajax来检索数据,同时传递控制器可以使用的一些变量。但是我很确定人们可以通过直接访问控制器来查看数据,因为数据会被回应。所以这不是一个选项,因为有敏感的数据。
因此,我提出的唯一“解决方案”是使用URL查询(我甚至不知道您可以在使用URL段时使用CodeIgniter)。这并没有真正解决我的问题。该URL仍然看起来有点混乱。但是,它至少具有“我正在被程序使用”看起来。我尽力总结评论中的代码,以便那些不需要阅读所有内容的人:
public function tp1s($page = 0){
$queryString = $_SERVER['QUERY_STRING']; //Get query string to pass on to the pagination
//Some Pagination config that my code will use
$config['base_url'] = base_url('admin/tp1s');
$config['per_page'] = 18;
$tableData = $this->Model->getThatData() //Get rows from table in database
//Check if filters are in place
if (isset($_GET['filter']) && isset($_GET['column'])) {
//Get url query variables and set PHP variables
$column = $_GET['column'];
$filter = $_GET['filter'];
$tableData = filterArray($tableData, $column, $filter); //Helper that filters data
}
//Check if sorting is in place
if(isset($_GET['sort']) && isset($_GET['by']) && isset($_GET['type'])){
//Get url query variables and set PHP variables
$sort = $_GET['sort'];
$by = $_GET['by'];
$type = $_GET['type'];
//Other functions within the helper that sort the data
if($by == 'ascending'){
$tableData = sortAscending($tableData, $sort, $type);
}elseif($by == 'descending'){
$tableData = sortDescending($tableData, $sort, $type);
}
}
//Some Pagination Config
$this->load->library('pagination');
$config['reuse_query_string'] = true;
$config['total_rows'] = count($tableData);
$this->pagination->initialize($config);
//Selects correct section of sorted and filtered data that should be presented to the user according to the Pagination
$tableData = array_slice($tableData, $page, $config['per_page']);
/*
Some unimportant code that loads the view and stuff
*/
}