(
function
($) {
$.fn.formJSON =
function
() {
var
serializeObj = {};
var
array =
this
.serializeArray();
var
str =
this
.serialize();
$(array).each(
function
() {
if
(serializeObj[
this
.name]) {
if
($.isArray(serializeObj[
this
.name])) {
serializeObj[
this
.name].push(
this
.value);
}
else
{
serializeObj[
this
.name] = [
serializeObj[
this
.name],
this
.value ];
}
}
else
{
serializeObj[
this
.name] =
this
.value;
}
});
return
serializeObj;
};
})(jQuery);
function
isEmpty(str) {
return
(str ==
null
|| $.trim(str).length == 0);
}
function
query_dataTable(options) {
var
data_table_object;
var
url = options.url ? options.url :
"自定义默认url"
;
var
table = options.table;
var
query = options.query ? options.query :
"#queryParam"
;
var
table_class = $(table).attr(
"class"
);
var
hidden_title = options.hidden_title ? options.hidden_title :
null
;
var
complete =
function
() {
if
(options.complete)
options.complete();
}
var
columns = [];
if
(isEmpty(table_class)) {
$(table).removeClass(
"table table-striped table-hover table-bordered"
);
$(table).addClass(
"table table-striped table-hover table-bordered"
);
}
var
data_options = $(table).attr(
"data-column"
);
var
bSort = options.sort ? options.sort :
false
;
data_options = JSON.parse(data_options);
var
selectedType = options.selectType ? options.selectType :
"checkbox"
;
var
dis = (selectedType ==
"radio"
) ?
"disabled"
:
""
;
if
(options.selected) {
columns.push({
"mDataProp"
:
""
,
"sTitle"
:
"<input title='全选/反选' type='"
+ selectedType +
"' "
+ dis +
" name='bootstarp_data_table_checkbox'>"
,
"sClass"
:
"left selected"
,
"bVisible"
:
true
,
"sWidth"
:
"2%"
,
"bSortable"
:
false
,
"fnRender"
:
function
() {
return
"<input title='选择' type='"
+ selectedType
+
"' name='bootstarp_data_table_checkbox'>"
;
}
});
}
var
displayLen = data_options.length;
$.each(data_options,
function
() {
var
visible =
this
[
"visible"
];
if
(visible)
displayLen--;
});
$.each(data_options,
function
(index, td) {
var
sClass = td.Class ? td.Class :
"center"
;
var
sTitle = td.title ? td.title :
""
;
var
bVisible = td[
"visible"
] ?
false
:
true
;
var
sWidth = td.width ? td.width : (100 / displayLen) +
"%"
;
var
bSortable = td.sort ? td.sort : bSort;
var
column = {
"mDataProp"
: td.name,
"sTitle"
: sTitle,
"sClass"
: sClass,
"bVisible"
: bVisible,
"sWidth"
: sWidth,
"bSortable"
: bSortable
};
if
(td.out) {
column[
"fnRender"
] =
function
(row, key) {
return
td.out(key, row.aData);
}
}
if
(td.render) {
column[
"fnRender"
] = td.render;
}
columns.push(column);
});
function
success(json) {
}
var
successFunc = options.success ? options.success : success;
function
datatable_callback(url, queryParam, callback, oSettings) {
queryParam = $(query).formJSON();
queryParam[
"PAGE_INFO.currentPage"
] = oSettings._iDisplayStart;
$.ajax({
type :
'POST'
,
dataType :
'json'
,
cache :
false
,
url : url,
data : queryParam,
success :
function
(json) {
json[
"sEcho"
] = oSettings._sEcho | oSettings.iDraw;
if
(options.success)
options.success(json);
callback(json);
},
error:
function
(e){
}
});
}
var
data_table_load = options.load ? options.load : datatable_callback;
data_table_object = $(table).dataTable({
"bFilter"
:
false
,
"bAutoWidth"
:
false
,
"sPaginationType"
:
"bootstrap"
,
"bLengthChange"
:
false
,
"bDestroy"
:
true
,
"bProcessing"
:
false
,
"sAjaxSource"
: url,
"fnServerData"
: data_table_load,
"bServerSide"
:
false
,
"bSort"
: bSort,
"aoColumns"
: columns,
"fnInitComplete"
: complete,
"oLanguage"
: {
"sProcessing"
:
"数据获取中..."
,
"sLengthMenu"
:
"_MENU_ 记录/页"
,
"sZeroRecords"
:
"没有匹配的记录"
,
"sInfo"
:
"显示第 _START_ 至 _END_ 条记录,共 _TOTAL_ 条"
,
"sInfoEmpty"
:
"显示第 0 至 0 条记录,共 0条"
,
"sInfoFiltered"
:
"(由 _MAX_ 条记录过滤)"
,
"sInfoPostFix"
:
""
,
"oPaginate"
: {
"sFirst"
:
"首页"
,
"sPrevious"
:
"上页"
,
"sNext"
:
"下页"
,
"sLast"
:
"末页"
}
}
});
if
(options.selected) {
$(
'tbody'
, $(table))
.on(
'click'
,
'td'
,
function
() {
if
($(
this
)
.find(
":input[name='bootstarp_data_table_checkbox']"
).length < 1) {
var
box = $(
this
)
.parent()
.find(
":input[name='bootstarp_data_table_checkbox']"
);
box.prop(
"checked"
, !box.is(
":checked"
));
}
if
(options.selectCall) {
var
selecteds = data_table_object.selectedAll();
options.selectCall(selecteds);
}
});
$(
'thead'
, $(table)).find(
":input[name='bootstarp_data_table_checkbox']"
).on(
"click"
,
function
() {
$(
'tbody'
, $(table)).find(
":input[name='bootstarp_data_table_checkbox']"
)
.prop(
"checked"
, $(
this
).is(
":checked"
));
if
(options.selectCall) {
var
selecteds = data_table_object.selectedAll();
options.selectCall(selecteds);
}
});
data_table_object.selectedAll =
function
() {
var
selected = [];
$.each(
this
.fnGetNodes(),
function
() {
var
rows =
this
;
var
checked = $(
this
).find(
":input[name='bootstarp_data_table_checkbox']"
).is(
":checked"
);
if
(checked)
selected.push(data_table_object.fnGetData(rows));
});
return
selected;
};
}
$(table).prev().hide();
if
(hidden_title) {
$(table).find(
"tr:eq(0)"
).hide();
}
return
data_table_object;
}