简体   繁体   English

与jQuery X-Editable库一起使用时,修改Select2下拉列表选择的输出

[英]Modify the output of the Select2 dropdown selection when used with jQuery X-Editable library

I have been trying to use X-Editable http://vitalets.github.io/x-editable/ with Select2 https://select2.github.io/ for weeks now without much luck. 我一直试图使用X-Editable http://vitalets.github.io/x-editable/与Select2 https://select2.github.io/连续几周没有太多运气。

Originally I was trying to load in data from AJAX request and then use the library but all I could see if old code that supposedly worked with the older versions of both libraries. 最初我试图从AJAX请求中加载数据,然后使用该库,但是我可以看到所有旧代码是否适用于两个库的旧版本。

To slightly simplify things I have now decided to load in my data separately so that X-Editable and Select2 only have to deal with an array of data that is already available. 为了略微简化,我现在决定分别加载我的数据,以便X-Editable和Select2只需要处理已经可用的数据数组。

My problem now is modifying the display of the data. 我现在的问题是修改数据的显示。

By default I am happy with the look of the edit field which looks like "tags" being selected. 默认情况下,我很满意编辑字段的外观,看起来像是“标签”被选中。

When editing is completed and on initial page load, it shows my selected items as a comma separated string. 编辑完成后,在初始页面加载时,它会将我选择的项目显示为逗号分隔的字符串。 I would like to modify that part to look different. 我想修改那个部分看起来不一样。

I saw some examples that worked as I want it to however they do not seem to work with the newer versions. 我看到了一些按照我的要求工作的示例,但它们似乎不适用于较新的版本。

This JSFiddle here http://jsfiddle.net/jasondavis/j72k110m/ shows my desired output and functionality...almost but the catch is this demo is using older versions of the Select2 library. 这里的JSFiddle http://jsfiddle.net/jasondavis/j72k110m/显示了我想要的输出和功能...差不多但是这个演示的问题是使用旧版本的Select2库。 It has Select2 version v3.4.4 and X-Editable version v1.5.1 它有Select2版本v3.4.4X-Editable版本v1.5.1

The latest version of Select2 is version v4.0.0 X-Editable is up to date at v1.5.1 as it has not been updated in a while. 最新版本的Select2版本v4.0.0 X-Editable是最新的v1.5.1,因为它有一段时间没有更新。


On initial load and after selecting new value it should look like this below which means it wraps our selected value in a span : 在初始加载和选择新值之后,它应该如下所示,这意味着它将我们选择的值包装在一个span

在此输入图像描述


When clicked to edit the selected values, it should look like this which looks like "tags" however it should not show the <span> part! 单击以编辑选定的值时,它应该看起来像“标签”,但它不应显示<span>部分!

在此输入图像描述


In this JSFiddle http://jsfiddle.net/jasondavis/N6bQE/320/ 在这个JSFiddle http://jsfiddle.net/jasondavis/N6bQE/320/
I have updated Select2 to the new version and I am trying to replicate the functionality from the other JSFiddle above by showing the selected values as "tags". 我已经将Select2更新为新版本,我试图通过将所选值显示为“标签”来复制上面其他JSFiddle的功能。

Another issue with this version is that when selecting values, it does not remove an option from the dropdown list. 此版本的另一个问题是,在选择值时,它不会从下拉列表中删除选项。 So even though 1 item is selected already, it still shows up as an option to click on again! 因此,即使已经选择了1个项目,它仍会显示为再次单击的选项!

The other issue is that new selected items do not get added on the non-edit screen. 另一个问题是新选择的项目不会添加到非编辑屏幕上。

More images below to show what I mean... 下面的更多图片显示我的意思...

在此输入图像描述

Image below shows the intial screen load selected items and also items that are selected and not in edit-mode. 下图显示了初始屏幕加载了所选项目以及选择但未处于编辑模式的项目。 The issue is that it is not updating with new items that are selected. 问题是它没有使用所选的新项目进行更新。 Also it shows the selected ID's instead of a title/name 它还显示所选的ID而不是标题/名称

My end goal is to simply use Select2 on my X-editable field to allow the selection of an Assigned User. 我的最终目标是在我的X-editable字段上使用Select2以允许选择已分配的用户。 Selected assigned users will show a gravatar thumbnail image and the user name. 选定的指定用户将显示一个重力图像缩略图图像和用户名。

Both Select2 and X-Editable allow the use of a template style output to modify there output however when they are working together, there default functionality is different and those output modification functions do not work the same as they do when running either library on it;s own. Select2和X-Editable都允许使用模板样式输出来修改输出,但是当它们一起工作时,默认功能不同,那些输出修改功能与它们在其上运行任何库时的工作方式不同;自己的。

I know what I want can be done since the 2 libraries are meant to work together, it's just a matter of getting the newer versions of Select2 to work with the older version of X-Editable which has not been updated in a long time sadly! 我知道我想要的是可以完成的,因为2个库可以协同工作,只需要让较新版本的Select2与旧版本的X-Editable一起工作,这在很长一段时间内都没有得到更新!

在此输入图像描述在此输入图像描述

The final jsfiddles that I was testing on are at https://jsfiddle.net/N6bQE/329/ (bootstrap-editable) and https://jsfiddle.net/N6bQE/331/ (poshytip-editable). 我正在测试的最终jsfiddles是https://jsfiddle.net/N6bQE/329/(bootstrap-editable )和https://jsfiddle.net/N6bQE/331/(poshytip-editable )。

Here are some observations I made while trying to make Select2 4.0.0 work with X-Editable. 以下是我在尝试使用X-Editable使Select2 4.0.0工作时所做的一些观察。

  1. You were setting data-value to apples, oranges, pie (note the spaces), which is technically not correct. 您将data-value设置为apples, oranges, pie (注意空格),这在技术上是不正确的。 The ids are supposed to match up exactly, but due to ids应该完全匹配,但由于 an unfortunate mistake 一个不幸的错误 a feature in older versions of Select2, extra whitespace was completely ignored. 旧版本的Select2中的一个功能 ,完全忽略了额外的空格。

    Removing the spaces solves the main issue, which was Select2 not matching options correctly (and as a result not displaying them). 删除空格解决了主要问题,即Select2无法正确匹配选项(因此不显示它们)。

  2. Your source did not contain any of the values you were passing in through data-value , so Select2 had no way of displaying them. 您的source不包含您通过data-value传递的任何 data-value ,因此Select2无法显示它们。 By lining some of the id attributes up, Select2 was one step closer to displaying the selected values when the popover was displayed. 通过将某些id属性向上排列,Select2距离显示弹出窗口时显示所选值的距离更近了一步。

  3. You were using both tags and source , which X-Editable does not allow. 你正在使用X-Editable不允许的tagssource The plugin appeared to prioritize tags over source , so nothing was actually being passed into Select2 and your source was totally ignored. 该插件似乎优先考虑source tags ,因此实际上没有任何内容传递到Select2,并且您的source完全被忽略。

  4. But that doesn't matter too much, because X-Editable isn't displaying the values correctly in the first place. 但这并不重要,因为X-Editable首先没有正确显示值。 You'll notice that the data-value is being displayed as a single tag, instead of being split up and displayed as multiple tags. 您会注意到data-value显示为单个标记,而不是分割并显示为多个标记。 In order to get X-Editable to cooperate with this, you need to set separator: ',' in your Select2 options. 为了让X-Editable与之合作,您需要在Select2选项中设置separator: ',' While this option is no longer supported for Select2, X-Editable completely ignores the viewseparator option in favor of this one. 虽然Select2不再支持此选项,但X-Editable完全忽略了viewseparator选项以支持此选项。

  5. In order to get the tags to update after they are edited, you need to check if value in the display method is an array or not. 为了编辑标签让标签更新,您需要检查display方法中的value是否为数组。 This is because X-Editable just passes back a string to the display method, instead of the array that one would expect. 这是因为X-Editable只是将字符串传递回display方法,而不是人们期望的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM