-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.edittable.js
159 lines (157 loc) · 4.8 KB
/
jquery.edittable.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
(function($){
/*
* Editable
* type : text,textarea,password,select
* type='select' : $(".d").editable({type:"select",options:{'选项1':'值1','选21':'值2','选项3':'值3'}});
* type='text' : $(".d").editable({editBy:"dbclick",type:"textarea",onSubmit:function(data){ alert(data.current)}});
*
*/
$.fn.editable = function(options){
var defaults = {
onEdit: null,//编辑的时候执行
onSubmit: null,//触发submitBy条件后执行
onCancel: null,//
editClass: null,
submit: null,
cancel: null,
type: 'text', //text, textarea or select
submitBy: 'blur', //blur,change,dblclick,click
editBy: 'click',
options: null
}
if(options=='disable')
return this.unbind(this.data('editable.options').editBy,this.data('editable.options').toEditable);
if(options=='enable')
return this.bind(this.data('editable.options').editBy,this.data('editable.options').toEditable);
if(options=='destroy')
return this.unbind(this.data('editable.options').editBy,this.data('editable.options').toEditable)
.data('editable.previous',null)
.data('editable.current',null)
.data('editable.options',null);
var options = $.extend(defaults, options);
options.toEditable = function(){
$this = $(this);
$this.data('editable.current',$this.html());
opts = $this.data('editable.options');
$.editableFactory[opts.type].toEditable($this.empty(),opts);
// Configure events,styles for changed content
$this.data('editable.previous',$this.data('editable.current'))
.children()
.focus()
.addClass(opts.editClass);
// Submit Event
if(opts.submit){
$('<button/>').appendTo($this)
.html(opts.submit)
.one('mouseup',function(){opts.toNonEditable($(this).parent(),true)});
}else
$this.one(opts.submitBy,function(){opts.toNonEditable($(this),true)})
.children()
.one(opts.submitBy,function(){opts.toNonEditable($(this).parent(),true)});
// Cancel Event
if(opts.cancel)
$('<button/>').appendTo($this)
.html(opts.cancel)
.one('mouseup',function(){opts.toNonEditable($(this).parent(),false)});
// Call User Function
if($.isFunction(opts.onEdit))
opts.onEdit.apply( $this,
[{
current:$this.data('editable.current'),
previous:$this.data('editable.previous')
}]
);
}
options.toNonEditable = function($this,change){
opts = $this.data('editable.options');
// Configure events,styles for changed content
$this.one(opts.editBy,opts.toEditable)
.data( 'editable.current',
change
?$.editableFactory[opts.type].getValue($this,opts)
:$this.data('editable.current')
)
.html(
opts.type=='password'
?'*****'
:$this.data('editable.current')
);
// Call User Function
var func = null;
if($.isFunction(opts.onSubmit)&&change==true)
func = opts.onSubmit;
else if($.isFunction(opts.onCancel)&&change==false)
func = opts.onCancel;
if(func!=null)
func.apply($this,
[{
current:$this.data('editable.current'),
previous:$this.data('editable.previous')
}]
);
}
this.data('editable.options',options);
return this.one(options.editBy,options.toEditable);
}
$.editableFactory = {
'text': {
toEditable: function($this,options){
$('<input/>').appendTo($this)
.val($this.data('editable.current'));
},
getValue: function($this,options){
return $this.children().val();
}
},
'password': {
toEditable: function($this,options){
$this.data('editable.current',$this.data('editable.password'));
$this.data('editable.previous',$this.data('editable.password'));
$('<input type="password"/>').appendTo($this)
.val($this.data('editable.current'));
},
getValue: function($this,options){
$this.data('editable.password',$this.children().val());
return $this.children().val();
}
},
'textarea': {
toEditable: function($this,options){
$('<textarea/>').appendTo($this)
.val($this.data('editable.current'));
},
getValue: function($this,options){
return $this.children().val();
}
},
'select': {
toEditable: function($this,options){
$select = $('<select/>').appendTo($this);
$.each( options.options,
function(key,value){
$('<option/>').appendTo($select)
.html(value)
.attr('value',key);
}
)
$select.children().each(
function(){
var opt = $(this);
if(opt.text()==$this.data('editable.current'))
return opt.attr('selected', 'selected').text();
}
)
},
getValue: function($this,options){
var item = null;
$('select', $this).children().each(
function(){
if($(this).attr('selected'))
return item = $(this).text();
}
)
return item;
}
}
}
})(jQuery);