CKEditor对话框选择框动态填充

发布于 2024-11-28 11:07:31 字数 153 浏览 2 评论 0原文

我在对话框中有两个选择框。我想知道是否可以根据第一个框的选择设置第二个选择框的内容。即,如果选择框 1 选择了 x,则选择框 2 的内容为 a、b , c..如果 select box1 选择了 y,则 select box2 内容为 d, e, f.. 这可以实现吗?

谢谢

I have two select boxes in a dialog..I was wondering if i can set the contents of the second select box based on the selection of the first box..i.e if select box1 has x selected, then select box2 contents are a, b, c..if select box1 has y selected, then select box2 contents are d, e, f.. Is this possible to implement?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷心人i 2024-12-05 11:07:31

我猜测您提到的带有两个选择框的对话框是您正在创建的自定义插件的一部分。所以我的答案显示了您可以在作为插件的一部分创建的对话框文件中使用的代码。

选择器一的 onChange 函数使用以下方法创建对选择器二的引用:

dialog.getContentElement( 'general', 'selectorTwo' )

“常规”是对话框的 ID包含选择器的面板。
'selectorTwo' 是第二个选择器的 id。

我修改了 cksource.com 上托管的 SimpleLink 教程的源代码:
使用自定义对话框窗口创建 CKEditor 插件
http://docs.cksource.com/CKEditor_3.x/Tutorials/SimpleLink_Plugin_Part_1

下载 SimpleLink 源代码这里:
http://docs.cksource.com/images/a/a8/SimpleLink.zip

解压并将 simpleLink 文件夹添加到您的 ckeditor/plugins 目录中。

通过将 simpleLink 插件添加到您的额外插件列表来更新您的配置文件:
config.extraPlugins = 'onchange,insertsnippet,resconfinearea,resiframe,simpleLink',

或者当您执行 CKEDITOR.replace 时使用:
extraPlugins : 'onchange,insertsnippet,resconfinearea,resiframe,simpleLink',

同时将按钮添加到工具栏 - 'SimpleLink'

您将将此消息底部的代码片段插入到 ckeditor/plugins/simpleLink/plugin 中。

为了使插入点易于定位,代码片段的前十二行与以下行重叠 ( 111 - 122 )的 simpleLink/plugin.js 文件。

插入代码片段后,打开 ckeditor/_source/plugins/forms/dialogs/select.js。
从 select.js 文件顶部复制以下三个函数并将它们添加到 simpleLink/plugin.js 文件顶部:
addOption()
removeAllOptions()
getSelect()


打开一个使用 CKEditor 的页面,然后单击您添加的 SimpleLink 按钮,您将在对话框窗口的底部看到选择器一和二。当您更改选择器一时,选择器二也会更新。您应该能够修改代码以适合您的应用程序。

这些页面提供了一些有用的信息,您在查看我发布的代码时可以参考:

使用自定义对话框窗口创建 CKEditor 插件
http://docs.cksource.com/CKEditor_3.x/Tutorials/SimpleLink_Plugin_Part_1

CKEDITOR 类.dialog
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.html

<强>类CKEDITOR.dialog.definition
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.definition.html< /a>

这是要插入 ckeditor/plugins/simpleLink/plugin.js 文件的代码片段。
只需选择行( 111 - 122 )并将其粘贴到:

{
  type : 'checkbox',
  id : 'newPage',
  label : 'Opens in a new page',
  // Default value.
  // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.ui.dialog.checkbox.html#constructor
  'default' : true,
  commit : function( data )
  {
    data.newPage = this.getValue();
  }
},
{
  type : 'select',
  id : 'selectorOne',
  label : 'Selector One',
  items :
  [
    [ '<none>', '' ],
    [ 'Set 1', 1],
    [ 'Set 2', 2 ],
    [ 'Set 3', 3 ]
  ],
  onChange : function()
  {
    var dialog = this.getDialog(),
      values = dialog.getContentElement( 'general', 'selectorTwo' ), // 'general' is the id of the dialog panel.
      selectedSet = parseInt(this.getValue());

    switch(selectedSet)
    {
    case 1:
      optionsNames = new Array("Set One <none>","Set One A","Set One B","Set One C"),
      optionsValues = new Array("","setOneA","setOneB","setOneC");
      break;
    case 2:
      optionsNames = new Array("Set Two <none>","Set Two A","Set Two B","Set Two C"),
      optionsValues = new Array("","setTwoA","setTwoB","setTwoC");
      break;
    case 3:
      optionsNames = new Array("Set Three <none>","Set Three A","Set Three B","Set Three C"),
      optionsValues = new Array("","setThreeA","setThreeB","setThreeC");
      break;
    default:
      optionsNames = new Array("<none>"),
      optionsValues = new Array("");
    }

    removeAllOptions( values );

    for ( var i = 0 ; i < optionsNames.length ; i++ )
    {
      var oOption = addOption( values, optionsNames[ i ],
        optionsValues[ i ], dialog.getParentEditor().document );
      if ( i == 0 )
      {
        oOption.setAttribute( 'selected', 'selected' );
        oOption.selected = true;
      }
    }
  },
  commit : function( data )
  {
    data.selectorOne = this.getValue();
  }
},
{
  type : 'select',
  id : 'selectorTwo',
  label : 'Selector Two',
  items :
  [
    [ '<none>', '' ]
  ],
  commit : function( data )
  {
    data.selectorTwo = this.getValue();
  }
},

希望这能解决问题,如果有不清楚的地方请告诉我,
Joe


你好,oggiemc,

很高兴知道您有机会尝试这些代码,而且它很有帮助,这真是太好了。

关于从plugins/forms/dialogs/select.js 文件复制的函数的其他问题的答案。

1)打开对话框时不应调用这三个函数。在我最初的答案中概述的创建的示例中没有发生这种情况。您可能需要在代码中搜索这三个函数名称,以查看它们是否在 selectOne 定义中包含的 ( onChange : function() ) 之外的其他位置调用。

2)我分解了(removeAllOptions()函数)并将其包含在下面。它有类似的代码行。对于您询问的行:

if (combo && oOption && oOption.getName() == 'option'),

它说 If (combo是否存在) AND (oOption是否存在) AND (oOption.getName() == 'option') If 语句为 true。
我所说的“存在”,是指它不为 null,也不是计算结果为 false 的值(0 或二进制 Not true)。

3) 是的,在 addOption() 函数的这个特定用法中,没有使用索引变量。您可以删除两个 if 语句,只保留每个 else 子句中的代码语句。

4) obj.getInputElement() 返回 DOM 元素对象,但 obj.getInputElement().$ 只返回 DOM 对象的 HTML 标签。尝试向 getSelect() 函数添加一些 console.log 调用,您将看到差异:

if ( obj && obj.domId && obj.getInputElement().$ ) {        // Dialog element.
  console.log(obj.getInputElement().$); // ADDED CONSOLE.LOG
  console.log(obj.getInputElement()); // ADDED CONSOLE.LOG

return obj.getInputElement();

以下是removeAllOptions()函数的流程:
在selectorOne 的onChange() 函数中,我们创建一个代表selectorTwo 的变量“values”。
value =dialog.getContentElement( 'general', 'selectorTwo' )

在根据selectorOne中选择的选项使用新选项值填充selectorTwo之前,我们从selectorTwo中删除所有现有选项:
删除所有选项(值); // 值代表selectorTwo

在removeAllOptions() 函数中,变量“combo”被赋予在函数调用中传递的值,因此:
组合=值=选择器二。

removeAllOptions() 函数调用 getSelect() 函数并将“combo”变量作为参数发送给它。
“combo”变量将被分配 getSelect() 函数返回的值。

在 getSelect() 函数中,变量“obj”被赋予在函数调用中传递的值,因此:
obj = 组合 = 值 = 选择器二。

getSelect() 函数可以返回三个值之一:
A) if ( obj && obj.domId && obj.getInputElement().$ )
如果 obj 为 true 并且 obj.domId 为 true 并且 obj.getInputElement().$ 返回 false 或 null 以外的任何值,它将返回 obj.getInputElement() 的值。
因此,如果 obj 变量存在并且设置了 obj 的 domId 属性,它将在 obj 上运行 getInputElement() 函数并检查 DOM 属性 ($) 是否已设置。
getInputElement() 使用domId 返回与selectorTwo 元素对应的DOM 元素。

B) 如果 A 的三个部分不全都为 true,则 getSelect() 函数会尝试:
if ( obj && obj.$ )
因此,如果 obj 变量存在并且它已经是 DOM 元素 ($),它只会发送回 obj。

C) 如果步骤 B 失败,则 getSelect() 返回 false,并且选择器Two 没有任何要删除的选项元素。

现在回到removeAllOptions()函数,看看如果getSelect()的步骤A或B返回一些东西会发生什么。
在这两种情况下,“combo”变量都将是带有 DOM 元素 (combo.$) 设置的 selectedTwo 对象。
它对 selecterTwo 内的选项元素数组运行 while 循环并删除它们。
它获取第一个选项元素“getChild( 0 )”并将其删除,位于 Child( 1 ) 位置的选项元素移动到位置 Child( 0 ) 并继续循环,直到没有剩余选项元素为止。

getInputElement() 函数位于 ckeditor_source\plugins\dialogui\plugin.js 的第 ( 1085 ) 行
getInputElement() 函数调用 getElement() 函数。
getElement() 函数位于 ckeditor_source\plugins\dialog\plugin.js 的第 ( 2560 ) 行

function removeAllOptions( combo )
{
  combo = getSelect( combo );
  while ( combo.getChild( 0 ) && combo.getChild( 0 ).remove() )
  { /*jsl:pass*/ }
}

function getSelect( obj )
{
  if ( obj && obj.domId && obj.getInputElement().$ )        // Dialog element.
    return  obj.getInputElement();
  else if ( obj && obj.$ )
    return obj;
  return false;
}

好吧,

I'm guessing that the dialog with two select boxes you mentioned is part of a custom plugin you are creating. So my answer shows code you could use in the dialog file that's being created as part of the plugin.

The onChange function of Selector One creates a reference to Selector Two using:

dialog.getContentElement( 'general', 'selectorTwo' )

'general' is the id of the dialog panel containing the selectors.
'selectorTwo' is the id of the second selector.

I modified the source code from the SimpleLink tutorial hosted at cksource.com:
Creating a CKEditor Plugin with a Custom Dialog Window
http://docs.cksource.com/CKEditor_3.x/Tutorials/SimpleLink_Plugin_Part_1

Download the SimpleLink source code here:
http://docs.cksource.com/images/a/a8/SimpleLink.zip

Unzip and add the simpleLink folder to your ckeditor/plugins directory.

Update your config file by adding the simpleLink plugin to your extraplugins list:
config.extraPlugins = 'onchange,insertsnippet,resconfinearea,resiframe,simpleLink',

Or when you do CKEDITOR.replace use:
extraPlugins : 'onchange,insertsnippet,resconfinearea,resiframe,simpleLink',

Also add the button to your toolbar - 'SimpleLink'

You're going to insert the code snippet from the bottom of this message into the ckeditor/plugins/simpleLink/plugin.js file.

To make the insertion point easy to locate, the first twelve lines of the snippet overlap with lines ( 111 - 122 ) of the simpleLink/plugin.js file.

After you insert the snippet, open ckeditor/_source/plugins/forms/dialogs/select.js.
Copy the following three functions from the top of the select.js file and add them at the top of the simpleLink/plugin.js file:
addOption()
removeAllOptions()
getSelect()


Open a page that's using CKEditor and click on the SimpleLink button you added, you'll see selectors one and two at the bottom of the dialog window. When you change selector one, selector two will be updated. You should be able to modify the code to suit your application.

These pages have some useful information you can refer to when reviewing the code I posted:

Creating a CKEditor Plugin with a Custom Dialog Window
http://docs.cksource.com/CKEditor_3.x/Tutorials/SimpleLink_Plugin_Part_1

Class CKEDITOR.dialog
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.html

Class CKEDITOR.dialog.definition
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.definition.html

Here's the code snippet to insert into the ckeditor/plugins/simpleLink/plugin.js file.
Just select lines ( 111 - 122 ) and paste this in:

{
  type : 'checkbox',
  id : 'newPage',
  label : 'Opens in a new page',
  // Default value.
  // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.ui.dialog.checkbox.html#constructor
  'default' : true,
  commit : function( data )
  {
    data.newPage = this.getValue();
  }
},
{
  type : 'select',
  id : 'selectorOne',
  label : 'Selector One',
  items :
  [
    [ '<none>', '' ],
    [ 'Set 1', 1],
    [ 'Set 2', 2 ],
    [ 'Set 3', 3 ]
  ],
  onChange : function()
  {
    var dialog = this.getDialog(),
      values = dialog.getContentElement( 'general', 'selectorTwo' ), // 'general' is the id of the dialog panel.
      selectedSet = parseInt(this.getValue());

    switch(selectedSet)
    {
    case 1:
      optionsNames = new Array("Set One <none>","Set One A","Set One B","Set One C"),
      optionsValues = new Array("","setOneA","setOneB","setOneC");
      break;
    case 2:
      optionsNames = new Array("Set Two <none>","Set Two A","Set Two B","Set Two C"),
      optionsValues = new Array("","setTwoA","setTwoB","setTwoC");
      break;
    case 3:
      optionsNames = new Array("Set Three <none>","Set Three A","Set Three B","Set Three C"),
      optionsValues = new Array("","setThreeA","setThreeB","setThreeC");
      break;
    default:
      optionsNames = new Array("<none>"),
      optionsValues = new Array("");
    }

    removeAllOptions( values );

    for ( var i = 0 ; i < optionsNames.length ; i++ )
    {
      var oOption = addOption( values, optionsNames[ i ],
        optionsValues[ i ], dialog.getParentEditor().document );
      if ( i == 0 )
      {
        oOption.setAttribute( 'selected', 'selected' );
        oOption.selected = true;
      }
    }
  },
  commit : function( data )
  {
    data.selectorOne = this.getValue();
  }
},
{
  type : 'select',
  id : 'selectorTwo',
  label : 'Selector Two',
  items :
  [
    [ '<none>', '' ]
  ],
  commit : function( data )
  {
    data.selectorTwo = this.getValue();
  }
},

Hope this does the trick, let me know if something isn't clear,
Joe


Hi oggiemc,

Good to know that you've had a chance to experiment with the code and it's great that it's been helpful.

Answers to the additional questions about the functions copied from the plugins/forms/dialogs/select.js file.

1) The three functions shouldn't be called when the dialog is opened. That's not occurring in the sample I created as outlined in my initial answer. You might want to search your code for the three function names to see if they are being called someplace other than the ( onChange : function() ) contained in the selectorOne definition.

2) I broke down the ( removeAllOptions() function ) and included it below. It has a similar line of code. For the line you asked about:

if (combo && oOption && oOption.getName() == 'option'),

It says If ( combo exists ) AND ( oOption exists ) AND ( oOption.getName() == 'option' ) the If statement is true.
By "exists", I mean it's not null and not something that evaluates to false (either 0 or a binary Not true).

3) Yes, in this particular usage of the addOption() function, the index variable isn't used. You can remove the two if statements and just leave the code statement from each else clause.

4) obj.getInputElement() returns the DOM element object, but obj.getInputElement().$ only returns the HTML tag for the DOM object. Try adding some console.log calls to the getSelect() function and you'll see the difference:

if ( obj && obj.domId && obj.getInputElement().$ ) {        // Dialog element.
  console.log(obj.getInputElement().$); // ADDED CONSOLE.LOG
  console.log(obj.getInputElement()); // ADDED CONSOLE.LOG

return obj.getInputElement();

Here is the flow for the removeAllOptions() function:
In the onChange() function for selectorOne we create a variable "values" which represents selectorTwo.
values = dialog.getContentElement( 'general', 'selectorTwo' )

Before populating selectorTwo with new option values based on the option selected in selectorOne, we remove any existing options from selectorTwo:
removeAllOptions( values ); // values represents selectorTwo

Within the removeAllOptions() function the variable "combo" is assigned the value that was passed in the function call, so:
combo = values = selectorTwo.

The removeAllOptions() function calls the getSelect() function and sends it the "combo" variable as a parameter.
The "combo" variable will be assigned the value that is returned by the getSelect() function.

Within the getSelect() function the variable "obj" is assigned the value that was passed in the function call, so:
obj = combo = values = selectorTwo.

The getSelect() function can return one of three values:
A) if ( obj && obj.domId && obj.getInputElement().$ )
If obj is true AND if obj.domId is true AND if obj.getInputElement().$ returns anything other than false or null, it will return the value of obj.getInputElement().
So, if the obj variable exists and the domId property of obj is set, it will run the getInputElement() function on obj and check that the DOM property ($) is set.
getInputElement() uses the domId to return the DOM element that corresponds to the selectorTwo element.

B) If not all three parts of A were true, the getSelect() function tries:
if ( obj && obj.$ )
So, if the obj variable exists and it already is the DOM element ($), it will just send back obj.

C) If step B fails, then getSelect() returns false and selectorTwo doesn't have any option elements to remove.

Now back to the removeAllOptions() function to see what happens if step A or B of getSelect() returns something.
In both cases the "combo" variable will be the selectorTwo object with the DOM element (combo.$) set.
It runs a while loop over the array of option elements inside selectorTwo and removes them.
It gets the first option element "getChild( 0 )" and removes it, the option element that was at position Child( 1 ) moves to position Child( 0 ) and the loop continues until there are no option elements left.

The getInputElement() function is located at line ( 1085 ) of ckeditor_source\plugins\dialogui\plugin.js
The getInputElement() function calls the getElement() function.
The getElement() function is located at line ( 2560 ) of ckeditor_source\plugins\dialog\plugin.js

function removeAllOptions( combo )
{
  combo = getSelect( combo );
  while ( combo.getChild( 0 ) && combo.getChild( 0 ).remove() )
  { /*jsl:pass*/ }
}

function getSelect( obj )
{
  if ( obj && obj.domId && obj.getInputElement().$ )        // Dialog element.
    return  obj.getInputElement();
  else if ( obj && obj.$ )
    return obj;
  return false;
}

Be Well,
Joe

聊慰 2024-12-05 11:07:31

是的,完全可以实现。在代码中,放置 2 个选择框。
然后在该插件的 js 代码中,将 clickhandler 绑定到选择框,将您想要的值放入第二个选择框中。

Yes, its totally possible to implement.. In the code, put 2 select boxes.
Then in the js code of that plugin, bind a clickhandler to the select box which puts the value u want in the second select box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文