简体   繁体   English

django +用jquery标记

[英]django + tagging with jquery

I want to use jQuery and create a tagging interface for users. 我想使用jQuery并为用户创建标记界面。 Similar to how users in StackOverflow can add tags for the type of question they are asking. 类似于StackOverflow中的用户可以为他们提出的问题类型添加标记。 I'm getting it to work using jQuery and tagit: 我正在使用jQuery和tagit工作:

http://jquery.webspirited.com/2011/02/jquery-tagit-a-jquery-tagging-plugin/ http://jquery.webspirited.com/2011/02/jquery-tagit-a-jquery-tagging-plugin/

New location for the library: 图书馆的新位置:

https://github.com/hailwood/jQuery-Tagit https://github.com/hailwood/jQuery-Tagit

The problem is that say the user has an error in another part of the form and clicks submit, the form reloads with the error message and all the tags are gone. 问题是,如果用户在表单的另一部分中出现错误并单击“提交”,则表单将重新加载错误消息并且所有标记都将消失。 Is there a simplar way to get tagging in Django? 有没有一种简单的方法来获取Django中的标记?

[EDIT] [编辑]

Trying this based on Hailwood's response below... 根据Hailwood在下面的回复来试试这个......

<ul name="event_tag" class="tags">
    <li class="tagit-choice" tagvalue="3">
        Dog
        <a class="tagit-close">x</a>
    </li>
</ul>

However, when I load the page loads, that particular tag is not loaded? 但是,当我加载页面加载时,没有加载该特定标记? It seems like the ul tag is cleared and then there is other information loaded in it. 似乎清除了ul标签,然后加载了其他信息。 I do not see it when I load the page. 我加载页面时没有看到它。

I also tried as per below: 我也按以下方式尝试:

<ul name="event_tag" class="tags">
    <li data-value="3">Dog</li>
</ul>

When I try this way, it appears for a second and then it disappears... 当我尝试这种方式时,它会出现一秒然后消失......

[EDIT 2] [编辑2]

Found the solution to my problem. 找到解决我问题的方法。 As Hailwood suggested, programmatically we can create the li 's as per below: 正如Hailwood建议的那样,我们可以按照以下方式创建li

<ul name="event_tag" class="tags">
    <li data-value="3">Dog</li>
</ul>

Reason it wasn't working for me was because I had for initial values: 它不适合我的原因是因为我有初始值:

$.getJSON("ajaxrequest.json", function(data) {
    $(".tags").tagit("fill", data);
});

Problem for me was that onload, it was all disappearing. 对我来说问题在于,它正在消失。 Reason for this is because of fill . 原因是因为fill When we replace fill with add as per: $(".tags").tagit("add", data); 当我们用add替换fill时: $(".tags").tagit("add", data); , then it works. ,然后它的工作原理。

Being the creator the tagit plugin you mentioned I guess I could help you out. 作为你提到的tagit插件的创建者,我想我可以帮助你。

From what I can see it looks like you are using it as part of a form, and when you click submit you validate the form on the server side, if an error occurs you reload the page with the form values in tow? 从我可以看到它看起来你正在使用它作为表单的一部分,当你点击提交你验证服务器端的表单,如果发生错误你重新加载页面与表格值拖?

What I would suggest you do is enable the hidden select option ( select: true ). 我建议你做的是启用隐藏的选择选项( select: true )。

When the form is submitted you will get a list of the selected tags. 提交表单后,您将获得所选标签的列表。

Then, if you reload the page simply load the tags back into the list as <li> s. 然后,如果您重新加载页面,只需将标签重新加载到列表中<li> s。

(Please note I do not know Python, so the following is pseudo code) (请注意我不懂Python,所以以下是伪代码)

if(form_values.tags)
    for(tag in form_values.tags)
        print '<li data-value=" '+tag.value+' "> '+tag.label+' </li>';
    endfor;
endif;

*You could also pass the tags to the initialTags option, but that would require outputting javascript, which imo is less tidy than the above method :) *你也可以将标签传递给initialTags选项,但这需要输出javascript,这比上面的方法更整洁:)

You should checkout jQuery Select2's support for tagging. 你应该检查jQuery Select2对标记的支持。

http://ivaynberg.github.com/select2/#tags http://ivaynberg.github.com/select2/#tags

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

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