HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Pagination plugin

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
paginationpluginstackoverflow

Problem

This plugin is written in jQuery and is made to support the codeigniter framework. It is an ajax powered table pagination plugin designed to provide only that. You should be able to use almost any other table scripts with this plugin.

```
(function($){
$.fn.extend({
tpaginate: function(options) {

//Settings list and the default values
var defaults = {
page: 0,
url : null,
rows: 20,
nodata: 'The table is empty!',
actions: true
};

var table;
var foot;
var body;
var curpagespan;
var totpagespan;
var oldbody;
var replaced = false;

var options = $.extend(defaults, options);

return this.each(function() {
table = this;
foot = $(table).find(' > tfoot > tr > td');
body = $(table).find(' > tbody');
create_footer(foot);
curpagespan = foot.find(' > #table_pagecount > span#page');
totpagespan = foot.find(' > #table_pagecount > span#page_total');
curpagespan.html(0);
totpagespan.html(0);
$('#searchButton').click(function(){
var term = $('#search').val();
if(term.length >= 3){
if(replaced)
load_data(1, term, true, false);
else
load_data(1, term, true, true);
}
});
$('#search').keyup(function(){
if($(this).val() == ''){
create_tbody(false, false, true);
}
});
});

function create_footer() {
var pagecount = $('Page of ');
var pager = $('FirstPrevNextLast');
foot.

Solution

That's a large piece of code.

I highly recommended to use lower camel case for variables.

Here are some hints from me, just only micro-refactoring.

if(term.length >= 3) {
  if(replaced)
    load_data(1, term, true, false);
  else
    load_data(1, term, true, true);
}


into:

if(term.length >= 3) {
  load_data(1, term, true, !replaced);                        
}


function load_data(page, search, action, save) {
   if(save == null)
     save = false;
   else
     save = true;
 ...


into:

function load_data(page, search, action, save) {
  save = save == null ? false : true;
...


for(var j in rows[i]) {
   if(j != 'id' && j != 'actions')
     row.append(''+(rows[i][j] == null ? '' : rows[i][j])+'');
   if(j == 'actions' && rows[i][j] != null && options.actions)
     row.append(rows[i][j]);
 }


into:

for(var j in rows[i]) {
  var rowsIJ = rows[i][j];

  if(j != 'id' && j != 'actions')
    row.append(''+(rowsIJ == null ? '' : rowsIJ)+'');
  if(j == 'actions' && rowsIJ != null && options.actions)
    row.append(rowsIJ);
}

Code Snippets

if(term.length >= 3) {
  if(replaced)
    load_data(1, term, true, false);
  else
    load_data(1, term, true, true);
}
if(term.length >= 3) {
  load_data(1, term, true, !replaced);                        
}
function load_data(page, search, action, save) {
   if(save == null)
     save = false;
   else
     save = true;
 ...
function load_data(page, search, action, save) {
  save = save == null ? false : true;
...
for(var j in rows[i]) {
   if(j != 'id' && j != 'actions')
     row.append('<td class="'+j+'td">'+(rows[i][j] == null ? '' : rows[i][j])+'</td>');
   if(j == 'actions' && rows[i][j] != null && options.actions)
     row.append(rows[i][j]);
 }

Context

StackExchange Code Review Q#77, answer score: 3

Revisions (0)

No revisions yet.