基于Ckeditor的表单设计器的开发

众所周知,表单、工作流是企业业务系统的核心,而一个通用的表单设计器能为开发节省很多的编码工作。

接下来,我将用几篇文章,介绍如何用CKeditor定制简单的表单设计器。


一、安装CKEditor


 从 CKEditor 网站:http://ckeditor.com/download 下载最新版本的 CKEditor 。

 将下载的文档解压到你的 website 根目录下的 ckeditor 目录中。

你可以将文档放在你的 website 的任意路径中。ckeditor 目录是缺省目录。


二、加载CKEditor


CKEditor 是 JavaScript 应用程序。要加载它,需要在页面中包含一个简单的文件引用。
如果已在站点根目录下的 ckeditor 目录中安装了 CKEditor , 你需要将下面的代码片段插入到
页面的<head>部分:
<head>
...
<script src="/ckeditor/ckeditor.js"></script>

</head>
当上述文件加载后,就可以使用 CKEditor JavaScript API。

三、生成编辑框


CKEditor 的工作就像页面中的文本域元素。编辑器提供了用户界面,可以容易在编写、格式化,并可与富文本一起处理,但是可以用<textarea>元素完成同样的事情(尽管不是那么容易) ,需要用户在其中输入 HTML 代码。

实际上,CKEditor 使用文本域将其数据传给服务器。对于终端用来来说,文本域是不可见的。

为了生成编辑器的实例,必须首先将<textarea>元素加入到 HTML 页面的源代码中。

<textarea name="editor1"></textarea>


插入文本域后,就可以使用 CKEditor JavaScript API 将 HTML 元素替换为编辑器的实例。
调用简单的 CKEDITOR.replace 方法:

<script>
CKEDITOR.replace( 'editor1' );
</script>


全部代码:

[java]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8.   
  9. <%@ include file="/page/common/common.jsp"%>  
  10. <script src="${contextPath}/ckeditor_standard/ckeditor/ckeditor.js"></script>  
  11.   
  12. </head>  
  13. <body>  
  14.     <form method="post" action="${contextPath}/editor/test1">  
  15.         <p>  
  16.             My Editor:<br>  
  17.             <textarea name="editor1"><p>Initial value.</p></textarea>  
  18.             <script>  
  19.                 CKEDITOR.replace('editor1');  
  20.             </script>  
  21.         </p>  
  22.         <p>  
  23.             <input type="submit">  
  24.         </p>  
  25.     </form>  
  26. </body>  
  27. </html>  

效果


可以把上面的界面集成到自己的项目中。


CKEditor的安装,加载和集成都已经说完了,下篇将介绍如果扩展自己的插件。

一、配置CKEditor


CKEditor 具有丰富的一组配置选项,可以定制其外观、功能和行为。主配置文件名字为config.js。此文件可以在 CKEditor 安装文件夹的根目录中找到。

可用的配置选项在 API 文档中可以找到所有可用的配置选项。参考 CKEDITOR.config 对象的定义。


API文档:http://docs.ckeditor.com/#


给大家看看我的config配置:

 

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. CKEDITOR.editorConfig = function( config ) {  
  2.       
  3.     //定义工具栏显示按钮  
  4.     config.toolbar_ths =  
  5.          [  
  6.              { name: 'document', items: ['Source''-''DocProps''Print''-''Templates'] },  
  7.              { name: 'clipboard', items: ['Cut''Copy''Paste''PasteText''PasteFromWord''-''Undo''Redo'] },  
  8.              { name: 'editing', items: ['Find''Replace''-''SelectAll''-''SpellChecker''Scayt'] },  
  9.              { name: 'links', items: ['Link''Unlink''Anchor'] },  
  10.             '/',  
  11.              { name: 'basicstyles', items: ['Bold''Italic''Underline''Strike''Subscript''Superscript''-''RemoveFormat'] },  
  12.              { name: 'paragraph', items: ['NumberedList''BulletedList''-''Outdent''Indent''-''Blockquote''-''JustifyLeft''JustifyCenter''JustifyRight''JustifyBlock''-''BidiLtr''BidiRtl'] },  
  13.                
  14.              { name: 'insert', items: ['Image''Flash''HorizontalRule''Smiley''SpecialChar''PageBreak''Iframe'] },  
  15.              '/',  
  16.              { name: 'styles', items: ['Styles''Format''Font''FontSize'] },  
  17.              { name: 'colors', items: ['TextColor''BGColor'] },  
  18.              { name: 'tools', items: ['Maximize''ShowBlocks'] },  
  19.              //自定义插件在工具栏上的位置  
  20.              { name: 'extent', items: ['Table','-','ths_form','-','ths_div','-','ths_label','-','ths_textfield','-','ths_select','-','ths_radio','-','ths_checkbox','-','ths_hiddenfield','-','ths_textarea','-','ths_button''-','Preview']}  
  21.          ];  
  22.       
  23.         //使用哪个工具栏  
  24.         config.toolbar = 'ths';  
  25.           
  26.         //加载自定义插件  
  27.         config.extraPlugins += (config.extraPlugins ? ',ths_textfield,ths_select,ths_radio,ths_checkbox,ths_hiddenfield,ths_textarea,ths_button,ths_div,ths_label,ths_form' : 'ths_textfield,ths_select,ths_radio,ths_checkbox,ths_hiddenfield,ths_textarea,ths_button,ths_div,ths_label,ths_form');  
  28.           
  29.         //移除不需要的插件  
  30.         config.removePlugins = 'forms,elementspath';  
  31.           
  32.         //移除不需要的工具栏按钮  
  33.         config.removeButtons = 'CreateDiv';  
  34.           
  35.         //去掉回车添加p标签,使用br  
  36.         config.enterMode = CKEDITOR.ENTER_BR ;    
  37.         config.shiftEnterMode =CKEDITOR.ENTER_BR;   
  38.         //config.startupMode = 'source';  (默认进入源代码编辑)  
  39.         //取消内容过滤  
  40.         config.allowedContent = true;  
  41.         //工具栏是否可以被收缩  
  42.         config.toolbarCanCollapse = true;  
  43.   
  44.         config.contentsCss = ['../assets/css/bootstrap.min.css'];  
  45.           
  46.         //工具栏的位置  
  47.         config.toolbarLocation = 'top';//可选:bottom  
  48.   
  49.         //工具栏默认是否展开  
  50.         config.toolbarStartupExpanded = true;  
  51.           
  52.         config.pasteFromWordRemoveStyles = false;  
  53.   
  54.           
  55. };  


这个配置,基本上囊括了CKEditor的最常用配置,注意注释部分


config中每一项的意思,请参照http://docs.ckeditor.com/#!/api/CKEDITOR.config


注意到config中,我们加入了自己的插件

 

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ths_textfield,ths_select,ths_radio,ths_checkbox,ths_hiddenfield,ths_textarea,ths_button,ths_div,ths_label,ths_form  

那么最终,我们的表单设计器是什么样子?

看看效果:



看到这,熟悉.NET的同学,有没有想起VS中的可视化页面设计器?其实要实现的表单设计器,就是运行在浏览器中类似vs这种设计器的Javascript富文本编辑器。


下节终于可以介绍重头戏,自定义插件的开发了。

前面基本环境都做得差不多了,这篇我们来介绍自定义插件的开发。


我们以"文本框“为例,

先来看效果:



点击确定,即可插入一个文本框


实现方式:


1、在ckeditor目录下 plugins文件夹下,新建如下结构:



plugin.js


 

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. CKEDITOR.plugins.add( 'ths_textfield', {  
  2.     icons: 'ths_textfield',  
  3.     init: function( editor ) {   //初始化  
  4.         var pluginName = 'ths_textfield'//控件名称  
  5.         editor.addCommand( pluginName, new CKEDITOR.dialogCommand( pluginName ) ); //给编辑器注册一个打开弹出窗命令  
  6.                  
  7.         editor.ui.addButton(pluginName, {  //在工具栏上增加一个按钮,绑定按钮事件  
  8.             label: '单行文本框',  
  9.             command: pluginName  
  10.         });  
  11.       
  12.         if ( editor.contextMenu ) {  //为文本框加右键属性菜单  
  13.             editor.addMenuGroup( 'textFieldGroup' );  
  14.              editor.addMenuItem( 'textFieldItem', {  
  15.                     label: '文本框属性',  
  16.                     command:pluginName,  
  17.                     group: 'textFieldGroup'  
  18.                 });  
  19.               //右键菜单的监听器,判断是否显示菜单         
  20.               editor.contextMenu.addListener( function( element ) {  
  21.                     if ( element && !element.isReadOnly() ) {  
  22.                         var name = element.getName();  
  23.                         if ( name == 'input' ) {  
  24.                             var type = element.getAttribute( 'type' ) || 'text';  
  25.                             if ( type=='text' ){  
  26.                                    return { textFieldItem: CKEDITOR.TRISTATE_OFF };  
  27.                             }  
  28.                         }  
  29.                     }  
  30.                 });  
  31.         }  
  32.                 //增加弹出窗  
  33.         CKEDITOR.dialog.add( pluginName, this.path + 'dialogs/'+pluginName+'.js' );  
  34.         //为文本框双击事件绑定一个事件,即显示弹出窗  
  35.         editor.on( 'doubleclick'function( evt ) {  
  36.             var element = evt.data.element;  
  37.               
  38.              if ( element.is( 'input' ) ) {  
  39.                     var type = element.getAttribute( 'type' ) || 'text';  
  40.                     if ( type=='text' ){  
  41.                         evt.data.dialog =pluginName;  
  42.                     }  
  43.             }  
  44.         })  
  45.           
  46.     }  
  47. });  

 


代码不过多解释,请看注释
不喜欢讲代码,大家结合文档看注释


ths_textfield.js

 

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. CKEDITOR.dialog.add( 'ths_textfield'function( editor ) {  
  2.       
  3.     return {  
  4.         title: '单行文本框属性',  
  5.         minWidth: 400,  
  6.         minHeight: 200,  
  7.         //弹出窗上显示的内容  
  8.         contents: [  
  9.             {  
  10.                 id: 'tab-basic',  
  11.                 label: '基本属性',  
  12.                 elements:[ {  
  13.                     type: 'hbox',  
  14.                     widths: [ '50%''50%' ],  
  15.                     children:  
  16.                     [  
  17.                          ths_editor_field(editor),              
  18.                              ths_editor_value(editor)  
  19.                      ]  
  20.                 },  
  21.                 {  
  22.                         type: 'hbox',  
  23.                         widths: [ '50%''50%' ],  
  24.                         children:  
  25.                         [ths_editor_relative_width(editor) ,  
  26.                          ths_editor_style(editor)  
  27.                         ]  
  28.                  }  
  29.                 ]  
  30.             },  
  31.   
  32.             {  
  33.                 id: 'tab-validate',  
  34.                 label: '数据校验',  
  35.                 elements: [  
  36.                     {  
  37.                         type: 'checkbox',  
  38.                         id: 'required',  
  39.                         label: '必填',  
  40.                         setup: function( element ) {  
  41.                             if(element.getAttribute( "required" )){  
  42.                                 this.setValue(true);  
  43.                             }  
  44.                         },  
  45.                         commit: function ( element ) {  
  46.                             var required = this.getValue();  
  47.                             if ( required )  
  48.                                 element.setAttribute( 'required''true' );  
  49.                             else if ( !this.insertMode )  
  50.                                 element.removeAttribute( 'required' );  
  51.                         }  
  52.                     }  
  53.                 ]  
  54.             },  
  55.             {  
  56.                 id: 'tab-event',  
  57.                 label: '事件',  
  58.                 elements: [  
  59.                                 ths_editor_onblur(editor),  
  60.                             ths_editor_onfocus(editor),  
  61.                             ths_editor_onclick(editor),  
  62.                             ths_editor_onchange(editor)  
  63.                 ]  
  64.             }  
  65.         ],  
  66.         //弹出窗显示事件  
  67.         onShow: function() {  
  68.   
  69.             var selection = editor.getSelection();  
  70.             var element = selection.getStartElement();  
  71.             if ( !element || element.getName() != 'input' || element.getAttribute( 'type' )!='text'  ) {  
  72.                 this.insertMode = true;  
  73.             }else{  
  74.                 this.insertMode = false;  
  75.             }  
  76.   
  77.             this.element = element;  
  78.             if ( !this.insertMode ){  
  79.                 this.setupContent( this.element );  
  80.             }  
  81.                   
  82.         },  
  83.         //弹出窗确定按钮事件  
  84.         onOk: function() {  
  85.             submitElement(this,editor,'text');  
  86.         }  
  87.     };  
  88. });  


补充js函数

这是针对所有表单组件的js

 

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. function submitElement(dialog,editor,type){  
  2.       if ( dialog.insertMode ){  //如果是新建状态  
  3.           
  4.         var div=editor.document.createElement( 'div' );  
  5.         var label;  
  6.         var element;  
  7.                 //为不同的元素赋值  
  8.             switch ( type ) {  
  9.                 case 'select':  
  10.                     element = editor.document.createElement( 'select' );  
  11.                     if(dialog.getValueOf( 'tab-basic''dictionary')){element.setAttribute( 'dictionary', dialog.getValueOf( 'tab-basic''dictionary') )};           
  12.                     break;  
  13.                 case 'textarea':  
  14.                     element = editor.document.createElement( 'textarea' );  
  15.                     if(dialog.getValueOf( 'tab-basic''value')){element.setAttribute( 'value', dialog.getValueOf( 'tab-basic''value') )};              
  16.                     break;  
  17.                 case 'text':  
  18.                     element = editor.document.createElement( 'input' );  
  19.                     element.setAttribute( 'type', type );  
  20.                     //if(dialog.getValueOf( 'tab-basic', 'size')!='default'){element.addClass( dialog.getValueOf( 'tab-basic', 'size') )};  
  21.                     if(dialog.getValueOf( 'tab-basic''value')){element.setAttribute( 'value', dialog.getValueOf( 'tab-basic''value') )};              
  22.                     break;  
  23.                 case 'checkbox':  
  24.                     label=editor.document.createElement( 'label' );  
  25.                     label.addClass( 'checkbox-inline' );  
  26.                     element = editor.document.createElement( 'input' );  
  27.                     element.setAttribute( 'type', type );  
  28.                     if(dialog.getValueOf( 'tab-basic''dictionary')){element.setAttribute( 'dictionary', dialog.getValueOf( 'tab-basic''dictionary') )};           
  29.                     break;  
  30.                 case 'radio':  
  31.                     label=editor.document.createElement( 'label' );  
  32.                     label.addClass( 'radio-inline' );  
  33.                     element = editor.document.createElement( 'input' );  
  34.                     element.setAttribute( 'type', type );  
  35.                     if(dialog.getValueOf( 'tab-basic''dictionary')){element.setAttribute( 'dictionary', dialog.getValueOf( 'tab-basic''dictionary') )};           
  36.                     break;  
  37.                 case 'hidden':  
  38.                     element = editor.document.createElement( 'input' );  
  39.                     element.setAttribute( 'type', type );  
  40.                     element.setAttribute( 'name', dialog.getValueOf( 'tab-basic''name' ) );  
  41.                     if(dialog.getValueOf( 'tab-basic''value')){element.setAttribute( 'value', dialog.getValueOf( 'tab-basic''value') )};              
  42.                     editor.insertElement( element );  
  43.                     return;  
  44.             }  
  45.           
  46.             element.addClass( 'form-control' );  
  47.             element.setAttribute( 'name', dialog.getValueOf( 'tab-basic''name' ) );  
  48.             if(dialog.getValueOf( 'tab-basic''style' )){  
  49.                 element.setAttribute( 'style', element.getAttribute('style') ? element.getAttribute('style')+dialog.getValueOf( 'tab-basic''style' ):dialog.getValueOf( 'tab-basic''style' ) )  
  50.             };  
  51.               
  52.             if(dialog.getValueOf( 'tab-basic''width' )) {div.addClass(dialog.getValueOf( 'tab-basic''width'))};  
  53.               
  54.             if(dialog.getValueOf( 'tab-event''onclick')){element.setAttribute( 'onclick', dialog.getValueOf( 'tab-event''onclick') )};  
  55.             if(dialog.getValueOf( 'tab-event''onfocus')){element.setAttribute( 'onfocus', dialog.getValueOf( 'tab-event''onfocus') )};  
  56.             if(dialog.getValueOf( 'tab-event''onblur')){element.setAttribute( 'onblur', dialog.getValueOf( 'tab-event''onblur') )} ;  
  57.             if(dialog.getValueOf( 'tab-event''onchange')){element.setAttribute( 'onchange', dialog.getValueOf( 'tab-event''onchange') )} ;  
  58.             if(dialog.getValueOf( 'tab-validate''required' )){element.setAttribute( 'required', dialog.getValueOf( 'tab-validate''required' ) )};  
  59.               
  60.             if(label){  
  61.                 label.append(element);  
  62.                 div.append(label);  
  63.             }else{  
  64.                 div.append(element);  
  65.             }  
  66.               
  67.             editor.insertElement( div );  
  68.           
  69.       }else{  
  70.           dialog.commitContent( dialog.element );  
  71.       }  
  72.       
  73. }  


弹出窗中,显示文本域的示例:

 

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. function ths_editor_size(editor){  
  2.     var editor_size={  
  3.            type : 'select',  
  4.         id: 'size',  
  5.         label: '大小:',  
  6.         style: 'width:190px',  
  7.         'default' : 'default',  
  8.         items :  
  9.                 [  
  10.                     [ '大''input-lg' ],  
  11.                     [ '默认''default' ],  
  12.                     [ '小''input-sm' ]  
  13.                 ],  
  14.         setup: function( element ) {  //弹出窗初始化时会调用  
  15.              
  16.            var classStr=element.getAttribute( "class" ) ? element.getAttribute( "class" ) : 'default';  
  17.              
  18.            if(classStr.indexOf('input')>=0){  
  19.                classStr=classStr.substring(classStr.indexOf('input'),classStr.indexOf('input')+8);  
  20.                this.setValue(classStr);  
  21.            }else{  
  22.                return;  
  23.            }  
  24.              
  25.         },  
  26.         commit: function( element ) {  //提交时会调用  
  27.              var classStr = this.getValue();  
  28.                
  29.              if(element.hasClass('input-lg')) element.removeClass('input-lg');  
  30.              if(element.hasClass('input-sm')) element.removeClass('input-sm');  
  31.                
  32.               if ( classStr && classStr!='default'){  
  33.                  element.addClass(classStr);  
  34.               }  
  35.         }  
  36.        }  
  37.     return editor_size;  
  38. }  



 

不想贴代码,感觉代码真的很无力,但想说明白一件事,似乎代码来得更直接些,上面代码均为核心代码。


随后会把代码开源,请留意


如果你英文比较好,推荐看如下两篇文章:

http://docs.ckeditor.com/#!/guide/plugin_sdk_sample_1

http://docs.ckeditor.com/#!/guide/plugin_sdk_sample_2

上篇,我们介绍了表单域的开发,那么针对一键初始化,保存,重置,清空,预览,这些操作如何做呢?


看看效果:



点击初始化,直接初始化一个完整的表单,当然初始化规则,需要你自己定义。




点击预览,即可弹出新窗口,预览设计好的表单。

其他操作不再一一截图。


这些效果,其实不难实现,参照Ckeditor提供的api,我们定义好自己的规则即可,完整代码如下:

 

[html]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.     <%@ include file="/page/common/common.jsp"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />  
  8. <meta charset="utf-8" />  
  9. <title>Insert title here</title>  
  10.   
  11.   
  12. <meta name="description" content="Static & Dynamic Tables" />  
  13. <meta name="viewport"  
  14.     content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />  
  15.   
  16. <!-- bootstrap & fontawesome -->  
  17. <link rel="stylesheet"  
  18.     href="${contextPath}/assets/css/bootstrap.min.css" />  
  19. <link rel="stylesheet"  
  20.     href="${contextPath}/assets/css/font-awesome.min.css" />  
  21.   
  22. <!-- page specific plugin styles -->  
  23.   
  24. <!-- text fonts -->  
  25. <link rel="stylesheet" href="${contextPath}/assets/css/ace-fonts.css" />  
  26.   
  27. <!-- ace styles -->  
  28. <link rel="stylesheet" href="${contextPath}/assets/css/ace.min.css"  
  29.     id="main-ace-style" />  
  30.   
  31. <!--[if lte IE 9]>  
  32.             <link rel="stylesheet" href="${contextPath}/assets/css/ace-part2.min.css" />  
  33.         <![endif]-->  
  34. <link rel="stylesheet"  
  35.     href="${contextPath}/assets/css/ace-skins.min.css" />  
  36. <link rel="stylesheet" href="${contextPath}/assets/css/ace-rtl.min.css" />  
  37.   
  38. <!--[if lte IE 9]>  
  39.           <link rel="stylesheet" href="${contextPath}/assets/css/ace-ie.min.css" />  
  40. <![endif]-->  
  41.   
  42. <link rel="stylesheet" href="${contextPath}/assets/css/ths.content.css" />  
  43. <link rel="stylesheet" type="text/css" href="${contextPath}/assets/css/easyui.css">  
  44.   
  45. <!-- ace settings handler -->  
  46. <script src="${contextPath}/assets/js/ace-extra.min.js"></script>  
  47.   
  48. <!-- HTML5shiv and Respond.js for IE8 to support HTML5 elements and media queries -->  
  49. <!--[if lte IE 8]>  
  50.         <script src="${contextPath}/assets/js/html5shiv.min.js"></script>  
  51.         <script src="${contextPath}/assets/js/respond.min.js"></script>  
  52. <![endif]-->  
  53.   
  54. <script src="${contextPath}/assets/js/jquery.min.js"></script>  
  55. <script src="${contextPath}/ckeditor/ckeditor.js"></script>  
  56. <script src="${contextPath}/ckeditor/ths_editor.js"></script>  
  57.   
  58. </head>  
  59. <body>  
  60.     <form id="editor_form" method="post" action="${contextPath}/editor/submit">  
  61.        
  62.      <input type="hidden" id="formid"   value="user_form" />  
  63.      <input type="hidden" id="listid"   value="user_list" />  
  64.      <input type="hidden" id="formurl"   value="${contextPath}/editor/formjson" />  
  65.      <input type="hidden" id="formjson"   value="" />  
  66.         <p>  
  67.             <textarea name="ths_editor"></textarea>  
  68.             <script>  
  69.             //初始化ckeditor  
  70.             var ths_editor=CKEDITOR.replace('ths_editor');  
  71.             //执行ckeditor插件  
  72.             function ths_exec_cmd(cmd){  
  73.                 CKEDITOR.instances['ths_editor'].execCommand(cmd);  
  74.             }  
  75.               
  76.             function ths_editor_init(flag){  
  77.                 if(flag=='table'){  
  78.                     var url="${contextPath}/editor/tableform"   
  79.                 }else{  
  80.                     var url="${contextPath}/editor/divform"   
  81.                 }  
  82.                   
  83.                 var param =  "formid=" + $('#formid').val()+ "&listid="  
  84.                         + $('#listid').val();  
  85.                  $.ajax({  
  86.                         type : 'post',  
  87.                         url :url,  
  88.                         data : param,  
  89.                         async : false,//这里必须是同步请求  
  90.                         success : function(response) {  
  91.                               CKEDITOR.instances['ths_editor'].insertHtml(response);  
  92.                         },  
  93.                         error:function (XMLHttpRequest, textStatus, errorThrown) {  
  94.                                alert(textStatus);  
  95.                         }  
  96.                 });  
  97.                   
  98.             }  
  99.               
  100.             function ths_editor_preview(){  
  101.                  var data = CKEDITOR.instances.ths_editor.getData();  
  102.                  $('#preview').html(data);  
  103.                  $('#preview').show()  
  104.             }  
  105.               
  106.             function ths_editor_reset(){  
  107.                 CKEDITOR.instances.ths_editor.setData( '', function() {  
  108.                     //this.checkDirty(); // true  
  109.                     ths_editor_init();  
  110.                 });  
  111.             }  
  112.               
  113.             function ths_editor_clear(){  
  114.                 CKEDITOR.instances.ths_editor.setData('');  
  115.                 $('#preview').html("");  
  116.                 $('#preview').hidden()  
  117.             }  
  118.               
  119.             function ths_editor_save(){  
  120.                  var param =  "formid=" + $('#formid').val()+ "&listid="  
  121.                     + $('#listid').val()+"&ths_editor="+CKEDITOR.instances.ths_editor.getData();  
  122.                    
  123.                  $.ajax({  
  124.                         type : 'post',  
  125.                         url : $('#editor_form').attr('action'),  
  126.                         data : param,  
  127.                         async : false,//这里必须是同步请求  
  128.                         success : function(response) {  
  129.                             alert(response);  
  130.                         },  
  131.                         error:function (XMLHttpRequest, textStatus, errorThrown) {  
  132.                                alert(textStatus);  
  133.                         }  
  134.                 });  
  135.                    
  136.             }  
  137.               
  138.             </script>  
  139.         </p>  
  140.         <p>  
  141.             <!--  <input type="submit">   -->  
  142.         </p>  
  143.     </form>  
  144.       
  145.     <input id="btn_inittable"  type="button"  value="初始化Table表单"  onclick="ths_editor_init('table')"></input>  
  146.     <input id="btn_initdiv"  type="button"  value="初始化Div表单"  onclick="ths_editor_init('div')"></input>  
  147.     <input id="btn_reset"  type="button"  value="重置"  onclick="ths_editor_reset()"></input>  
  148.     <input id="btn_clear"  type="button"  value="清空"  onclick="ths_editor_clear()"></input>  
  149.     <input id="btn_preview"  type="button"  value="预览"  onclick="ths_editor_preview()"></input>  
  150.     <input id="btn_save"  type="button"  value="保存"  onclick="ths_editor_save()"></input>  
  151.     <input id="btn_ths_textfield"  type="button"  value="单行文本框"  onclick="ths_exec_cmd('ths_textfield')"></input>  
  152.     <input id="btn_ths_textarea"  type="button"  value="多行文本框"  onclick="ths_exec_cmd('ths_textarea')"></input>  
  153.     <input id="btn_ths_select"  type="button"  value="下拉框"  onclick="ths_exec_cmd('ths_select')"></input>  
  154.     <input id="btn_ths_radio"  type="button"  value="单选框"  onclick="ths_exec_cmd('ths_radio')"></input>  
  155.     <input id="btn_ths_checkbox"  type="button"  value="复选框"  onclick="ths_exec_cmd('ths_checkbox')"></input>  
  156.     <input id="btn_ths_hiddenfield"  type="button"  value="隐藏域"  onclick="ths_exec_cmd('ths_hiddenfield')"></input>  
  157.       
  158.     <div  id="preview" style="padding:10px;margin-top:10px;display:none">  
  159.     </div>  
  160.       
  161. </body>  
  162. </html>  



 

这几篇文章更多地是介绍使用CKeditor来定制自己的表单设计器的一个思路,其中包括了CKeditor的初始化,配置,插件开发,扩展功能等。

不要拘泥于每一行代码,重要的是思路。


随后会把代码公开到CSDN CODE,不要急着跟我要代码哈。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值