You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
679 lines
28 KiB
679 lines
28 KiB
4 years ago
|
/// <reference path="/Scripts/jquery.1.11.0.min.js" />
|
||
|
/// <reference path="/Scripts/jquery.easyui.min.js" />
|
||
|
/// <reference path="/Scripts/My97DatePicker/WdatePicker.js" />
|
||
|
|
||
|
$.fn.extend({ datagrideditable: function () {
|
||
|
///<summary>
|
||
|
/// easyui表格扩展类
|
||
|
/// 作者:黄顺权
|
||
|
/// 日期:2015年05月05日
|
||
|
///</summary>
|
||
|
|
||
|
if (arguments.length == 0) return;
|
||
|
|
||
|
var command = arguments[0];
|
||
|
|
||
|
if (command == "getValue") {
|
||
|
return $.fn.datagrideditable.getValue(this, arguments[1], arguments[2]);
|
||
|
} else if (command == "setValue") {
|
||
|
return $.fn.datagrideditable.setValue(this, arguments[1], arguments[2], arguments[3]);
|
||
|
} else if (command == "getSelectedIndex") {
|
||
|
|
||
|
} else if (command == "setSelectedIndex") {
|
||
|
} else if (command == "undo") {
|
||
|
return $.fn.datagrideditable.undo(this);
|
||
|
} else if (command == "redo") {
|
||
|
return $.fn.datagrideditable.redo(this);
|
||
|
} else if (command == "editable") {
|
||
|
return $.fn.datagrideditable.editable(table, arguments[1]);
|
||
|
} else if (command == "rowIndexSelected") {
|
||
|
return $.fn.datagrideditable.rowIndexSelected(this);
|
||
|
} else if (command == "appendRow") {
|
||
|
var data = (arguments.length == 2) ? arguments[1] : {};
|
||
|
return $.fn.datagrideditable.appendRow(this, data);
|
||
|
} else if (command == "deleteRow") {
|
||
|
var rowIndex = (arguments.length == 2) ? arguments[1] : null;
|
||
|
return $.fn.datagrideditable.deleteRow(this, rowIndex);
|
||
|
} else if (command == "loadData") {
|
||
|
return this.datagrid("loadData", arguments[1]);
|
||
|
} else if (command == "isValid") {
|
||
|
return $.fn.datagrideditable.isValid(this);
|
||
|
} else if (command == "onCellChange") {
|
||
|
$.fn.datagrideditable.onCellChangeHandlers[this.selector] = arguments[1];
|
||
|
return this;
|
||
|
} else if (command == "getCell") {
|
||
|
return $.fn.datagrideditable.getCell(this, arguments[1], arguments[2]);
|
||
|
} else if (command == "getCellControl") {
|
||
|
return $.fn.datagrideditable.getCellControl(this, arguments[1], arguments[2]);
|
||
|
} else if (arguments.length == 1 && command.columns instanceof Array && command.editable) {
|
||
|
return $.fn.datagrideditable.setOptions(this, arguments[0]);
|
||
|
} else {
|
||
|
return this.datagrid.apply(this, arguments);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
$.fn.datagrideditable.prepareColumn = function (table, col) {
|
||
|
///<summary>准备列的显示样式</summary>
|
||
|
if (col == undefined || col.editor == undefined) return;
|
||
|
var editorType = col.editor.type || col.editor;
|
||
|
if (!col.hasOwnProperty("oldFormatter")
|
||
|
&& col.formatter instanceof Function
|
||
|
&& col.formatter.toString().indexOf("this.oldFormatter") == -1) {
|
||
|
col.oldFormatter = col.formatter;
|
||
|
}
|
||
|
switch (editorType) {
|
||
|
case "checkbox":
|
||
|
col.formatter = function (value, row, index) {
|
||
|
var checked = value ? ' checked' : "";
|
||
|
var html = [];
|
||
|
html.push("<input style=\"text-align:center;\" type=\"checkbox\" " + checked + " value=\"" + value + "\"");
|
||
|
html.push(" tabindex=" + $.fn.datagrideditable.getTableIndex());
|
||
|
html.push(" editorType=\"" + editorType + "\"");
|
||
|
html.push(" onclick=\"$.fn.datagrideditable.UpdateCellValue('" + table.selector + "', this,'" + this.field + "',this.checked);\"");
|
||
|
html.push(" />");
|
||
|
return html.join("");
|
||
|
}
|
||
|
break;
|
||
|
case "combobox":
|
||
|
col.formatter = function (value, row, index) {
|
||
|
var opts = table.datagrid("options");
|
||
|
if (opts.onComboListLoad instanceof Function) {
|
||
|
opts.onComboListLoad.call(this.editor.options, row, col, index);
|
||
|
}
|
||
|
var html = [];
|
||
|
html.push("<select style=\"width:" + (this.boxWidth + 4) + "px;margin-left:-2px;\"");
|
||
|
html.push(" tabindex=" + $.fn.datagrideditable.getTableIndex());
|
||
|
html.push(" editorType=\"" + editorType + "\"");
|
||
|
html.push(" onchange=\"$.fn.datagrideditable.UpdateCellValue('" + table.selector + "', this,'" + this.field + "',this.value);\"");
|
||
|
html.push(">");
|
||
|
if (this.editor.options && this.editor.options.data instanceof Array) {
|
||
|
var editorOptions = this.editor.options;
|
||
|
var arr = editorOptions.data;
|
||
|
for (var j = 0; j < arr.length; j++) {
|
||
|
var optionData = arr[j];
|
||
|
var itemValue = optionData[editorOptions.valueField];
|
||
|
html.push("<option ");
|
||
|
if (value == itemValue) html.push("selected ");
|
||
|
html.push("value='" + itemValue + "'>" + optionData[editorOptions.textField] + "</option>\n");
|
||
|
}
|
||
|
}
|
||
|
html.push("</select>");
|
||
|
return html.join("");
|
||
|
}
|
||
|
break;
|
||
|
case "datebox":
|
||
|
col.formatter = function (value, row, index) {
|
||
|
if (value == undefined) {
|
||
|
value = '';
|
||
|
} else if (typeof (this.oldFormatter) == "function") {
|
||
|
value = this.oldFormatter(value, row, index);
|
||
|
}
|
||
|
if (value == undefined) value = '';
|
||
|
var checkResult = $.fn.datagrideditable.checkValid(value, col.required, col.validType);
|
||
|
var html = [];
|
||
|
html.push("<input value=\"" + value + "\"");
|
||
|
html.push(" tabindex=" + $.fn.datagrideditable.getTableIndex());
|
||
|
html.push(" editorType=\"" + editorType + "\"");
|
||
|
if (col.required) html.push(" required=\"true\"");
|
||
|
if (col.validType) html.push(" validType=\"" + col.validType + "\"");
|
||
|
if (!checkResult.isValid) {
|
||
|
html.push(" class=\"validatebox-invalid\" title=\"" + checkResult.message + "\"");
|
||
|
}
|
||
|
html.push(" style=\"width:" + this.boxWidth + "px;margin-left:-2px;\"");
|
||
|
html.push(" onchange=\"$.fn.datagrideditable.UpdateCellValue('" + table.selector + "', this,'" + this.field + "',this.value);\"");
|
||
|
html.push(" onfocus=\"WdatePicker();\"");
|
||
|
html.push(" />");
|
||
|
return html.join("");
|
||
|
}
|
||
|
break;
|
||
|
case "numberbox":
|
||
|
col.formatter = function (value, row, index) {
|
||
|
if (value == undefined) {
|
||
|
value = '';
|
||
|
} else if (typeof (this.oldFormatter) == "function") {
|
||
|
value = this.oldFormatter(value, row, index);
|
||
|
}
|
||
|
if (value == undefined) value = '';
|
||
|
var checkResult = $.fn.datagrideditable.checkValid(value, col.required, col.validType);
|
||
|
var html = [];
|
||
|
html.push("<input value=\"" + value + "\"");
|
||
|
html.push(" tabindex=" + $.fn.datagrideditable.getTableIndex());
|
||
|
html.push(" editorType=\"" + editorType + "\"");
|
||
|
if (col.required) html.push(" required=\"true\"");
|
||
|
if (col.validType) html.push(" validType=\"" + col.validType + "\"");
|
||
|
if (!checkResult.isValid) {
|
||
|
html.push(" class=\"validatebox-invalid\" title=\"" + checkResult.message + "\"");
|
||
|
}
|
||
|
html.push(" style=\"border:1px solid #7f9db9;width:" + this.boxWidth + "px;margin-left:-2px;\"");
|
||
|
html.push(" onkeydown =\"$.fn.datagrideditable.isNumeric($(this).val(), arguments[0]);\"");
|
||
|
html.push(" onchange=\"$.fn.datagrideditable.UpdateCellValue('" + table.selector + "', this,'" + this.field + "',this.value);\"");
|
||
|
html.push(" />");
|
||
|
return html.join("");
|
||
|
}
|
||
|
break;
|
||
|
case "currencybox":
|
||
|
col.formatter = function (value, row, index) {
|
||
|
if (value == undefined) {
|
||
|
value = '';
|
||
|
} else if (typeof (this.oldFormatter) == "function") {
|
||
|
value = this.oldFormatter(value, row, index);
|
||
|
}
|
||
|
if (value == undefined) value = '';
|
||
|
value = $.fn.datagrideditable.number2Currency(value);
|
||
|
var checkResult = $.fn.datagrideditable.checkValid(value, col.required, col.validType);
|
||
|
var html = [];
|
||
|
html.push("<input value=\"" + value + "\"");
|
||
|
html.push(" tabindex=" + $.fn.datagrideditable.getTableIndex());
|
||
|
html.push(" editorType=\"" + editorType + "\"");
|
||
|
if (col.required) html.push(" required=\"true\"");
|
||
|
if (col.validType) html.push(" validType=\"" + col.validType + "\"");
|
||
|
if (!checkResult.isValid) {
|
||
|
html.push(" class=\"validatebox-invalid\" title=\"" + checkResult.message + "\"");
|
||
|
}
|
||
|
html.push(" style=\"border:1px solid #7f9db9;width:" + this.boxWidth + "px;margin-left:-2px;\"");
|
||
|
html.push(" onkeydown =\"$.fn.datagrideditable.isNumeric($(this).val(), arguments[0]);\"");
|
||
|
html.push(" onblur=\"$.fn.datagrideditable.UpdateCellValue('" + table.selector + "', this,'" + this.field + "',this.value);\"");
|
||
|
html.push(" onfocus=\"this.value=$.fn.datagrideditable.currency2Number(this.value);$.fn.datagrideditable.setCursortSelectAll(this);\"");
|
||
|
html.push(" />");
|
||
|
return html.join("");
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
col.formatter = function (value, row, index) {
|
||
|
if (value == undefined) {
|
||
|
value = '';
|
||
|
} else if (this.oldFormatter instanceof Function && this.oldFormatter.toString().indexOf("this.oldFormatter") == -1) {
|
||
|
value = this.oldFormatter(value, row, index);
|
||
|
}
|
||
|
if (value == undefined) value = '';
|
||
|
var checkResult = $.fn.datagrideditable.checkValid(value, col.required, col.validType);
|
||
|
var html = [];
|
||
|
html.push("<input value=\"" + value + "\"");
|
||
|
html.push(" tabindex=" + $.fn.datagrideditable.getTableIndex());
|
||
|
html.push(" editorType=\"" + editorType + "\"");
|
||
|
if (col.required) html.push(" required=\"true\"");
|
||
|
if (col.validType) html.push(" validType=\"" + col.validType + "\"");
|
||
|
if (!checkResult.isValid) {
|
||
|
html.push(" class=\"validatebox-invalid\" title=\"" + checkResult.message + "\"");
|
||
|
}
|
||
|
html.push(" style=\"border:1px solid #7f9db9;width:" + this.boxWidth + "px;margin-left:-2px;\"");
|
||
|
html.push(" onchange=\"$.fn.datagrideditable.UpdateCellValue('" + table.selector + "', this,'" + this.field + "',this.value);\"");
|
||
|
html.push(" onfocus=\"$.fn.datagrideditable.setCursortSelectAll(this);\"");
|
||
|
html.push(" />");
|
||
|
return html.join("");
|
||
|
};
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.DispacthClickEvent = function () {
|
||
|
///<summary>分派单元格Click事件</summary>
|
||
|
if (this.fireEvent) {
|
||
|
this.parentNode.fireEvent("onclick");
|
||
|
} else {
|
||
|
var evt = document.createEvent('HTMLEvents');
|
||
|
evt.initEvent('click', true, true);
|
||
|
this.parentNode.dispatchEvent(evt);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.checkValid = function (value, required, validType) {
|
||
|
///<summary>检查表格单元格值的合法性</summary>
|
||
|
var isValid = true; //数据合法性
|
||
|
var message = ""; //提示信息
|
||
|
if (required != undefined && required != null && required.toString() == "true" && (value == undefined || value.length == 0)) {
|
||
|
message = "该项数据不能为空!";
|
||
|
isValid = false;
|
||
|
}
|
||
|
if (isValid && typeof (validType) == "string") {
|
||
|
var rules = $.fn.validatebox.defaults.rules;
|
||
|
var ruleName = validType; //规则名称
|
||
|
var ruleParameters = null; //规则参数
|
||
|
|
||
|
var pos = validType.indexOf("["); //判断验证表达式中是否包含数组
|
||
|
if (pos != -1) { //如果包含
|
||
|
ruleName = validType.substr(0, pos); //修正规则名称
|
||
|
ruleParameters = eval("(" + validType.substr(pos) + ")"); //规则参数
|
||
|
}
|
||
|
|
||
|
//调用验证方法验证数据合法性
|
||
|
isValid = rules[ruleName].validator(value, ruleParameters);
|
||
|
if (!isValid) { //数据非法
|
||
|
message = rules[ruleName].message;
|
||
|
if (message) {
|
||
|
if (ruleParameters instanceof Array) { //替换提示信息中的占位符
|
||
|
for (var i = 0; i < ruleParameters.length; i++) {
|
||
|
message = message.replace("{0}", ruleParameters[i]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return { isValid: isValid, message: message };
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.UpdateCellValue = function (tblId, obj, fieldName, value) {
|
||
|
///<summary>修改表格单元格值</summary>
|
||
|
var editorType = obj.getAttribute("editorType");
|
||
|
if (editorType == "currencybox" && (isNaN(value) || value.length == 0)) value = 0;
|
||
|
var checkResult = $.fn.datagrideditable.checkValid(value, obj.getAttribute("required"), obj.getAttribute("validType"));
|
||
|
if (checkResult.isValid) {
|
||
|
$(obj).removeClass("validatebox-invalid");
|
||
|
obj.title = "";
|
||
|
} else {
|
||
|
obj.title = checkResult.message;
|
||
|
$(obj).addClass("validatebox-invalid");
|
||
|
}
|
||
|
|
||
|
//刷新数据。
|
||
|
var rowIndex = $(obj).parents("tr")[0].rowIndex;
|
||
|
var table = $(tblId);
|
||
|
var rows = table.datagrid("getRows");
|
||
|
rows[rowIndex][fieldName] = value;
|
||
|
$.fn.datagrideditable.cacheManager.update(tblId, table.datagrid("getData"));
|
||
|
|
||
|
//格式化数据
|
||
|
if (editorType == "currencybox") {
|
||
|
obj.value = $.fn.datagrideditable.number2Currency(obj.value);
|
||
|
}
|
||
|
|
||
|
//调用事件处理方法
|
||
|
if ($.fn.datagrideditable.onCellChangeHandlers[table.selector] instanceof Function) {
|
||
|
$.fn.datagrideditable.onCellChangeHandlers[table.selector].call(table, obj, rowIndex, fieldName, value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.getCursortPosition = function (ctrl) {
|
||
|
///<summary>获取光标位置函数</summary>
|
||
|
if (ctrl.value.length == 0) return 0;
|
||
|
var caretPos = 0;
|
||
|
if (document.selection) { // IE Support
|
||
|
var sel = document.selection.createRange();
|
||
|
sel.moveStart('character', -ctrl.value.length);
|
||
|
caretPos = sel.text.length;
|
||
|
}
|
||
|
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
|
||
|
caretPos = ctrl.selectionStart;
|
||
|
return caretPos;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.setCursortSelectAll = function (ctrl) {
|
||
|
///<summary>设置光标全选函数</summary>
|
||
|
if (ctrl.value.length == 0) return;
|
||
|
if (document.selection) { // IE Support
|
||
|
var sel = document.selection.createRange();
|
||
|
//sel.collapse();
|
||
|
sel.moveStart('character', 0);
|
||
|
sel.moveEnd('character', ctrl.value.length);
|
||
|
sel.select();
|
||
|
}
|
||
|
else if (ctrl.selectionStart) {
|
||
|
ctrl.selectionStart = 0;
|
||
|
ctrl.selectionEnd = ctrl.value.length;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.onCellKeyDown = function (table, e) {
|
||
|
///<summary>单元格按键事件处理函数</summary>
|
||
|
var elementTagName = e.target.tagName;
|
||
|
if (elementTagName != "INPUT" && elementTagName != "SELECT") return;
|
||
|
var cell = $(e.target).parents("td");
|
||
|
var row = cell.parents("tr");
|
||
|
var colIndex = cell[0].cellIndex;
|
||
|
var rowIndex = row[0].rowIndex;
|
||
|
|
||
|
var oldRowIndex = rowIndex;
|
||
|
var oldColIndex = colIndex;
|
||
|
if (e.keyCode == 37) { //向左光标键
|
||
|
var pos = $.fn.datagrideditable.getCursortPosition(document.activeElement);
|
||
|
if (pos == 0) colIndex--;
|
||
|
} else if (e.keyCode == 39) { //向右光标键
|
||
|
var pos = $.fn.datagrideditable.getCursortPosition(document.activeElement);
|
||
|
if (elementTagName == "INPUT") {
|
||
|
if (pos == e.target.value.length || e.target.type == "checkbox") colIndex++;
|
||
|
} else {
|
||
|
colIndex++;
|
||
|
}
|
||
|
} else if (e.keyCode == 38) { //向上光标键
|
||
|
if (elementTagName == "SELECT") {
|
||
|
if (e.target.selectedIndex == 0) rowIndex--;
|
||
|
} else {
|
||
|
rowIndex--;
|
||
|
}
|
||
|
} else if (e.keyCode == 40) { //向下光标键
|
||
|
if (elementTagName == "SELECT") {
|
||
|
if (e.target.selectedIndex == e.target.options.length - 1) rowIndex++;
|
||
|
} else {
|
||
|
rowIndex++;
|
||
|
}
|
||
|
}
|
||
|
if (rowIndex != oldRowIndex || colIndex != oldColIndex) {
|
||
|
var rowCount = table.datagrid("getRows").length;
|
||
|
var colCount = table.datagrid("getColumnFields").length + table.datagrid("getColumnFields", true).length;
|
||
|
if (rowIndex < 0 || rowIndex > rowCount - 1 || colIndex < 0 || colIndex > colCount - 1) {
|
||
|
return true;
|
||
|
}
|
||
|
var viewIndex = cell.parents("div .datagrid-view1").length > 0 ? 0 : 1;
|
||
|
var cell = table.parent().find(".datagrid-body:eq(" + viewIndex + ") table")[0].rows[rowIndex].cells[colIndex];
|
||
|
var jqCell = $(cell);
|
||
|
jqCell.find("input").focus();
|
||
|
jqCell.find("select").focus();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.cacheManager = new function () {
|
||
|
///<summary>缓存管理对象</summary>
|
||
|
var _cache = {};
|
||
|
|
||
|
var getCache = function (key) {
|
||
|
if (!_cache.hasOwnProperty(key)) {
|
||
|
_cache[key] = { cursor: 0, values: [] };
|
||
|
var data = $(key).datagrid("getData");
|
||
|
_cache[key].values.push($.extend(true, {}, data));
|
||
|
}
|
||
|
return _cache[key];
|
||
|
}
|
||
|
|
||
|
this.init = function (key, data) {
|
||
|
if (_cache.hasOwnProperty(key)) return;
|
||
|
_cache[key] = { cursor: 0, values: [] };
|
||
|
_cache[key].values.push($.extend(true, {}, data));
|
||
|
}
|
||
|
|
||
|
this.update = function (key, data) {
|
||
|
var cache = getCache(key);
|
||
|
cache.values[cache.cursor] = $.extend(true, {}, data);
|
||
|
}
|
||
|
|
||
|
this.add = function (key, data) {
|
||
|
var cache = getCache(key);
|
||
|
cache.values.push($.extend(true, {}, data));
|
||
|
cache.cursor = cache.values.length - 1;
|
||
|
};
|
||
|
|
||
|
this.remove = function (key) {
|
||
|
delete _cache[key];
|
||
|
}
|
||
|
|
||
|
this.undo = function (key) {
|
||
|
var cache = getCache(key);
|
||
|
if (cache.cursor <= 0) return null;
|
||
|
cache.cursor--;
|
||
|
return cache.values[cache.cursor];
|
||
|
}
|
||
|
|
||
|
this.redo = function (key) {
|
||
|
var cache = getCache(key);
|
||
|
if (cache.cursor >= cache.values.length) return null;
|
||
|
cache.cursor++;
|
||
|
return cache.values[cache.cursor];
|
||
|
}
|
||
|
};
|
||
|
|
||
|
$.fn.datagrideditable.isNumeric = function (value, e) {
|
||
|
///<summary>检查数字合法性</summary>
|
||
|
var result = false;
|
||
|
var key = event ? event.keyCode : e.which;
|
||
|
|
||
|
if (((key == 109 || key == 189) && value.length == 0) //第一个字符为负号
|
||
|
|| (key > 95 && key < 106) //小键盘上的0到9
|
||
|
|| (key > 47 && key < 60) //大键盘上的0到9
|
||
|
|| (key == 110 && value.indexOf(".") < 0) //小键盘上的.而且以前没有输入.
|
||
|
|| (key == 190 && value.indexOf(".") < 0) //大键盘上的.而且以前没有输入.
|
||
|
|| key == 8 || key == 9 || key == 46 || key == 13
|
||
|
|| (key >= 33 && key <= 40) //光标位置控制键
|
||
|
|| (key >= 112 && key <= 121) //功能键
|
||
|
|| (event || e).ctrlKey
|
||
|
) {
|
||
|
result = true;
|
||
|
} else {
|
||
|
if (window.event) //IE
|
||
|
{
|
||
|
event.returnValue = false;
|
||
|
}
|
||
|
else //Firefox
|
||
|
{
|
||
|
e.preventDefault();
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.getValue = function (table, rowIndex, fieldName) {
|
||
|
///<summary>获得单元格的值</summary>
|
||
|
fieldName = $.fn.datagrideditable.getFieldName(table, fieldName);
|
||
|
var rows = table.datagrid("getRows");
|
||
|
return rows[rowIndex][fieldName];
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.setValue = function (table, rowIndex, fieldName, value) {
|
||
|
///<summary>设置单元格的值</summary>
|
||
|
fieldName = $.fn.datagrideditable.getFieldName(table, fieldName);
|
||
|
var rows = table.datagrid("getRows");
|
||
|
rows[rowIndex][fieldName] = value;
|
||
|
|
||
|
var cell = table.parent().find(".datagrid-body td[field='" + fieldName + "']:eq(" + rowIndex + ")");
|
||
|
var element = cell.find("input,select");
|
||
|
if (element.length == 0) element = cell.find("div:last");
|
||
|
if (element.length == 0) return;
|
||
|
var tagName = element.prop("tagName");
|
||
|
if (tagName == "INPUT") {
|
||
|
if (element.attr("type") == "checkbox") {
|
||
|
element.prop("checked", value);
|
||
|
} else {
|
||
|
element.val(value);
|
||
|
}
|
||
|
} else if (tagName == 'SELECT') {
|
||
|
element.find("option[value='" + value + "']").attr("selected", true);
|
||
|
} else if (tagName == 'DIV') {
|
||
|
element.text(value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.undo = function (table) {
|
||
|
///<summary>撤销操作</summary>
|
||
|
if (!table.datagrid("options").editable) return;
|
||
|
var rows = $.fn.datagrideditable.cacheManager.undo(table.selector);
|
||
|
if (rows == null) return;
|
||
|
table.datagrid("loadData", rows);
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.redo = function (table) {
|
||
|
///<summary>恢复操作</summary>
|
||
|
if (!table.datagrid("options").editable) return;
|
||
|
var rows = $.fn.datagrideditable.cacheManager.redo(table.selector);
|
||
|
if (rows == null) return;
|
||
|
table.datagrid("loadData", rows);
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.rowIndexSelected = function (table) {
|
||
|
///<summary>获得表格被选择的行</summary>
|
||
|
var arr = [];
|
||
|
var rowSelected = table.parent().find(".datagrid-body-inner .datagrid-row-checked");
|
||
|
for (var i = 0; i < rowSelected.length; i++) {
|
||
|
arr.push(rowSelected[i].rowIndex);
|
||
|
}
|
||
|
return arr;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.editable = function (table, isEditable) {
|
||
|
///<summary>设置表格为是否可编辑,或者获得表格是否为编辑</summary>
|
||
|
var options = table.datagrid("options");
|
||
|
if (isEditable == undefined) {
|
||
|
return options.editable != undefined && options.editable.toString() == "true";
|
||
|
}
|
||
|
var data = table.datagrid("getData");
|
||
|
data = $.extend(true, {}, data); //克隆对象
|
||
|
options.editable = isEditable;
|
||
|
var columns = $.fn.datagrideditable.getColumns(options);
|
||
|
for (var i = 0; i < columns.length; i++) {
|
||
|
columns[i].formatter = columns[i].oldFormatter;
|
||
|
delete columns[i].oldFormatter;
|
||
|
}
|
||
|
if (options.hasOwnProperty("url")) {
|
||
|
options.oldUrl = options.url;
|
||
|
delete options.url;
|
||
|
}
|
||
|
table.datagrideditable(options);
|
||
|
table.datagrideditable("loadData", data);
|
||
|
|
||
|
if (options.hasOwnProperty("oldUrl")) {
|
||
|
options.url = options.oldUrl;
|
||
|
delete options.oldUrl;
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.appendRow = function (table, data) {
|
||
|
///<summary>添加数据行</summary>
|
||
|
if (!table.datagrid("options").editable) return;
|
||
|
data = data || {};
|
||
|
var result = table.datagrid("appendRow", data);
|
||
|
$.fn.datagrideditable.cacheManager.add(table.selector, table.datagrid("getData"));
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.deleteRow = function (table, rowIndex) {
|
||
|
///<summary>删除数据行</summary>
|
||
|
if (!table.datagrid("options").editable) return;
|
||
|
var rowIndexArraySelected;
|
||
|
if (rowIndex instanceof Array) {
|
||
|
rowIndexArraySelected = rowIndex;
|
||
|
} else if (!isNaN(rowIndex)) {
|
||
|
rowIndexArraySelected = [rowIndex];
|
||
|
} else {
|
||
|
rowIndexArraySelected = table.datagrideditable("rowIndexSelected");
|
||
|
}
|
||
|
for (var i = rowIndexArraySelected.length - 1; i >= 0; i--) {
|
||
|
table.datagrid("deleteRow", rowIndexArraySelected[i]);
|
||
|
}
|
||
|
table.datagrid("clearSelections");
|
||
|
$.fn.datagrideditable.cacheManager.add(table.selector, table.datagrid("getData"));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.setOptions = function (table, options) {
|
||
|
///<summary>设置表格配置</summary>
|
||
|
|
||
|
var columns = $.fn.datagrideditable.getColumns(options);
|
||
|
for (var i = 0; i < columns.length; i++) {
|
||
|
$.fn.datagrideditable.prepareColumn(table, columns[i]);
|
||
|
}
|
||
|
options.onResizeColumn = function (field, width) {
|
||
|
var column = $(this).datagrid("getColumnOption", field);
|
||
|
if (typeof (column.editor) == "undefined") return;
|
||
|
var editorType = column.editor.type || column.editor;
|
||
|
if (editorType != "checkbox") {
|
||
|
var cells;
|
||
|
if (editorType == "combobox") {
|
||
|
cells = $(this).parent().find(".datagrid-body ." + column.cellClass + " select")
|
||
|
cells.css("width", (column.boxWidth + 4) + "px");
|
||
|
} else {
|
||
|
cells = $(this).parent().find(".datagrid-body ." + column.cellClass + " input")
|
||
|
cells.css("width", column.boxWidth);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
options.onSelect = function (rowIndex, rowData) {
|
||
|
$(this).datagrid('unselectRow', rowIndex);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
options.checkOnSelect = false;
|
||
|
options.selectOnCheck = false;
|
||
|
|
||
|
var result = table.datagrid(options);
|
||
|
table.datagrid("options").view.onAfterRender = function (target, rows) {
|
||
|
$.fn.datagrideditable.cacheManager.init(table.selector, table.datagrid("getData"));
|
||
|
table.parent().find(".datagrid-body:eq(1)").keydown(function (e) {
|
||
|
return $.fn.datagrideditable.onCellKeyDown(table, e);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
//$.parser.parse(this);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.isValid = function (table) {
|
||
|
///<summary>判断单元格的值是否合法</summary>
|
||
|
return table.parent().find(".datagrid-body .validatebox-invalid:first").length == 0;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.currency2Number = function (num) {
|
||
|
///<summary>货币转换为数字</summary>
|
||
|
num = num.replace(/\,/g, '');
|
||
|
var pos = num.indexOf(".");
|
||
|
if (pos == -1) return num;
|
||
|
var decPart = parseInt(num.substring(pos + 1));
|
||
|
if (decPart == 0) {
|
||
|
num = num.substr(0, num.length - 3);
|
||
|
}
|
||
|
return num;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.number2Currency = function (num) {
|
||
|
///<summary>数字转换为货币</summary>
|
||
|
if (isNaN(num)) num = "0";
|
||
|
var sign = num < 0 ? "-" : "";
|
||
|
num = Math.floor(num * 100 + 0.50000000001);
|
||
|
var decPart = num % 100;
|
||
|
if (decPart < 10) decPart = "0" + decPart;
|
||
|
var intPart = Math.floor(num / 100).toString().split("").reverse();
|
||
|
var result = "." + decPart;
|
||
|
for (var i = 0; i < intPart.length; i++) {
|
||
|
if (i > 0 && i < intPart.length - 1 && i % 3 == 0) {
|
||
|
result = "," + result;
|
||
|
}
|
||
|
result = intPart[i] + result;
|
||
|
}
|
||
|
return sign + result;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.getColumns = function (options) {
|
||
|
///<summary>获得表格列</summary>
|
||
|
return options.frozenColumns instanceof Array ? options.frozenColumns[0].concat(options.columns[0]) : options.columns[0];
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.getFieldName = function (table, fieldName) {
|
||
|
///<summary>获得表格字段名称</summary>
|
||
|
if (typeof (fieldName) == "number") {
|
||
|
var options = table.datagrid("options");
|
||
|
var columns = $.fn.datagrideditable.getColumns(options);
|
||
|
return columns[fieldName].field;
|
||
|
} else {
|
||
|
return fieldName;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.getCell = function (table, rowIndex, fieldName) {
|
||
|
fieldName = $.fn.datagrideditable.getFieldName(table, fieldName);
|
||
|
return table.parent().find(".datagrid-body td[field='" + fieldName + "']:eq(" + rowIndex + ")");
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.getCellControl = function (table, rowIndex, fieldName) {
|
||
|
fieldName = $.fn.datagrideditable.getFieldName(table, fieldName);
|
||
|
var cell = table.parent().find(".datagrid-body td[field='" + fieldName + "']:eq(" + rowIndex + ")");
|
||
|
var element = cell.find("input,select");
|
||
|
if (element != null && element.length > 0) {
|
||
|
return element[0];
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable.getSelectedIndex = function (table, rowIndex, fieldName) {
|
||
|
}
|
||
|
|
||
|
$.fn.datagrideditable._tabIndex = 10000;
|
||
|
$.fn.datagrideditable.getTableIndex = function () {
|
||
|
///<summary>获得TAB索引</summary>
|
||
|
$.fn.datagrideditable._tabIndex++;
|
||
|
return $.fn.datagrideditable._tabIndex;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
$.fn.datagrideditable.onCellChangeHandlers = {};
|