CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Pagination: How to trans string param when using $pager->links()?

Open wuzheng40 opened this issue 9 years ago • 2 comments

I want to search items with condition, submit form by method GET, It will be a querystring like search_Id=&search_Username=&search_Email=&search_Auth=&search_Status=1

When I use $pager->links() to genrate a pager link, it's link only contain page=2.

How can I trans param to $pager->links() 2017-02-23 9 28 38 2017-02-23 9 42 18

wuzheng40 avatar Feb 23 '17 01:02 wuzheng40

It clones the current request's URI and just adds the page query variable to that, so it should, theoretically, work as you want it to automatically. It sounds like it's not currently, though, so that's something that needs to be looked into. Unfortunately, I am completely slammed with work right now and won't have a whole lot of time to tackle this.

If you want to take a look at it, check in the URI class' addQuery() method since that's what is being called to add the page query variable to it. That and the _toString() method of the URI are the two most likely places this issue will be cropping up.

lonnieezell avatar Feb 23 '17 03:02 lonnieezell

I think it's a bug. When I visit a url like http://localhost:9888/Admin/Admins?search_Auth=Admin&search_Status=1.

	protected function ensureGroup(string $group)
	{
		...
		$this->groups[$group] = [
			'uri'         => clone Services::request()->uri,
			...
		];
	}

uri 's value is http://localhost:9888/Admin/Admins, it isn't contain ?search_Auth=Admin&search_Status=1

So it return http://localhost:9888/Admin/Admins?page=2

I mod code add a if($_GET) to retrieve querystring and set it in uri.

	protected function ensureGroup(string $group)
	{
		if (array_key_exists($group, $this->groups))
		{
			return;
		}

		$this->groups[$group] = [
			'uri'         => clone Services::request()->uri,
			'hasMore'     => false,
			'total'       => null,
			'currentPage' => $_GET['page_'.$group] ?? $_GET['page'] ?? 1,
			'perPage'     => $this->config->perPage,
			'pageCount'   => 1,
		];

		if($_GET)
		{
			$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
		}
	}

Mod setQuery-->addQuery

        public function getPageURI(int $page = null, string $group = 'default', $returnObject = false)
	{
		...
		$uri->setQuery('page='.$page);
		...
	}

To

        public function getPageURI(int $page = null, string $group = 'default', $returnObject = false)
	{
		...
		$uri->addQuery('page='.$page);
		...
	}

It's work. 2017-02-23 4 16 46

wuzheng40 avatar Feb 23 '17 08:02 wuzheng40