function listTable(){
return {
rows:[],
alias:{},
options:{},
noNumbering:false,
noThead:false,
tools:false,
fontSize:12,
replaceWith:{
},
loadData:function(data){
data = typeof data === 'undefined' ? {} : data;
this.page = 0;
if(data.hasOwnProperty('row')){
this.rows = data.row;
}
if(data.hasOwnProperty('alias')){
this.alias = data.alias;
}
if(data.hasOwnProperty('options')){
this.options = data.options;
}
if(data.hasOwnProperty('title')){
this.title = data.title;
}else{
this.title = 'Display Data Untuk: ';
}
if(data.hasOwnProperty('search')){
this.search = data.search;
this.title = this.title + data.search;
}
if(data.hasOwnProperty('rule')){
this.rule = data.rule;
}
if(data.hasOwnProperty('tools')){
this.tools = data.tools;
}
},
drawTable:function(table_el){
this.table_el = table_el;
pagination = this.pagination();
title = '
' + this.title + '
';
pagination = '' + pagination + ' ' + this.genTools() + '
';
jQuery(table_el).html(title + pagination + this.genTable() + pagination);
return true;
},
genTools:function(){
if(!this.tools){
return false;
}
d = '';
for(var i in this.tools){
if(this.tools[i].hasOwnProperty('type')){
type = this.tools[i].type;
attr = '';
if(this.tools[i].hasOwnProperty('attr')){
for(var a in this.tools[i].attr){
attr += ' '+a+'="'+this.tools[i].attr[a]+'" ';
}
}
if(type == 'button'){
d += '';
}
if(type == 'text'){
d += ''+(this.tools[i].hasOwnProperty('content') ? this.tools[i].content : '')+'';
}
}
}
return d;
},
genTable:function(){
rows = this.rows;
table = '';
table += '';
table += 'Judul | ';
table += 'Penulis | ';
table += 'Penerbit | ';
table += 'Tahun | ';
table += 'Kota | ';
table += 'Kelas Subyek | ';
table += 'ISSN/ISBN | ';
table += '
';
table += '';
for(var row in rows){
if(rows[row].hasOwnProperty('judul')){
table += '';
table += ''+rows[row].judul+' | ';
table += ''+rows[row].penulis+' | ';
table += ''+rows[row].penerbit+' | ';
table += ''+rows[row].th_terbit+' | ';
table += ''+rows[row].kota+' | ';
table += ''+rows[row].kelas+' | ';
table += ''+rows[row][(rows[row].hasOwnProperty('issn') ? 'issn' : 'isbn')]+' | ';
table += '
';
}else{
table += '--Tidak Ditemukan--
';
}
}
table += "
";
return table;
},
replaceWithVal:function(row,str){
for(var col in row){
str = str.replace('{'+col+'}',row[col]);
}
return str;
},
pagination:function(){
range = parseInt(this.options.limit[1]);
count = parseInt(this.options.data_count);
pg = (range + parseInt(this.options.limit[0])) / range;
c = '';
pg = pg < 1 ? 1 : pg;
side = 3;
pg = parseInt(pg);
console.log(pg);
pages = Math.ceil(count / range);
maxShown = 1 + (side * 2);
shown = [];
half = ((maxShown - 1) / 2);
start = pg - half;
start = start < 1 ? 1 : start;
maxShown = pg + half;
maxShown = maxShown > pages ? pages : maxShown;
if(pg > (half + 1)){
c += '1';
}
if(pg > (half + 2)){
c += '...';
}
for(var i = start;i <= maxShown;i++){
shown.push(i);
c += ''+i+'';
}
if((pg+half) < pages - 1){
c += '...';
}
if((pg+half) < pages){
c += ''+pages+'';
}
return 'Halaman: '+c+'
Baris/Hal.: '+this.rangeSelect();
},
rangeSelect:function(){
return '';
},
getConfig:function(){
return {
'rule':this.rule,
'search':this.search,
'options':this.options
};
},
openRange:function(range){
this.options.limit = [0,parseInt(range)];
return this.getConfig();
},
openPage:function(pg){
pg = parseInt(pg);
pg = pg < 1 ? 1 : pg;
range = parseInt(this.options.limit[1]);
this.options.limit[1] = range;
this.options.limit[0] = (pg - 1) * range;
return this.getConfig();
},
openOrder:function(col,type){
type = type == 'DESC' ? 'DESC' : 'ASC';
if(!this.options.hasOwnProperty('order')){
this.options.order = null;
}
this.options.order = {'col':col,'sort':type};
return this.getConfig();
}
}
}
jQuery(document).ready(function(){
_list = new listTable();
jQuery('#sendMe').click(function(e){
e.preventDefault();
dataSend(serialiseObj('#searchm'));
});
jQuery(document).on('click','.pg',function(){
page(jQuery(this).data('page'));
});
rule = getParameterByName('rule');
search = getParameterByName('search');
target = getParameterByName('target');
if(rule != null && search != null){
window._data_send = {rule:rule,search:search,target:target};
dataSend(window._data_send);
}else{
window._data_send = {rule:"",search:"",target:"buku"};
dataSend(window._data_send);
}
});
function serialiseObj(el){
data = {
rule:jQuery(el).find('[name="rule"]').val(),
search:jQuery(el).find('[name="search"]').val(),
target:jQuery(el).find('[name="target"]').val()
};
console.log(data);
window._data_send = data;
return data;
}
function dataSend(data){
jQuery.ajax({
url:'http://perpus.iainsalatiga.ac.id/my/statis/index.php/list-buku',
type:'POST',
data:data,
beforeSend:function(){
jQuery('#message').html('Loading...
');
},
success:function(d){
jQuery('#message').html('');
_list.loadData(d.arg);
_list.drawTable('#data-row');
},
error:function(){
jQuery('#message').html('');
}
});
}
function page(pg){
pg = parseInt(pg);
data = window._data_send;
data.pg = pg;
dataSend(data);
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}