/**
* @author zhixin wen ");
if (i == 0 && !that.options.cardView && that.options.detailView) {
html.push(
sprintf(
' ");
});
this.$header.html(html.join(""));
this.$header.find("th[data-field]").each(function(i) {
$(this).data(visibleColumns[$(this).data("field")]);
});
this.$container
.off("click", ".th-inner")
.on("click", ".th-inner", function(event) {
var target = $(this);
if (target.closest(".bootstrap-table")[0] !== that.$container[0])
return false;
if (that.options.sortable && target.parent().data().sortable) {
that.onSort(event);
}
});
this.$header
.children()
.children()
.off("keypress")
.on("keypress", function(event) {
if (that.options.sortable && $(this).data().sortable) {
var code = event.keyCode || event.which;
if (code == 13) {
//Enter keycode
that.onSort(event);
}
}
});
if (!this.options.showHeader || this.options.cardView) {
this.$header.hide();
this.$tableHeader.hide();
this.$tableLoading.css("top", 0);
} else {
this.$header.show();
this.$tableHeader.show();
this.$tableLoading.css("top", this.$header.outerHeight() + 1);
// Assign the correct sortable arrow
this.getCaret();
}
this.$selectAll = this.$header.find('[name="btSelectAll"]');
this.$selectAll.off("click").on("click", function() {
var checked = $(this).prop("checked");
that[checked ? "checkAll" : "uncheckAll"]();
that.updateSelected();
});
};
BootstrapTable.prototype.initFooter = function() {
if (!this.options.showFooter || this.options.cardView) {
this.$tableFooter.hide();
} else {
this.$tableFooter.show();
}
};
/**
* @param data
* @param type: append / prepend
*/
BootstrapTable.prototype.initData = function(data, type) {
if (type === "append") {
this.data = this.data.concat(data);
} else if (type === "prepend") {
this.data = [].concat(data).concat(this.data);
} else {
this.data = data || this.options.data;
}
// Fix #839 Records deleted when adding new row on filtered table
if (type === "append") {
this.options.data = this.options.data.concat(data);
} else if (type === "prepend") {
this.options.data = [].concat(data).concat(this.options.data);
} else {
this.options.data = this.data;
}
if (this.options.sidePagination === "server") {
return;
}
this.initSort();
};
BootstrapTable.prototype.initSort = function() {
var that = this,
name = this.options.sortName,
order = this.options.sortOrder === "desc" ? -1 : 1,
index = $.inArray(this.options.sortName, this.header.fields);
if (index !== -1) {
this.data.sort(function(a, b) {
if (that.header.sortNames[index]) {
name = that.header.sortNames[index];
}
var aa = getItemField(a, name, that.options.escape),
bb = getItemField(b, name, that.options.escape),
value = calculateObjectValue(
that.header,
that.header.sorters[index], [aa, bb]
);
if (value !== undefined) {
return order * value;
}
// Fix #161: undefined or null string sort bug.
if (aa === undefined || aa === null) {
aa = "";
}
if (bb === undefined || bb === null) {
bb = "";
}
// IF both values are numeric, do a numeric comparison
if ($.isNumeric(aa) && $.isNumeric(bb)) {
// Convert numerical values form string to float.
aa = parseFloat(aa);
bb = parseFloat(bb);
if (aa < bb) {
return order * -1;
}
return order;
}
if (aa === bb) {
return 0;
}
// If value is not a string, convert to string
if (typeof aa !== "string") {
aa = aa.toString();
}
if (aa.localeCompare(bb) === -1) {
return order * -1;
}
return order;
});
}
};
BootstrapTable.prototype.onSort = function(event) {
var $this =
event.type === "keypress" ?
$(event.currentTarget) :
$(event.currentTarget).parent(),
$this_ = this.$header.find("th").eq($this.index());
this.$header.add(this.$header_).find("span.order").remove();
if (this.options.sortName === $this.data("field")) {
this.options.sortOrder =
this.options.sortOrder === "asc" ? "desc" : "asc";
} else {
this.options.sortName = $this.data("field");
this.options.sortOrder = $this.data("order") === "asc" ? "desc" : "asc";
}
this.trigger("sort", this.options.sortName, this.options.sortOrder);
$this.add($this_).data("order", this.options.sortOrder);
// Assign the correct sortable arrow
this.getCaret();
if (this.options.sidePagination === "server") {
this.initServer(this.options.silentSort);
return;
}
this.initSort();
this.initBody();
};
BootstrapTable.prototype.initToolbar = function() {
var that = this,
html = [],
timeoutId = 0,
$keepOpen,
$search,
switchableCount = 0;
if (this.$toolbar.find(".bars").children().length) {
$("body").append($(this.options.toolbar));
}
this.$toolbar.html("");
if (
typeof this.options.toolbar === "string" ||
typeof this.options.toolbar === "object"
) {
$(sprintf('', this.options.toolbarAlign))
.appendTo(this.$toolbar)
.append($(this.options.toolbar));
}
// showColumns, showToggle, showRefresh
html = [
sprintf(
'',
that.options.columns.length
)
);
}
$.each(columns, function(j, column) {
var text = "",
halign = "", // header align style
align = "", // body align style
style = "",
class_ = sprintf(' class="%s"', column["class"]),
order = that.options.sortOrder || column.order,
unitWidth = "px",
width = column.width;
if (column.width !== undefined && !that.options.cardView) {
if (typeof column.width === "string") {
if (column.width.indexOf("%") !== -1) {
unitWidth = "%";
}
}
}
if (column.width && typeof column.width === "string") {
width = column.width.replace("%", "").replace("px", "");
}
halign = sprintf(
"text-align: %s; ",
column.halign ? column.halign : column.align
);
align = sprintf("text-align: %s; ", column.align);
style = sprintf("vertical-align: %s; ", column.valign);
style += sprintf(
"width: %s; ",
(column.checkbox || column.radio) && !width ?
"36px" :
width ?
width + unitWidth :
undefined
);
if (typeof column.fieldIndex !== "undefined") {
that.header.fields[column.fieldIndex] = column.field;
that.header.styles[column.fieldIndex] = align + style;
that.header.classes[column.fieldIndex] = class_;
that.header.formatters[column.fieldIndex] = column.formatter;
that.header.events[column.fieldIndex] = column.events;
that.header.sorters[column.fieldIndex] = column.sorter;
that.header.sortNames[column.fieldIndex] = column.sortName;
that.header.cellStyles[column.fieldIndex] = column.cellStyle;
that.header.searchables[column.fieldIndex] = column.searchable;
if (!column.visible) {
return;
}
if (that.options.cardView && !column.cardVisible) {
return;
}
visibleColumns[column.field] = column;
}
html.push(
" "
);
html.push(
sprintf(
' ");
});
html.push(""
);
if (this.options.cardView) {
html.push(sprintf(' ");
}
// show no records
if (!html.length) {
html.push(
'', this.header.fields.length));
}
if (!this.options.cardView && this.options.detailView) {
html.push(
" ",
'',
sprintf(
'',
this.options.iconsPrefix,
this.options.icons.detailOpen
),
"",
" "
);
}
$.each(this.header.fields, function(j, field) {
var text = "",
value = getItemField(item, field, that.options.escape),
type = "",
cellStyle = {},
id_ = "",
class_ = that.header.classes[j],
data_ = "",
rowspan_ = "",
title_ = "",
column = that.columns[getFieldIndex(that.columns, field)];
if (!column.visible) {
return;
}
style = sprintf(
'style="%s"',
csses.concat(that.header.styles[j]).join("; ")
);
value = calculateObjectValue(
column,
that.header.formatters[j], [value, item, i],
value
);
// handle td's id and class
if (item["_" + field + "_id"]) {
id_ = sprintf(' id="%s"', item["_" + field + "_id"]);
}
if (item["_" + field + "_class"]) {
class_ = sprintf(' class="%s"', item["_" + field + "_class"]);
}
if (item["_" + field + "_rowspan"]) {
rowspan_ = sprintf(' rowspan="%s"', item["_" + field + "_rowspan"]);
}
if (item["_" + field + "_title"]) {
title_ = sprintf(' title="%s"', item["_" + field + "_title"]);
}
cellStyle = calculateObjectValue(
that.header,
that.header.cellStyles[j], [value, item, i],
cellStyle
);
if (cellStyle.classes) {
class_ = sprintf(' class="%s"', cellStyle.classes);
}
if (cellStyle.css) {
var csses_ = [];
for (var key in cellStyle.css) {
csses_.push(key + ": " + cellStyle.css[key]);
}
style = sprintf(
'style="%s"',
csses_.concat(that.header.styles[j]).join("; ")
);
}
if (
item["_" + field + "_data"] &&
!$.isEmptyObject(item["_" + field + "_data"])
) {
$.each(item["_" + field + "_data"], function(k, v) {
// ignore data-index
if (k === "index") {
return;
}
data_ += sprintf(' data-%s="%s"', k, v);
});
}
if (column.checkbox || column.radio) {
type = column.checkbox ? "checkbox" : type;
type = column.radio ? "radio" : type;
text = [
sprintf(
that.options.cardView ?
'',
column["class"] || ""
),
"",
that.header.formatters[j] && typeof value === "string" ? value : "",
that.options.cardView ? "" : " ",
].join("");
item[that.header.stateField] =
value === true || (value && value.checked);
} else {
value =
typeof value === "undefined" || value === null ?
that.options.undefinedText :
value;
text = that.options.cardView ?
[
'",
id_,
class_,
style,
data_,
rowspan_,
title_
),
value,
" ",
].join("");
// Hide empty data on Card view when smartDisplay is set to true.
if (
that.options.cardView &&
that.options.smartDisplay &&
value === ""
) {
// Should set a placeholder for event binding correct fieldIndex
text = '';
}
}
html.push(text);
});
if (this.options.cardView) {
html.push("");
}
html.push("',
sprintf(
' "
);
}
this.$body.html(html.join(""));
if (!fixedScroll) {
this.scrollTo(0);
}
// click to select by column
this.$body
.find("> tr[data-index] > td")
.off("click dblclick")
.on("click dblclick", function(e) {
var $td = $(this),
$tr = $td.parent(),
item = that.data[$tr.data("index")],
index = $td[0].cellIndex,
field =
that.header.fields[
that.options.detailView && !that.options.cardView ?
index - 1 :
index
],
column = that.columns[getFieldIndex(that.columns, field)],
value = getItemField(item, field, that.options.escape);
if ($td.find(".detail-icon").length) {
return;
}
that.trigger(
e.type === "click" ? "click-cell" : "dbl-click-cell",
field,
value,
item,
$td
);
that.trigger(
e.type === "click" ? "click-row" : "dbl-click-row",
item,
$tr
);
// if click to select - then trigger the checkbox/radio click
if (
e.type === "click" &&
that.options.clickToSelect &&
column.clickToSelect
) {
var $selectItem = $tr.find(
sprintf('[name="%s"]', that.options.selectItemName)
);
if ($selectItem.length) {
$selectItem[0].click(); // #144: .trigger('click') bug
}
}
});
this.$body
.find("> tr[data-index] > td > .detail-icon")
.off("click")
.on("click", function() {
var $this = $(this),
$tr = $this.parent().parent(),
index = $tr.data("index"),
row = data[index]; // Fix #980 Detail view, when searching, returns wrong row
// remove and update
if ($tr.next().is("tr.detail-view")) {
$this
.find("i")
.attr(
"class",
sprintf(
"%s %s",
that.options.iconsPrefix,
that.options.icons.detailOpen
)
);
$tr.next().remove();
that.trigger("collapse-row", index, row);
} else {
$this
.find("i")
.attr(
"class",
sprintf(
"%s %s",
that.options.iconsPrefix,
that.options.icons.detailClose
)
);
$tr.after(
sprintf(
'%s ',
this.$header.find("th").length,
this.options.formatNoMatches()
),
" ',
$tr.find("td").length
)
);
var $element = $tr.next().find("td");
var content = calculateObjectValue(
that.options,
that.options.detailFormatter, [index, row, $element],
""
);
if ($element.length === 1) {
$element.append(content);
}
that.trigger("expand-row", index, row, $element);
}
that.resetView();
});
this.$selectItem = this.$body.find(
sprintf('[name="%s"]', this.options.selectItemName)
);
this.$selectItem.off("click").on("click", function(event) {
event.stopImmediatePropagation();
var $this = $(this),
checked = $this.prop("checked"),
row = that.data[$this.data("index")];
if (that.options.maintainSelected && $(this).is(":radio")) {
$.each(that.options.data, function(i, row) {
row[that.header.stateField] = false;
});
}
row[that.header.stateField] = checked;
if (that.options.singleSelect) {
that.$selectItem.not(this).each(function() {
that.data[$(this).data("index")][that.header.stateField] = false;
});
that.$selectItem.filter(":checked").not(this).prop("checked", false);
}
that.updateSelected();
that.trigger(checked ? "check" : "uncheck", row, $this);
});
$.each(this.header.events, function(i, events) {
if (!events) {
return;
}
// fix bug, if events is defined with namespace
if (typeof events === "string") {
events = calculateObjectValue(null, events);
}
var field = that.header.fields[i],
fieldIndex = $.inArray(field, that.getVisibleFields());
if (that.options.detailView && !that.options.cardView) {
fieldIndex += 1;
}
for (var key in events) {
that.$body.find(">tr:not(.no-records-found)").each(function() {
var $tr = $(this),
$td = $tr
.find(that.options.cardView ? ".card-view" : "td")
.eq(fieldIndex),
index = key.indexOf(" "),
name = key.substring(0, index),
el = key.substring(index + 1),
func = events[key];
$td
.find(el)
.off(name)
.on(name, function(e) {
var index = $tr.data("index"),
row = that.data[index],
value = row[field];
func.apply(this, [e, value, row, index]);
});
});
}
});
this.updateSelected();
this.resetView();
this.trigger("post-body");
};
BootstrapTable.prototype.initServer = function(silent, query) {
var that = this,
data = {},
params = {
searchText: this.searchText,
sortName: this.options.sortName,
sortOrder: this.options.sortOrder,
},
request;
if (this.options.pagination) {
params.pageSize =
this.options.pageSize === this.options.formatAllRows() ?
this.options.totalRows :
this.options.pageSize;
params.pageNumber = this.options.pageNumber;
}
if (!this.options.url && !this.options.ajax) {
return;
}
if (this.options.queryParamsType === "limit") {
params = {
search: params.searchText,
sort: params.sortName,
order: params.sortOrder,
};
if (this.options.pagination) {
params.limit =
this.options.pageSize === this.options.formatAllRows() ?
this.options.totalRows :
this.options.pageSize;
params.offset =
this.options.pageSize === this.options.formatAllRows() ?
0 :
this.options.pageSize * (this.options.pageNumber - 1);
}
}
if (!$.isEmptyObject(this.filterColumnsPartial)) {
params["filter"] = JSON.stringify(this.filterColumnsPartial, null);
}
data = calculateObjectValue(
this.options,
this.options.queryParams, [params],
data
);
$.extend(data, query || {});
// false to stop request
if (data === false) {
return;
}
if (!silent) {
this.$tableLoading.show();
}
request = $.extend({},
calculateObjectValue(null, this.options.ajaxOptions), {
type: this.options.method,
url: this.options.url,
data: this.options.contentType === "application/json" &&
this.options.method === "post" ?
JSON.stringify(data) :
data,
cache: this.options.cache,
contentType: this.options.contentType,
dataType: this.options.dataType,
success: function(res) {
res = calculateObjectValue(
that.options,
that.options.responseHandler, [res],
res
);
that.load(res);
that.trigger("load-success", res);
if (!silent) that.$tableLoading.hide();
},
error: function(res) {
that.trigger("load-error", res.status, res);
if (!silent) that.$tableLoading.hide();
},
}
);
if (this.options.ajax) {
calculateObjectValue(this, this.options.ajax, [request], null);
} else {
$.ajax(request);
}
};
BootstrapTable.prototype.initSearchText = function() {
if (this.options.search) {
if (this.options.searchText !== "") {
var $search = this.$toolbar.find(".search input");
$search.val(this.options.searchText);
this.onSearch({ currentTarget: $search });
}
}
};
BootstrapTable.prototype.getCaret = function() {
var that = this;
$.each(this.$header.find("th"), function(i, th) {
$(th)
.find(".sortable")
.removeClass("desc asc")
.addClass(
$(th).data("field") === that.options.sortName ?
that.options.sortOrder :
"both"
);
});
};
BootstrapTable.prototype.updateSelected = function() {
var checkAll =
this.$selectItem.filter(":enabled").length &&
this.$selectItem.filter(":enabled").length ===
this.$selectItem.filter(":enabled").filter(":checked").length;
this.$selectAll.add(this.$selectAll_).prop("checked", checkAll);
this.$selectItem.each(function() {
$(this)
.closest("tr")[$(this).prop("checked") ? "addClass" : "removeClass"]("selected");
});
};
BootstrapTable.prototype.updateRows = function() {
var that = this;
this.$selectItem.each(function() {
that.data[$(this).data("index")][that.header.stateField] = $(this).prop(
"checked"
);
});
};
BootstrapTable.prototype.resetRows = function() {
var that = this;
$.each(this.data, function(i, row) {
that.$selectAll.prop("checked", false);
that.$selectItem.prop("checked", false);
if (that.header.stateField) {
row[that.header.stateField] = false;
}
});
};
BootstrapTable.prototype.trigger = function(name) {
var args = Array.prototype.slice.call(arguments, 1);
name += ".bs.table";
this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
this.$el.trigger($.Event(name), args);
this.options.onAll(name, args);
this.$el.trigger($.Event("all.bs.table"), [name, args]);
};
BootstrapTable.prototype.resetHeader = function() {
// fix #61: the hidden table reset header bug.
// fix bug: get $el.css('width') error sometime (height = 500)
clearTimeout(this.timeoutId_);
this.timeoutId_ = setTimeout(
$.proxy(this.fitHeader, this),
this.$el.is(":hidden") ? 100 : 0
);
};
BootstrapTable.prototype.fitHeader = function() {
var that = this,
fixedBody,
scrollWidth,
focused,
focusedTemp;
if (that.$el.is(":hidden")) {
that.timeoutId_ = setTimeout($.proxy(that.fitHeader, that), 100);
return;
}
fixedBody = this.$tableBody.get(0);
scrollWidth =
fixedBody.scrollWidth > fixedBody.clientWidth &&
fixedBody.scrollHeight >
fixedBody.clientHeight + this.$header.outerHeight() ?
getScrollBarWidth() :
0;
this.$el.css("margin-top", -this.$header.outerHeight());
focused = $(":focus");
if (focused.length > 0) {
var $th = focused.parents("th");
if ($th.length > 0) {
var dataField = $th.attr("data-field");
if (dataField !== undefined) {
var $headerTh = this.$header.find("[data-field='" + dataField + "']");
if ($headerTh.length > 0) {
$headerTh.find(":input").addClass("focus-temp");
}
}
}
}
this.$header_ = this.$header.clone(true, true);
this.$selectAll_ = this.$header_.find('[name="btSelectAll"]');
this.$tableHeader
.css({
"margin-right": scrollWidth,
})
.find("table")
.css("width", this.$el.outerWidth())
.html("")
.attr("class", this.$el.attr("class"))
.append(this.$header_);
focusedTemp = $(".focus-temp:visible:eq(0)");
if (focusedTemp.length > 0) {
focusedTemp.focus();
this.$header.find(".focus-temp").removeClass("focus-temp");
}
// fix bug: $.data() is not working as expected after $.append()
this.$header.find("th[data-field]").each(function(i) {
that.$header_
.find(sprintf('th[data-field="%s"]', $(this).data("field")))
.data($(this).data());
});
var visibleFields = this.getVisibleFields();
this.$body
.find(">tr:first-child:not(.no-records-found) > *")
.each(function(i) {
var $this = $(this),
index = i;
if (that.options.detailView && !that.options.cardView) {
if (i === 0) {
that.$header_
.find("th.detail")
.find(".fht-cell")
.width($this.innerWidth());
}
index = i - 1;
}
that.$header_
.find(sprintf('th[data-field="%s"]', visibleFields[index]))
.find(".fht-cell")
.width($this.innerWidth());
});
// horizontal scroll event
// TODO: it's probably better improving the layout than binding to scroll event
this.$tableBody.off("scroll").on("scroll", function() {
that.$tableHeader.scrollLeft($(this).scrollLeft());
if (that.options.showFooter && !that.options.cardView) {
that.$tableFooter.scrollLeft($(this).scrollLeft());
}
});
that.trigger("post-header");
};
BootstrapTable.prototype.resetFooter = function() {
var that = this,
data = that.getData(),
html = [];
if (!this.options.showFooter || this.options.cardView) {
//do nothing
return;
}
if (!this.options.cardView && this.options.detailView) {
html.push(
' '
);
}
$.each(this.columns, function(i, column) {
var falign = "", // footer align style
style = "",
class_ = sprintf(' class="%s"', column["class"]);
if (!column.visible) {
return;
}
if (that.options.cardView && !column.cardVisible) {
return;
}
falign = sprintf(
"text-align: %s; ",
column.falign ? column.falign : column.align
);
style = sprintf("vertical-align: %s; ", column.valign);
html.push("");
html.push(' ");
});
this.$tableFooter.find("tr").html(html.join(""));
clearTimeout(this.timeoutFooter_);
this.timeoutFooter_ = setTimeout(
$.proxy(this.fitFooter, this),
this.$el.is(":hidden") ? 100 : 0
);
};
BootstrapTable.prototype.fitFooter = function() {
var that = this,
$footerTd,
elWidth,
scrollWidth;
clearTimeout(this.timeoutFooter_);
if (this.$el.is(":hidden")) {
this.timeoutFooter_ = setTimeout($.proxy(this.fitFooter, this), 100);
return;
}
elWidth = this.$el.css("width");
scrollWidth = elWidth > this.$tableBody.width() ? getScrollBarWidth() : 0;
this.$tableFooter
.css({
"margin-right": scrollWidth,
})
.find("table")
.css("width", elWidth)
.attr("class", this.$el.attr("class"));
$footerTd = this.$tableFooter.find("td");
this.$body
.find(">tr:first-child:not(.no-records-found) > *")
.each(function(i) {
var $this = $(this);
$footerTd.eq(i).find(".fht-cell").width($this.innerWidth());
});
};
BootstrapTable.prototype.toggleColumn = function(
index,
checked,
needUpdate
) {
if (index === -1) {
return;
}
this.columns[index].visible = checked;
this.initHeader();
this.initSearch();
this.initPagination();
this.initBody();
if (this.options.showColumns) {
var $items = this.$toolbar
.find(".keep-open input")
.prop("disabled", false);
if (needUpdate) {
$items.filter(sprintf('[value="%s"]', index)).prop("checked", checked);
}
if (
$items.filter(":checked").length <= this.options.minimumCountColumns
) {
$items.filter(":checked").prop("disabled", true);
}
}
};
BootstrapTable.prototype.toggleRow = function(index, uniqueId, visible) {
if (index === -1) {
return;
}
this.$body
.find(
typeof index !== "undefined" ?
sprintf('tr[data-index="%s"]', index) :
sprintf('tr[data-uniqueid="%s"]', uniqueId)
)[visible ? "show" : "hide"]();
};
BootstrapTable.prototype.getVisibleFields = function() {
var that = this,
visibleFields = [];
$.each(this.header.fields, function(j, field) {
var column = that.columns[getFieldIndex(that.columns, field)];
if (!column.visible) {
return;
}
visibleFields.push(field);
});
return visibleFields;
};
// PUBLIC FUNCTION DEFINITION
// =======================
BootstrapTable.prototype.resetView = function(params) {
var padding = 0;
if (params && params.height) {
this.options.height = params.height;
}
this.$selectAll.prop(
"checked",
this.$selectItem.length > 0 &&
this.$selectItem.length === this.$selectItem.filter(":checked").length
);
if (this.options.height) {
var toolbarHeight = getRealHeight(this.$toolbar),
paginationHeight = getRealHeight(this.$pagination),
height = this.options.height - toolbarHeight - paginationHeight;
this.$tableContainer.css("height", height + "px");
}
if (this.options.cardView) {
// remove the element css
this.$el.css("margin-top", "0");
this.$tableContainer.css("padding-bottom", "0");
return;
}
if (this.options.showHeader && this.options.height) {
this.$tableHeader.show();
this.resetHeader();
padding += this.$header.outerHeight();
} else {
this.$tableHeader.hide();
this.trigger("post-header");
}
if (this.options.showFooter) {
this.resetFooter();
if (this.options.height) {
padding += this.$tableFooter.outerHeight() + 1;
}
}
// Assign the correct sortable arrow
this.getCaret();
this.$tableContainer.css("padding-bottom", padding + "px");
this.trigger("reset-view");
};
BootstrapTable.prototype.getData = function(useCurrentPage) {
return this.searchText ||
!$.isEmptyObject(this.filterColumns) ||
!$.isEmptyObject(this.filterColumnsPartial) ?
useCurrentPage ?
this.data.slice(this.pageFrom - 1, this.pageTo) :
this.data :
useCurrentPage ?
this.options.data.slice(this.pageFrom - 1, this.pageTo) :
this.options.data;
};
BootstrapTable.prototype.load = function(data) {
var fixedScroll = false;
// #431: support pagination
if (this.options.sidePagination === "server") {
this.options.totalRows = data.total;
fixedScroll = data.fixedScroll;
data = data[this.options.dataField];
} else if (!$.isArray(data)) {
// support fixedScroll
fixedScroll = data.fixedScroll;
data = data.data;
}
this.initData(data);
this.initSearch();
this.initPagination();
this.initBody(fixedScroll);
};
BootstrapTable.prototype.append = function(data) {
this.initData(data, "append");
this.initSearch();
this.initPagination();
this.initSort();
this.initBody(true);
};
BootstrapTable.prototype.prepend = function(data) {
this.initData(data, "prepend");
this.initSearch();
this.initPagination();
this.initSort();
this.initBody(true);
};
BootstrapTable.prototype.remove = function(params) {
var len = this.options.data.length,
i,
row;
if (!params.hasOwnProperty("field") || !params.hasOwnProperty("values")) {
return;
}
for (i = len - 1; i >= 0; i--) {
row = this.options.data[i];
if (!row.hasOwnProperty(params.field)) {
continue;
}
if ($.inArray(row[params.field], params.values) !== -1) {
this.options.data.splice(i, 1);
}
}
if (len === this.options.data.length) {
return;
}
this.initSearch();
this.initPagination();
this.initSort();
this.initBody(true);
};
BootstrapTable.prototype.removeAll = function() {
if (this.options.data.length > 0) {
this.options.data.splice(0, this.options.data.length);
this.initSearch();
this.initPagination();
this.initBody(true);
}
};
BootstrapTable.prototype.getRowByUniqueId = function(id) {
var uniqueId = this.options.uniqueId,
len = this.options.data.length,
dataRow = null,
i,
row,
rowUniqueId;
for (i = len - 1; i >= 0; i--) {
row = this.options.data[i];
if (row.hasOwnProperty(uniqueId)) {
// uniqueId is a column
rowUniqueId = row[uniqueId];
} else if (row._data.hasOwnProperty(uniqueId)) {
// uniqueId is a row data property
rowUniqueId = row._data[uniqueId];
} else {
continue;
}
if (typeof rowUniqueId === "string") {
id = id.toString();
} else if (typeof rowUniqueId === "number") {
if (Number(rowUniqueId) === rowUniqueId && rowUniqueId % 1 === 0) {
id = parseInt(id);
} else if (rowUniqueId === Number(rowUniqueId) && rowUniqueId !== 0) {
id = parseFloat(id);
}
}
if (rowUniqueId === id) {
dataRow = row;
break;
}
}
return dataRow;
};
BootstrapTable.prototype.removeByUniqueId = function(id) {
var len = this.options.data.length,
row = this.getRowByUniqueId(id);
if (row) {
this.options.data.splice(this.options.data.indexOf(row), 1);
}
if (len === this.options.data.length) {
return;
}
this.initSearch();
this.initPagination();
this.initBody(true);
};
BootstrapTable.prototype.updateByUniqueId = function(params) {
var rowId;
if (!params.hasOwnProperty("id") || !params.hasOwnProperty("row")) {
return;
}
rowId = $.inArray(this.getRowByUniqueId(params.id), this.options.data);
if (rowId === -1) {
return;
}
$.extend(this.data[rowId], params.row);
this.initSort();
this.initBody(true);
};
BootstrapTable.prototype.insertRow = function(params) {
if (!params.hasOwnProperty("index") || !params.hasOwnProperty("row")) {
return;
}
this.data.splice(params.index, 0, params.row);
this.initSearch();
this.initPagination();
this.initSort();
this.initBody(true);
};
BootstrapTable.prototype.updateRow = function(params) {
if (!params.hasOwnProperty("index") || !params.hasOwnProperty("row")) {
return;
}
$.extend(this.data[params.index], params.row);
this.initSort();
this.initBody(true);
};
BootstrapTable.prototype.showRow = function(params) {
if (!params.hasOwnProperty("index") && !params.hasOwnProperty("uniqueId")) {
return;
}
this.toggleRow(params.index, params.uniqueId, true);
};
BootstrapTable.prototype.hideRow = function(params) {
if (!params.hasOwnProperty("index") && !params.hasOwnProperty("uniqueId")) {
return;
}
this.toggleRow(params.index, params.uniqueId, false);
};
BootstrapTable.prototype.getRowsHidden = function(show) {
var rows = $(this.$body[0]).children().filter(":hidden"),
i = 0;
if (show) {
for (; i < rows.length; i++) {
$(rows[i]).show();
}
}
return rows;
};
BootstrapTable.prototype.mergeCells = function(options) {
var row = options.index,
col = $.inArray(options.field, this.getVisibleFields()),
rowspan = options.rowspan || 1,
colspan = options.colspan || 1,
i,
j,
$tr = this.$body.find(">tr"),
$td;
if (this.options.detailView && !this.options.cardView) {
col += 1;
}
$td = $tr.eq(row).find(">td").eq(col);
if (row < 0 || col < 0 || row >= this.data.length) {
return;
}
for (i = row; i < row + rowspan; i++) {
for (j = col; j < col + colspan; j++) {
$tr.eq(i).find(">td").eq(j).hide();
}
}
$td.attr("rowspan", rowspan).attr("colspan", colspan).show();
};
BootstrapTable.prototype.updateCell = function(params) {
if (!params.hasOwnProperty("index") ||
!params.hasOwnProperty("field") ||
!params.hasOwnProperty("value")
) {
return;
}
this.data[params.index][params.field] = params.value;
if (params.reinit === false) {
return;
}
this.initSort();
this.initBody(true);
};
BootstrapTable.prototype.getOptions = function() {
return this.options;
};
BootstrapTable.prototype.getSelections = function() {
var that = this;
return $.grep(this.data, function(row) {
return row[that.header.stateField];
});
};
BootstrapTable.prototype.getAllSelections = function() {
var that = this;
return $.grep(this.options.data, function(row) {
return row[that.header.stateField];
});
};
BootstrapTable.prototype.checkAll = function() {
this.checkAll_(true);
};
BootstrapTable.prototype.uncheckAll = function() {
this.checkAll_(false);
};
BootstrapTable.prototype.checkAll_ = function(checked) {
var rows;
if (!checked) {
rows = this.getSelections();
}
this.$selectAll.add(this.$selectAll_).prop("checked", checked);
this.$selectItem.filter(":enabled").prop("checked", checked);
this.updateRows();
if (checked) {
rows = this.getSelections();
}
this.trigger(checked ? "check-all" : "uncheck-all", rows);
};
BootstrapTable.prototype.check = function(index) {
this.check_(true, index);
};
BootstrapTable.prototype.uncheck = function(index) {
this.check_(false, index);
};
BootstrapTable.prototype.check_ = function(checked, index) {
var $el = this.$selectItem
.filter(sprintf('[data-index="%s"]', index))
.prop("checked", checked);
this.data[index][this.header.stateField] = checked;
this.updateSelected();
this.trigger(checked ? "check" : "uncheck", this.data[index], $el);
};
BootstrapTable.prototype.checkBy = function(obj) {
this.checkBy_(true, obj);
};
BootstrapTable.prototype.uncheckBy = function(obj) {
this.checkBy_(false, obj);
};
BootstrapTable.prototype.checkBy_ = function(checked, obj) {
if (!obj.hasOwnProperty("field") || !obj.hasOwnProperty("values")) {
return;
}
var that = this,
rows = [];
$.each(this.options.data, function(index, row) {
if (!row.hasOwnProperty(obj.field)) {
return false;
}
if ($.inArray(row[obj.field], obj.values) !== -1) {
var $el = that.$selectItem
.filter(":enabled")
.filter(sprintf('[data-index="%s"]', index))
.prop("checked", checked);
row[that.header.stateField] = checked;
rows.push(row);
that.trigger(checked ? "check" : "uncheck", row, $el);
}
});
this.updateSelected();
this.trigger(checked ? "check-some" : "uncheck-some", rows);
};
BootstrapTable.prototype.destroy = function() {
this.$el.insertBefore(this.$container);
$(this.options.toolbar).insertBefore(this.$el);
this.$container.next().remove();
this.$container.remove();
this.$el
.html(this.$el_.html())
.css("margin-top", "0")
.attr("class", this.$el_.attr("class") || ""); // reset the class
};
BootstrapTable.prototype.showLoading = function() {
this.$tableLoading.show();
};
BootstrapTable.prototype.hideLoading = function() {
this.$tableLoading.hide();
};
BootstrapTable.prototype.togglePagination = function() {
this.options.pagination = !this.options.pagination;
var button = this.$toolbar.find('button[name="paginationSwitch"] i');
if (this.options.pagination) {
button.attr(
"class",
this.options.iconsPrefix + " " + this.options.icons.paginationSwitchDown
);
} else {
button.attr(
"class",
this.options.iconsPrefix + " " + this.options.icons.paginationSwitchUp
);
}
this.updatePagination();
};
BootstrapTable.prototype.refresh = function(params) {
if (params && params.url) {
this.options.url = params.url;
this.options.pageNumber = 1;
}
this.initServer(params && params.silent, params && params.query);
};
BootstrapTable.prototype.resetWidth = function() {
if (this.options.showHeader && this.options.height) {
this.fitHeader();
}
if (this.options.showFooter) {
this.fitFooter();
}
};
BootstrapTable.prototype.showColumn = function(field) {
this.toggleColumn(getFieldIndex(this.columns, field), true, true);
};
BootstrapTable.prototype.hideColumn = function(field) {
this.toggleColumn(getFieldIndex(this.columns, field), false, true);
};
BootstrapTable.prototype.getHiddenColumns = function() {
return $.grep(this.columns, function(column) {
return !column.visible;
});
};
BootstrapTable.prototype.filterBy = function(columns) {
this.filterColumns = $.isEmptyObject(columns) ? {} : columns;
this.options.pageNumber = 1;
this.initSearch();
this.updatePagination();
};
BootstrapTable.prototype.scrollTo = function(value) {
if (typeof value === "string") {
value = value === "bottom" ? this.$tableBody[0].scrollHeight : 0;
}
if (typeof value === "number") {
this.$tableBody.scrollTop(value);
}
if (typeof value === "undefined") {
return this.$tableBody.scrollTop();
}
};
BootstrapTable.prototype.getScrollPosition = function() {
return this.scrollTo();
};
BootstrapTable.prototype.selectPage = function(page) {
if (page > 0 && page <= this.options.totalPages) {
this.options.pageNumber = page;
this.updatePagination();
}
};
BootstrapTable.prototype.prevPage = function() {
if (this.options.pageNumber > 1) {
this.options.pageNumber--;
this.updatePagination();
}
};
BootstrapTable.prototype.nextPage = function() {
if (this.options.pageNumber < this.options.totalPages) {
this.options.pageNumber++;
this.updatePagination();
}
};
BootstrapTable.prototype.toggleView = function() {
this.options.cardView = !this.options.cardView;
this.initHeader();
// Fixed remove toolbar when click cardView button.
//that.initToolbar();
this.initBody();
this.trigger("toggle", this.options.cardView);
};
BootstrapTable.prototype.refreshOptions = function(options) {
//If the objects are equivalent then avoid the call of destroy / init methods
if (compareObjects(this.options, options, true)) {
return;
}
this.options = $.extend(this.options, options);
this.trigger("refresh-options", this.options);
this.destroy();
this.init();
};
BootstrapTable.prototype.resetSearch = function(text) {
var $search = this.$toolbar.find(".search input");
$search.val(text || "");
this.onSearch({ currentTarget: $search });
};
BootstrapTable.prototype.expandRow_ = function(expand, index) {
var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', index));
if ($tr.next().is("tr.detail-view") === (expand ? false : true)) {
$tr.find("> td > .detail-icon").click();
}
};
BootstrapTable.prototype.expandRow = function(index) {
this.expandRow_(true, index);
};
BootstrapTable.prototype.collapseRow = function(index) {
this.expandRow_(false, index);
};
BootstrapTable.prototype.expandAllRows = function(isSubTable) {
if (isSubTable) {
var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', 0)),
that = this,
detailIcon = null,
executeInterval = false,
idInterval = -1;
if (!$tr.next().is("tr.detail-view")) {
$tr.find("> td > .detail-icon").click();
executeInterval = true;
} else if (!$tr.next().next().is("tr.detail-view")) {
$tr.next().find(".detail-icon").click();
executeInterval = true;
}
if (executeInterval) {
try {
idInterval = setInterval(function() {
detailIcon = that.$body
.find("tr.detail-view")
.last()
.find(".detail-icon");
if (detailIcon.length > 0) {
detailIcon.click();
} else {
clearInterval(idInterval);
}
}, 1);
} catch (ex) {
clearInterval(idInterval);
}
}
} else {
var trs = this.$body.children();
for (var i = 0; i < trs.length; i++) {
this.expandRow_(true, $(trs[i]).data("index"));
}
}
};
BootstrapTable.prototype.collapseAllRows = function(isSubTable) {
if (isSubTable) {
this.expandRow_(false, 0);
} else {
var trs = this.$body.children();
for (var i = 0; i < trs.length; i++) {
this.expandRow_(false, $(trs[i]).data("index"));
}
}
};
BootstrapTable.prototype.updateFormatText = function(name, text) {
if (this.options[sprintf("format%s", name)]) {
if (typeof text === "string") {
this.options[sprintf("format%s", name)] = function() {
return text;
};
} else if (typeof text === "function") {
this.options[sprintf("format%s", name)] = text;
}
}
this.initToolbar();
this.initPagination();
this.initBody();
};
// BOOTSTRAP TABLE PLUGIN DEFINITION
// =======================
var allowedMethods = [
"getOptions",
"getSelections",
"getAllSelections",
"getData",
"load",
"append",
"prepend",
"remove",
"removeAll",
"insertRow",
"updateRow",
"updateCell",
"updateByUniqueId",
"removeByUniqueId",
"getRowByUniqueId",
"showRow",
"hideRow",
"getRowsHidden",
"mergeCells",
"checkAll",
"uncheckAll",
"check",
"uncheck",
"checkBy",
"uncheckBy",
"refresh",
"resetView",
"resetWidth",
"destroy",
"showLoading",
"hideLoading",
"showColumn",
"hideColumn",
"getHiddenColumns",
"filterBy",
"scrollTo",
"getScrollPosition",
"selectPage",
"prevPage",
"nextPage",
"togglePagination",
"toggleView",
"refreshOptions",
"resetSearch",
"expandRow",
"collapseRow",
"expandAllRows",
"collapseAllRows",
"updateFormatText",
];
$.fn.bootstrapTable = function(option) {
var value,
args = Array.prototype.slice.call(arguments, 1);
this.each(function() {
var $this = $(this),
data = $this.data("bootstrap.table"),
options = $.extend({},
BootstrapTable.DEFAULTS,
$this.data(),
typeof option === "object" && option
);
if (typeof option === "string") {
if ($.inArray(option, allowedMethods) < 0) {
throw new Error("Unknown method: " + option);
}
if (!data) {
return;
}
value = data[option].apply(data, args);
if (option === "destroy") {
$this.removeData("bootstrap.table");
}
}
if (!data) {
$this.data(
"bootstrap.table",
(data = new BootstrapTable(this, options))
);
}
});
return typeof value === "undefined" ? this : value;
};
$.fn.bootstrapTable.Constructor = BootstrapTable;
$.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS;
$.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
$.fn.bootstrapTable.locales = BootstrapTable.LOCALES;
$.fn.bootstrapTable.methods = allowedMethods;
$.fn.bootstrapTable.utils = {
sprintf: sprintf,
getFieldIndex: getFieldIndex,
compareObjects: compareObjects,
calculateObjectValue: calculateObjectValue,
};
// BOOTSTRAP TABLE INIT
// =======================
$(function() {
$('[data-toggle="table"]').bootstrapTable();
});
})(jQuery);