0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHPのPaginate->sortで、タブが切り替わってしまうのを、防ぐ。

Last updated at Posted at 2014-12-10

自分用メモです。かなり不親切なメモです。ご注意ください。

より良い方法を見つけたので、更新しました。

より良い方法、hash使う

View
<?php $this->Paginator->options(array('url' => array('?' => $params, '#' => 'hoge'))); ?>
Controller
// ソートのoption用パラメータを用意
$params = $this->request->query ? $this->request->query : '';
$this->set('params', $params);

こんな感じにしておけば、既存のGETパラメータも失わずに、URLの後ろに、#hogeを付けることができる!

あとはこれを(#hoge)をjQuery側で、ひっぱってきてタブを切り替えればよい。

js
// hashを確認して、タブを切り替える
var platform = location.hash.substr(1);
if (platform) {
	jQuery('.toggle').removeClass('active');
	jQuery('[data-platform=' + platform + ']').addClass('active');
	jQuery('.toggle-data').hide();
	jQuery('[data-platform=' + platform + ']').show();
	jQuery('.' + platform).show();
}

getUrlVars使う、めんどい方法。

しかもこちらは、他にパラメータを付けていた場合は、それが無視されちゃったので、ダメ。

view.ctp
<?php $this->Paginator->options(array('url' => array('tab' => 'hoge'))); ?>

という感じで、paginatorにオプションつけます。そうするとURLが以下のようになるので。

ソートのところのURL
http://piyo.net/limit:100/sort:Model.field/direction:asc/tab:hoge

これで、GETパラメータ取得して、jQuery側でこねこねしてみました。

foo.js
jQuery(document).ready(function($){

	// GETパラメータを確認して、タブを切り替える 
	var tab = getUrlVars()['tab'];
	if (tab != null) {
		jQuery('.toggle').removeClass('active');
		jQuery('[data-tab=' + tab + ']').addClass('active');
		jQuery('.toggle-data').hide();
		jQuery('[data-tab=' + tab + ']').show();
		jQuery('.' + tab).show();
	}

	function getUrlVars()
	{
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('/') + 1).split('/');
		for(var i = 0; i <hashes.length; i++)
		{
			hash = hashes[i].split(':');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	}
});

以上です。ありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?