/*
정렬이 가능한 목록 javascript
거의 그룹웨어에서 사용함
2021-07-16 fontawesome5 버전에 맞게 수정
*/
function init_sort_list(e){
var search_form = $(e).find("form[name=frmSearch]"),
page = $(e).find("input[name=p]"),
pagesize = $(e).find("select[name=ps]").length > 0 ? $(e).find("select[name=ps]") : $(e).find("input[name=ps]"),
query = $(e).find("input[name=qs]").val(),
order = $(e).find("input[name=ordo]"),
path = $(e).find("input[name=path]").val(),
table = $(e).find("table.tbl_sort_list");
search_form.on("submit",function(){ //검색 시
query = $(search_form).serialize();
page.val(1);
load_contents(page.val(), pagesize.val(), query, order.val(), table.children("tbody"), path);
});
search_form.find(".btn_search_aside .reset").on("click",function(){
page.val(1);
search_form.find("input[type=text]").val('');
search_form.find("select").each(function(){
$(this).val( $(this).children("option").eq(0).val() );
});
if(search_form.find("input[type=checkbox]").length > 0){
var cb_data = '';
search_form.find("input[type=checkbox]").each(function(){
if(cb_data.indexOf($(this).attr("name")) < 0){
if(cb_data.length > 0){cb_data += '|^|';}
cb_data += $(this).attr("name");
}
});
cb_arr = cb_data.split('|^|');
for(i = 0; i < cb_arr.length; i++){
$("input[name="+ cb_arr[i] +"]").prop("checked", false);
if($("input[name="+ cb_arr[i] +"_o]").val().indexOf('|') > 0){
cb_o_arr = $("input[name="+ cb_arr[i] +"_o]").val().split('|');
$("input[name="+ cb_arr[i] +"]").each(function(){
for(j = 0; j < cb_o_arr.length; j++){
if($(this).val() == cb_o_arr[j]){
$(this).prop("checked", true);return;return false;
}
}
});
} else {
$("input[name="+ cb_arr[i] +"]").each(function(){
if($(this).val() == $("input[name="+ cb_arr[i] +"_o]").val()){
$(this).prop("checked", true);return false;
}
});
}
}
}
qs = '';
search_form.find("input[type=hidden]").each(function(){
if(qs.length > 0){ qs += '&'; }
qs += $(this).attr("name") +'='+ $(this).val();
});
load_contents(page.val(), pagesize.val(), qs, order.val(), table.children("tbody"), path);
});
$(table).find("thead tr span").each(function(){ //정렬 마크업
$(this).append('');
ordo = order.val();
if(ordo.split('_')[0] == $(this).attr("c")){
fa_ordo_class = ordo.split('_')[1].toLowerCase() == 'asc' ? 'up' : 'down';
$(this).children("i").removeClass("fa-sort").addClass("fa-sort-"+ fa_ordo_class);
}
}).on("click",function(){ //클릭 액션
var col = $(this).attr("c"),
ordo = order.val(),
sort = 'asc';
//console.log(ordo);
if (ordo.split('_')[0] == col){
sort = (ordo.split('_')[1].toLowerCase() == 'asc') ? 'desc' : 'asc';
}
var orderby = col+'_'+sort.toUpperCase();
load_contents(page.val(), pagesize.val(), query, orderby, table.children("tbody"), path);
});
//출력수 변경
pagesize.on("change",function(){
page.val(1);
load_contents(page.val(), pagesize.val(), query, order.val(), table.children("tbody"), path);
});
//페이저 맹글기
init_pager(e);
}
function load_contents(p, ps, qs, o, t, url){
var section = searchParent(t, ".sec_list_group"),
url = url +'?'+ qs;
$.ajax({
url: url,
type: 'post',
dataType: 'html',
data: {p:p, ps:ps, ordo:o},
beforeSend: function(){
call_zeno_loader(section, "불러오는 중입니다.");
$("button").prop("disabled", true);
},
success: function(html){
remove_zeno_loader(section);
$("button").prop("disabled", false);
$(t).empty().html( html );
$(section).find("input[name=p]").val(p);
//order mark change
find_art = searchParent(t, "article");
find_art.find("input[name=ordo]").val(o);
find_sort = o.split('_')[0],
find_order = o.split('_')[1];
find_art.find("table thead tr th span").each(function(){
fa_ordo_class = find_order.toLowerCase() == 'asc' ? 'up' : 'down';
cls = ($(this).attr("c") == find_sort) ? 'fa fa-sort-'+ fa_ordo_class +' fs10 ml5' : 'fa fa-sort fs10 ml5';
$(this).children("i").removeClass().addClass(cls);
});
//pagination
init_pager(section);
// button action
if(typeof load_button_action == 'function'){
load_button_action(section); // 버튼과 관련된 게 있음 로딩~
};
},
error: function(){
remove_zeno_loader(section);
$("button").prop("disabled", false);
alert('스크립트 에러');
}
});
}
function init_pager(e){
var pagesize = $(e).find("select[name=ps]").length > 0 ? $(e).find("select[name=ps]") : $(e).find("input[name=ps]");
var int_last_page_num = Math.ceil($(e).find("input[name=tc]").val() / pagesize.val());
var int_now_page_num = $(e).find("input[name=p]").val();
var int_start_page_num, int_end_page_num;
var int_view_page_size = 10;
// calc first, last
if( int_last_page_num < ((int_view_page_size * 1) + 1) ){
int_start_page_num = 1;
int_end_page_num = int_last_page_num;
} else {
int_start_page_num = int_now_page_num;
int_end_page_num = (int_start_page_num * 1) + ((int_view_page_size * 1) - 1);
if (int_last_page_num < int_end_page_num) int_end_page_num = int_last_page_num;
if(int_start_page_num < ((int_view_page_size * 1) - 4)){
int_start_page_num = 1;
int_end_page_num = int_start_page_num + ((int_view_page_size * 1) - 1);
} else if (int_now_page_num > (int_last_page_num - 4)){
int_end_page_num = int_last_page_num;
int_start_page_num = int_end_page_num - ((int_view_page_size * 1) - 1);
} else {
int_start_page_num = int_start_page_num - 4;
int_end_page_num = int_start_page_num + ((int_view_page_size * 1) - 1);
}
}
// make html
var pager_html = '';
pager_html += '';
pager_html += '';
for (i = int_start_page_num; i <= int_end_page_num; i++){
if (i == int_now_page_num){
pager_html += '';
} else {
pager_html += '';
}
}
pager_html += '';
pager_html += '';
// make pagination
var pager = $(e).find(".pagination");
pager.empty();
if ($(e).find("input[name=tc]").val() > 0){pager.html(pager_html);}
// disabled
if (int_now_page_num == 1){
pager.children("button.btn_arrow.first").addClass("disabled");
pager.children("button.btn_arrow.prev").addClass("disabled");
} else if(int_now_page_num == int_last_page_num){
pager.children("button.btn_arrow.next").addClass("disabled");
pager.children("button.btn_arrow.last").addClass("disabled");
}
if (int_last_page_num == 1){
pager.children("button.btn_arrow.next").addClass("disabled");
pager.children("button.btn_arrow.last").addClass("disabled");
}
// button action
pager.children("button:not(.disabled)").on("click",function(){
var page = $(this).attr("p"),
pagesize = $(e).find("select[name=ps]").length > 0 ? $(e).find("select[name=ps]").val() : $(e).find("input[name=ps]").val(),
query = $(e).find("input[name=qs]").val(),
order = $(e).find("input[name=ordo]").val(),
path = $(e).find("input[name=path]").val(),
table = $(e).find("table.tbl_sort_list");
if(page != $("input[name=p]").val()){
load_contents(page, pagesize, query, order, table.children("tbody"), path);
}
});
$(e).find(".tot_article strong").html($(e).find("input[name=tc]").val());
$(e).find(".page_info strong").html(int_now_page_num);
$(e).find(".page_info em").html(int_last_page_num);
}
//목록 새로고침
function reload_basic_list(e){
var page = $(e).find("input[name=p]").val(),
pagesize = $(e).find("select[name=ps]").length > 0 ? $(e).find("select[name=ps]").val() : $(e).find("input[name=ps]").val(),
query = $(e).find("input[name=qs]").val(),
order = $(e).find("input[name=ordo]").val(),
path = $(e).find("input[name=path]").val(),
table = $(e).find("table.tbl_sort_list");
load_contents(page, pagesize, query, order, table.children("tbody"), path);
}