-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjQuery.jqGrid.dynamicLink.js
100 lines (94 loc) · 4.15 KB
/
jQuery.jqGrid.dynamicLink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Copyright (c) 2012, Dr. Oleg Kiriljuk, oleg.kiriljuk@ok-soft-gmbh.com
* Dual licensed under the MIT and GPL licenses
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
* Date: 2012-02-17
*/
/*global jQuery */
(function ($) {
'use strict';
/*jslint unparam: true */
$.extend($.fn.fmatter, {
/* supported formatoptions:
* url: as string or function. Default value: '#'
* target: as string or function
* cellValue: as string or function. Dafault value is the cell contain
* attr: string. The value of the attr property must be a plain object which properties
* defines additional attributes like "class" or "title" which can be set
* on the "<a>" element.
* unformat: as function. Defines the value returned by the unformatter. The unformatter
* are called by different standard method like getRowData, getCell, getCol.
*/
dynamicLink: function (cellValue, options, rowData) {
// href, target, rel, title, onclick
// other attributes like media, hreflang, type are not supported currently
var op = {url: '#'}, attr, attrName, attrValue, attrStr = '';
if (typeof options.colModel.formatoptions !== 'undefined') {
op = $.extend({}, op, options.colModel.formatoptions);
}
if ($.isFunction(op.target)) {
op.target = op.target.call(this, cellValue, options.rowId, rowData, op);
}
if ($.isFunction(op.url)) {
op.url = op.url.call(this, cellValue, options.rowId, rowData, op);
}
if ($.isFunction(op.cellValue)) {
cellValue = op.cellValue.call(this, cellValue, options.rowId, rowData, op);
}
attr = op.attr;
if ($.isPlainObject(attr)) {
// enumerate properties of
for (attrName in attr) {
if (attr.hasOwnProperty(attrName)) {
if ($.isFunction(attr[attrName])) {
attrStr += ' ' + attrName + '="' + attr[attrName].call(this, cellValue, options.rowId, rowData, op) + '"';
} else {
attrStr += ' ' + attrName + '="' + attr[attrName] + '"';
}
}
}
}
if ($.fmatter.isString(cellValue) || $.fmatter.isNumber(cellValue)) {
return '<a' +
(op.target ? ' target=' + op.target : '') +
(op.onClick ? ' onclick="return $.fn.fmatter.dynamicLink.onClick.call(this, arguments[0]);"' : '') +
' href="' + op.url + '"' + attrStr + '>' +
(cellValue || ' ') + '</a>';
} else {
return ' ';
}
}
});
$.extend($.fn.fmatter.dynamicLink, {
unformat: function (cellValue, options, elem) {
var op = {}, text;
if (typeof options.colModel.formatoptions !== 'undefined') {
op = $.extend({}, op, options.colModel.formatoptions);
}
if ($.isFunction(op.unformat)) {
return op.unformat.call(this, cellValue, elem, op);
}
text = $(elem).text();
return text === ' ' ? '' : text;
},
onClick: function (e) {
var $cell = $(this).closest('td'),
$row = $cell.closest('tr.jqgrow'),
$grid = $row.closest('table.ui-jqgrid-btable'),
p,
colModel,
iCol;
if ($grid.length === 1) {
p = $grid[0].p;
if (p) {
iCol = $.jgrid.getCellIndex($cell[0]);
colModel = p.colModel;
colModel[iCol].formatoptions.onClick.call($grid[0],
$row.attr('id'), $row[0].rowIndex, iCol, $cell.text(), e);
}
}
return false;
}
});
}(jQuery));