How to add AJAX pagination in your plugins

15 years 9 months ago #67416 by p9939068
Go to plugin.class.php, just below the function _writePaging, add:

[code:1]
function _AJAXwritePaging( $user, $pagingParams, $postfix, $limit, $total, $task = 'tabclass' )
{
$base_url = $this->_getAbsURLwithParam( $pagingParams, $task, false, array( $postfix . 'limitstart' ) );
$prefix = $this->_getPrefix( $postfix );
return AJAXwritePagesLinks( $user, $pagingParams[$postfix . 'limitstart'], $limit, $total, $base_url, null, $prefix );
}
[/code:1]


Mike Feng
Creator of SIMGallery, SIMAnswers, and ParaInvite
www.simbunch.com
twitter.com/simbunch

Please Log in to join the conversation.

15 years 9 months ago #67417 by p9939068
Replied by p9939068 on topic Re:How to add AJAX pagination in your plugins
Next, go to comprofiler.class.php, just below the function writePagesLinks, add:

[code:1]
function AJAXwritePagesLinks($user, $limitstart, $limit, $total,$ue_base_url,$search=null,$prefix='') {
global $_CB_framework;
$ue_base_url = str_replace("index.php","index2.php",$ue_base_url)."&no_html=1&format=raw&user=".$user->id;
$limitstart = max( (int) $limitstart, 0 );
$limit = max( (int) $limit, 1 );
$total = (int) $total;
$ret='';
if (is_array($search)) {
$search_str = '';
foreach ($search as $k => $v) {
if ($v && $k!='limitstart') $search_str .= '&'.urlencode($prefix.$k).'='.urlencode($v);
}
} else {
$search_str = (($search) ? '&'.urlencode($prefix).'search='.urlencode($search) : '');
}
$limstart_str = '&'.urlencode($prefix).'limitstart=';
$pages_in_list = 6; // set how many pages you want displayed in the menu (not including first&last, and ev. ... repl by single page number.
$displayed_pages = $pages_in_list;
$total_pages = ceil( $total / $limit );
$this_page = ceil( ($limitstart+1) / $limit );
// $start_loop = (floor(($this_page-1)/$displayed_pages))*$displayed_pages+1;
$start_loop = $this_page-floor($displayed_pages/2); if ($start_loop < 1) $start_loop = 1; if ($start_loop == 3) $start_loop = 2; //BB
if ($start_loop + $displayed_pages - 1 < $total_pages-2) {
$stop_loop = $start_loop + $displayed_pages - 1;
} else {
$stop_loop = $total_pages;
}

if ($this_page > 1) {
$page = ($this_page - 2) * $limit;
$firsturl = $ue_base_url.$limstart_str.'0'.$search_str;
$ret .= "\n<a onClick=\"sendAJAXpage_".$prefix."('$firsturl')\" class=\"pagenav\" href=\"javascript:void(0)\" title=\"" . _UE_FIRST_PAGE . "\">&lt;&lt; " . _UE_FIRST_PAGE . "</a>";
$prevurl = $ue_base_url.$limstart_str.$page.$search_str;
$ret .= "\n<a onClick=\"sendAJAXpage_".$prefix."('$prevurl')\" class=\"pagenav\" href=\"javascript:void(0)\" title=\"" . _UE_PREV_PAGE . "\">&lt; " . _UE_PREV_PAGE . "</a>";
if ($start_loop > 1) $ret .= "\n<a onClick=\"sendAJAXpage_".$prefix."('$firsturl')\" class=\"pagenav\" href=\"javascript:void(0)\" title=\"" . _UE_FIRST_PAGE . "\"><strong>1</strong></a>";
if ($start_loop > 2) $ret .= "\n<span class=\"pagenav\"> <strong>...</strong> </span>";
} else {
$ret .= '<span class="pagenav">&lt;&lt; '. _UE_FIRST_PAGE .'</span> ';
$ret .= '<span class="pagenav">&lt; '. _UE_PREV_PAGE .'</span> ';
}

for ($i=$start_loop; $i <= $stop_loop; $i++) {
$page = ($i - 1) * $limit;
if ($i == $this_page) {
$ret .= "\n <span class=\"pagenav\">[".$i."]</span> ";
} else {
$pageurl = $ue_base_url.$limstart_str.$page.$search_str."&no_html=1&format=raw";
$ret .= "\n<a onClick=\"sendAJAXpage_".$prefix."('$pageurl')\" class=\"pagenav\" href=\"javascript:void(0);\"><strong>$i</strong></a>";
}
}

if ($this_page < $total_pages) {
$page = $this_page * $limit;
$end_page = ($total_pages-1) * $limit;
if ($stop_loop < $total_pages-1) $ret .= "\n<span class=\"pagenav\"> <strong>...</strong> </span>";
$endurl = $ue_base_url.$limstart_str.$end_page.$search_str;
if ($stop_loop < $total_pages) $ret .= "\n<a onClick=\"sendAJAXpage_".$prefix."('$endurl')\" class=\"pagenav\" href=\"javascript:void(0);\" title=\"" . _UE_END_PAGE . "\"><strong>".$total_pages."</strong></a>";
$nexturl = $ue_base_url.$limstart_str.$page.$search_str;
$ret .= "\n<a onClick=\"sendAJAXpage_".$prefix."('$nexturl')\" class=\"pagenav\" href=\"javascript:void(0)\" title=\"" . _UE_NEXT_PAGE . "\">" . _UE_NEXT_PAGE . " &gt;</a>";
$ret .= "\n<a onClick=\"sendAJAXpage_".$prefix."('$endurl')\" class=\"pagenav\" href=\"javascript:void(0)\" title=\"" . _UE_END_PAGE . "\">" . _UE_END_PAGE . " &gt;&gt;</a>";
} else {
$ret .= '<span class="pagenav">'. _UE_NEXT_PAGE .' &gt;</span> ';
$ret .= '<span class="pagenav">'. _UE_END_PAGE .' &gt;&gt;</span>';
}
return $ret;
}
[/code:1]

Post edited by: p9939068, at: 2008/07/05 14:55


Mike Feng
Creator of SIMGallery, SIMAnswers, and ParaInvite
www.simbunch.com
twitter.com/simbunch

Please Log in to join the conversation.

15 years 9 months ago #67418 by p9939068
Replied by p9939068 on topic Re:How to add AJAX pagination in your plugins
In your CB plugin (that uses CB pagination), replace [code:1]$this->_writePaging[/code:1] with [code:1]$this->_AJAXwritePaging[/code:1]

IMPORTANT: _AJAXwritePaging has a new param $user which you need to add to the params list. It's the first in the function's param list. See the first post on this thread for more information


Mike Feng
Creator of SIMGallery, SIMAnswers, and ParaInvite
www.simbunch.com
twitter.com/simbunch

Please Log in to join the conversation.

15 years 9 months ago #67419 by p9939068
Replied by p9939068 on topic Re:How to add AJAX pagination in your plugins
Add this javascript somewhere on your plugin:

[code:1]
<script type=\"text/javascript\">
function sendAJAXpage_cbplugin(url) {
new Ajax(url, {
method: 'get',
update: $('cbplugin_maindiv')
}).request();
}
</script>
[/code:1]

where cbplugin_maindiv is the div in which to load up your HTMLRequest stuff.
IMPORTANT: replace cbplugin with the tab name of your plugin (lowercase). Example: if your plugin has the tab getMyCBPluginTab, your div should be named "mycbplugintab_maindiv", and the javascript function should be sendAJAXpage_mycbplugin(url).

The rest is pretty common sense AJAX stuff. I believe you can add this javascript snippet into CB's own JS library, but I haven't figure that one out ;)

It is important to note that this javascript requires mootools, but you can modify it easily to work with jQ.

Post edited by: p9939068, at: 2008/07/05 14:56


Mike Feng
Creator of SIMGallery, SIMAnswers, and ParaInvite
www.simbunch.com
twitter.com/simbunch

Please Log in to join the conversation.

15 years 9 months ago #67422 by p9939068
Replied by p9939068 on topic Re:How to add AJAX pagination in your plugins
Your CB plugin should have the following format:

function getTabComponent($tab,$my,$ui,$postdata) {
this thing handles AJAX requests in CB
$user = new moscomprofilerUser( $_CB_database );
$user->load( (int) cbGetParam( $_GET, 'user' ) );
return _outputStuff($tab,$user,$ui)

}

function getDisplayTab($tab,$user,$ui) {
usual getDisplayTab stuff
div id=cbplugin_maindiv
call function _outputStuff($tab,$user,$ui)
end div
}

function _outputStuff($tab,$user,$ui) {
return output including pagination HTML codes
eg. << < 1 2 3 > >> etc
}


Mike Feng
Creator of SIMGallery, SIMAnswers, and ParaInvite
www.simbunch.com
twitter.com/simbunch

Please Log in to join the conversation.

15 years 9 months ago #67423 by p9939068
Replied by p9939068 on topic Re:How to add AJAX pagination in your plugins
example:
www.pishieposhie.com/Profile/View-user-profile.html?user=63

(please do not register if you're not from Singapore!)

Post edited by: p9939068, at: 2008/07/07 04:59


Mike Feng
Creator of SIMGallery, SIMAnswers, and ParaInvite
www.simbunch.com
twitter.com/simbunch

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.221 seconds

Facebook Twitter LinkedIn