• js双击修改元素内容并提交到后端封装实现


    前面发过一个版本了,后来又追加了些功能。重新发一版。新版支持select和radio。

    效果图:

    右上角带有绿标的,是可以修改的单元格。如果不喜欢显示绿标,可以传递参数时指定不显示,如果想改为其它颜色,也可以传递参数时指定颜色。如果觉得标记符号太小,可以通过signSize设置其大小。例如 1, 1.5, 2, 3 都可以。必须 为数字。

    封装代码:

    1. function wm_click_modify(config) {
    2. this.identify = config.identify;
    3. this.callback = config.callback;
    4. this.srcElement = null;
    5. this.id = null;
    6. this.field = null;
    7. this.input = null;
    8. this.signSize = typeof config.signSize == 'undefined' ? 1 : config.signSize;
    9. this.sign = typeof config.signObj == 'undefined' ? true : config.signObj;
    10. this.signColor = typeof config.signColor == 'undefined' ? '#f00' : config.signColor;
    11. this.dataType = null;
    12. this.datas = null;
    13. this.radio_value = null;
    14. this.select_value = null;
    15. this.selector = null;
    16. }
    17. wm_click_modify.prototype = {
    18. create: function () {
    19. var elem = document.querySelectorAll(this.identify)
    20. var that = this;
    21. for (i = 0; i < elem.length; i++) {
    22. if (that.sign == true) {
    23. elem[i].style.position = 'relative';
    24. elem[i].style.overflow = 'hidden';
    25. var div = document.createElement("div");
    26. div.style.position = 'absolute';
    27. div.style.backgroundColor = this.signColor;
    28. div.style.right = this.signSize * -2 + 'px';
    29. div.style.top = this.signSize * -2 + 'px';
    30. div.title = '双击空白区域可编辑'
    31. div.style.width = this.signSize * 4 + 'px';
    32. div.style.height = this.signSize * 4 + 'px';
    33. div.style.transform = "rotate(45deg)"
    34. elem[i].appendChild(div)
    35. }
    36. elem[i].addEventListener('dblclick', function (e) {
    37. that.dblclick_e(e);
    38. }, true)
    39. }
    40. },
    41. dblclick_e: function (e) {
    42. if (e.srcElement.nodeName.toLowerCase() == 'input' || e.srcElement.nodeName.toLowerCase() == 'textarea' || e.srcElement.nodeName.toLowerCase() == 'radio' || e.srcElement.nodeName.toLowerCase() == 'select') {
    43. return;
    44. }
    45. var input;
    46. var that = this;
    47. this.srcElement = e.srcElement;
    48. var id = e.srcElement.getAttribute('data-id');
    49. var field = e.srcElement.getAttribute('data-field');
    50. this.id = id;
    51. this.field = field;
    52. this.dataType = e.srcElement.getAttribute('data-datatype');
    53. var ohtml = e.srcElement.innerHTML;
    54. var ovalue = e.srcElement.innerText;
    55. var container_remove = document.getElementsByClassName('container_wm_click_modify');
    56. if (container_remove.length > 0) {
    57. for (var i = 0; i < container_remove.length; i++) {
    58. container_remove[i].parentNode.innerHTML = container_remove[i].getAttribute('ohtml');
    59. }
    60. }
    61. let offsetWidth = e.srcElement.offsetWidth == 0 ? 40 : e.srcElement.offsetWidth;
    62. let offsetHeight = e.srcElement.offsetHeight == 0 ? 100 : e.srcElement.offsetHeight;
    63. e.srcElement.innerHTML = '';
    64. var container = document.createElement('span')
    65. container.className = 'container_wm_click_modify';
    66. container.setAttribute('ohtml', ohtml);
    67. e.srcElement.appendChild(container);
    68. if (this.dataType == null || this.dataType == 'text' || this.dataType == '') {
    69. input = document.createElement("textarea");
    70. this.input = input;
    71. input.id = 'wm_click_create_input_' + field + '_' + id;
    72. input.style.borderColor = "#ccc";
    73. input.style.borderWidth = "1px"
    74. input.style.padding = "5px";
    75. //input.style.resize = 'none';
    76. input.style.width = offsetWidth + 'px';
    77. input.style.height = offsetHeight + 'px';
    78. input.setAttribute('ovalue', ovalue);
    79. input.setAttribute('ovalue_html', ohtml);
    80. input.value = ovalue;
    81. e.srcElement.appendChild(input)
    82. input.focus();
    83. input.onblur = function () {
    84. if (this.getAttribute('ovalue') != this.value) {
    85. console.log('提交')
    86. var newvalue = this.value;
    87. if (typeof that.callback != 'undefined') {
    88. that.callback({ id: id, field: field, value: newvalue })
    89. } else {
    90. console.log('未定义callback')
    91. e.srcElement.innerHTML = document.getElementById(field + '_' + id).value;
    92. }
    93. } else {
    94. console.log('不提交')
    95. e.srcElement.innerHTML = this.getAttribute('ovalue_html');
    96. }
    97. }
    98. } else if (this.dataType == 'radio') {
    99. var label, span, that = this
    100. var data_list = e.srcElement.getAttribute('data-datalist');
    101. if (data_list == '' || data_list == null) {
    102. return;
    103. }
    104. this.datas = JSON.parse(data_list);
    105. var tmp = document.createElement('input')
    106. //tmp.id = this.field + '_' + this.id;
    107. tmp.value = ovalue;
    108. tmp.type = 'hidden';
    109. tmp.setAttribute('ovalue_html', ohtml);
    110. tmp.setAttribute('ovalue', ovalue);
    111. this.input = tmp;
    112. container.appendChild(tmp)
    113. for (var k = 0; k < this.datas.length; k++) {
    114. label = document.createElement("label");
    115. label.style.marginRight = "10px"
    116. container.appendChild(label);
    117. input = document.createElement("input");
    118. input.type = "radio";
    119. input.name = field;
    120. input.value = this.datas[k].value;
    121. input.setAttribute('value_title', this.datas[k].title)
    122. if (ovalue == this.datas[k].title) {
    123. input.checked = true;
    124. input.focus();
    125. }
    126. input.style.marginRight = "6px";
    127. input.style.width = "24px";
    128. input.style.height = "24px";
    129. input.style.verticalAlign = "middle";
    130. span = document.createElement("span");
    131. span.innerHTML = this.datas[k].title;
    132. label.appendChild(span);
    133. label.insertBefore(input, span);
    134. var that = this;
    135. input.onclick = function (e) {
    136. var newvalue = this.getAttribute('value_title');
    137. that.radio_value = newvalue;
    138. if (newvalue != ovalue) {
    139. console.log('提交')
    140. tmp.value = newvalue;
    141. if (typeof that.callback != 'undefined') {
    142. that.callback({ id: id, field: field, value: this.value })
    143. } else {
    144. console.log('未定义callback')
    145. that.srcElement.innerHTML = ohtml;
    146. }
    147. } else {
    148. console.log('不提交')
    149. that.srcElement.innerHTML = ohtml;
    150. }
    151. }
    152. input.onblur = function (e) {
    153. console.log("on my god")
    154. }
    155. }
    156. } else if (this.dataType == 'select') {
    157. var label, span, that = this
    158. var data_list = e.srcElement.getAttribute('data-datalist');
    159. if (data_list == '' || data_list == null) {
    160. return;
    161. }
    162. this.datas = JSON.parse(data_list);
    163. var tmp = document.createElement('input')
    164. tmp.value = ovalue;
    165. tmp.type = 'hidden';
    166. tmp.setAttribute('ovalue_html', ohtml);
    167. tmp.setAttribute('ovalue', ovalue);
    168. this.input = tmp;
    169. container.appendChild(tmp)
    170. var selector = document.createElement('select');
    171. this.selector = selector;
    172. container.appendChild(selector)
    173. selector.style.padding = "2px";
    174. selector.style.fontSize = "18px"
    175. for (var k = 0; k < this.datas.length; k++) {
    176. selector.options.add(new Option(this.datas[k].title, this.datas[k].value))
    177. if (this.datas[k].title == ovalue) {
    178. selector.options[k].selected = true;
    179. }
    180. }
    181. selector.focus();
    182. selector.onchange = function (e) {
    183. var newvalue = selector.options[selector.selectedIndex].text;
    184. if (newvalue != ovalue) {
    185. that.select_value = newvalue;
    186. console.log('提交', newvalue)
    187. if (typeof that.callback != 'undefined') {
    188. that.callback({ id: id, field: field, value: this.value })
    189. } else {
    190. console.log('未定义callback')
    191. that.srcElement.innerHTML = ohtml;
    192. }
    193. }
    194. }
    195. selector.onblur = function (e) {
    196. that.srcElement.innerHTML = ohtml;
    197. }
    198. }
    199. },
    200. success: function () {
    201. try {
    202. if (this.dataType == null || this.dataType == 'text' || this.dataType == '') {
    203. this.srcElement.innerHTML = document.getElementById('wm_click_create_input_' + this.field + '_' + this.id).value;
    204. } else if (this.dataType == 'radio') {
    205. this.srcElement.innerHTML = this.radio_value
    206. } else if (this.dataType == 'select') {
    207. this.selector.onblur = null;
    208. this.srcElement.innerHTML = this.select_value;
    209. }
    210. } catch (e) {
    211. console.log('这里不要展示给用户。select在blur时已删除元素,因此会报错。而又必须在blur时干点什么')
    212. }
    213. if (this.sign == true) {
    214. var div = document.createElement("div");
    215. div.style.position = 'absolute';
    216. div.style.backgroundColor = this.signColor;
    217. div.style.right = this.signSize * -2 + 'px';
    218. div.style.top = this.signSize * -2 + 'px';
    219. div.style.width = this.signSize * 4 + 'px';
    220. div.style.height = this.signSize * 4 + 'px';
    221. div.style.transform = "rotate(45deg)"
    222. this.srcElement.appendChild(div)
    223. }
    224. },
    225. fail: function () {
    226. try {
    227. this.srcElement.innerHTML = this.input.getAttribute('ovalue_html');
    228. } catch (e) {
    229. }
    230. }
    231. };

    1、调用方法:

    new wm_click_modify(json格式的参数).create();

    可以看到,主要就是json格式的参数这一块怎么写。后面会说,先说说前置工作。

    html里的代码也要做些修改。

    原来你的代码是这样写的:

    sos

    假如这个内容是数据库中id为3的记录字段名为name的内容,现在我们想双击这个DIV可以修改,修改完后再提交到后端。我们要这样来修改:

     

    <div modify data-id="3" data-field="name">sosdiv>

    事实上,并不局限于div。在table中的td也可以这样来弄。理论上在哪都行,这取决于选择器能否正确选择要修改的元素。

    如果是td那就要这样改:

    <td modify data-id="3" data-field="name">sostd>

     html内容已经准备好了。下面我们要让双击修改效果生效,调用上面封装的代码。以表格元素td为例。js代码如下:

    1. new wm_click_modify({identify:"td[modify]", signObj: true, signSize:1.5, signColor:'green', callback: function(res){
    2. var that = this;
    3. var loading = msgbox.show('正在提交', 'loading')
    4. wm_ajax('/admin/Setting/edit.html',{
    5. data: {
    6. id:res.id,
    7. field:res.field,
    8. value:res.value
    9. },
    10. dataType: 'json',
    11. type: 'post',
    12. sync: true,
    13. success: function(data){
    14. console.log(data)
    15. if(data.code > 0){
    16. msgbox.close(loading)
    17. msgbox.show(data.msg, 'right');
    18. that.success()
    19. } else {
    20. msgbox.close(loading)
    21. msgbox.show(data.msg, 'error');
    22. that.fail();
    23. }
    24. },
    25. error: function(data){
    26. msgbox.close(loading)
    27. msgbox.show(data.msg, 'error');
    28. that.fail();
    29. }
    30. });
    31. }}).create();

    第一个参数是 identify,即指定选择器的选择。凡是能被querySelector 选择的写法均受支持。示例代码中写的是td[modify],即带modify属性的td元素。必须传递。

    第二个参数是signObj,即指定是否显示可编辑指示器。就是那个小绿点。如果未指定这个参数,则默认显示。

    第三个参数signSize,即指定指示器大小。取值可以为1,1.5, 2 等这些数字。如果未指定这个参数,默认为1

    第四个参数是signColor,即指定指示器颜色。取值为css颜色代码,例如: 'red'    '#f00'  都可以。如果未指定这个参数,默认为红色

    第五个参数是回调函数  
     

    1. callback:function(res){
    2. //在res中,包含以下内容: id(本条数据的记录id)、field(要修改的字段名)、value(新值)
    3. 取值分别为:
    4. res.id
    5. res.field
    6. res.value
    7. }        

    系统判断内容是否有修改,如果有修改,会回调这个函数,您需要在这个函数中写提交后端的代码,如果没有修改,不会回调这个函数。

    2、回调函数里的代码怎么写?

    回调函数一般就是用ajax往后端提交,有一点需要注意,当后端返回到前端处理结果之后,还需要调用一下封装代码中的一个方法。伪代码如下:

    1. const that = this;
    2. ajax({
    3. url:'',
    4. data: {
    5. id:res.id,
    6. field:res.field,
    7. value:res.value
    8. },
    9. success: function(data){
    10. if(data.code > 0){
    11. alert('修改成功');
    12. that.success() //这是新添加的
    13. } else {
    14. that.fail(); //这是新添加的
    15. }
    16. },
    17. error:function(e){
    18. alert("服务器错误,请稍候再试");
    19. that.fail(); //这是新添加的
    20. }
    21. })

    懂ajax的应该看明白了。就是加了  that.success()  和that.fail()。必须要调用,否则页面上的内容不会改变。这样,后端就收到了要修改哪条记录的哪个字段,新值是多少。

    上面是对纯文本内容的修改,假如要修改的是性别呢,我们希望用户只选择男或女,并不希望输入其它内容。还有就是特殊的内容一般也只让选择不让手动修改。我们来看看select和radio的用法。

    从html的修改开始。还是以table的td为例。

    假设原始html内容为:

    <td>sostd>

    如果想让用户双击后变成select,需要这样修改:

    <td modify data-id="3" data-field="gender" data-datalist='[{"value":"0", "title":"不知道"},{"value":"1", "title":"男"},{"value":"2", "title":"女"}]' data-datatype="select">sos<td>

    其中data-id和data-field和上面一样,一个指定数据表的id(唯一索引),一个指定字段名。后面datalist就是一个数据列表,用户可以选择里面的数据。data-datatype 指定元素类型,这里的值取的是select,意思是说当用户双击后,会变成select让用户选择data-datalist中的数据。如果想用radio,那只需要把data-datatype的值设置为radio即可。

    因为radio没有blur事件,封装代码虽然处理了这种情况,但是还是不如select更方便。所以,2者选一的话建议使用select。

    调用部分代码不变。

  • 相关阅读:
    二维码怎么做列表?点击可跳转其他内容
    顺序读写函数的介绍:fscanf & fprintf
    “深入理解机器学习性能评估指标:TP、TN、FP、FN、精确率、召回率、准确率、F1-score和mAP”
    【Spark】PySpark DataFrame
    falco 【3】 default macro
    C++goto语句
    《web课程设计》用HTML CSS做一个简洁、漂亮的个人博客网站
    01 初识HTML5
    【技术分享】堆叠交换机替换指导
    Herodotus——无需bridge借助Storage proof实现的以太坊跨层数据访问
  • 原文地址:https://blog.csdn.net/superfans98/article/details/134550769