问题描述
我在 mongodb 中玩家和锦标赛之间存在多对多关系.
I have a many-to-many relationship in mongodb between Players and Tournaments.
我希望能够一次将许多玩家添加到锦标赛中.这在没有 ajax 的情况下是微不足道的,但我们有数千名玩家的数据库,因此表单选择变得巨大.
I want to be able to add many Players to a Tournament at once. This is trivial to do without ajax, but we have a DB of thousands of Players, and so the form select becomes huge.
我们想为此使用ajax.是否可以创建一个小部件(使用 js)来正确处理这个问题?如果是这样,有关使用什么 jquery 插件(或其他)的任何提示?
We want to use ajax for this. Is it possible to create a single widget (with js) to handle this properly? If so, any hints on what jquery plugin (or other) to use?
如果不是,那么执行此操作的标准策略是什么?我想我可以大量更改此表单的视图并使用 ajax 自动完成一次添加一个玩家,然后再添加一些代码一次删除每个玩家.但是,我真的很想拥有一个可以重复使用的小部件,因为它更干净,而且效率更高.
If not, whats the standard strategy to do this? I suppose I could heavily change the view for this form and use an ajax autocomplete to add one player at a time, and then some more code to delete each player one at a time. However, I'd really like to have a single widget I can re-use because its so much cleaner and seems much more efficient.
我一整天都在玩 Select2(类似于 jQuery Chosen)并且我让它为通过 ajax 添加许多玩家,但它不允许我在最初加载页面时设置已经附加的玩家,所以我将无法看到谁已经在锦标赛中,并且必须重新输入每个人.
I have been playing with Select2 all day (similar to jQuery Chosen) and I have it working for adding many Players via ajax, but it does not allow me to set the already attached players when I initially load the page, so I won't be able to see who's already in the tournament and would have to retype everyone in.
感谢您对此事的任何意见!我无法通过谷歌找到任何东西.
Thanks for ANY input on this matter! I can't find anything via Google.
推荐答案
我能够在 onload 函数中的构造函数之后通过 $.ajax
完成此操作,其中 //website/jsonItem
是所有项目的 json 编码列表,而 //website/jsonItemUser
是附加到用户的所有项目的 json 编码列表.我使用 //
来保持调用之间的 https/http
一致.
I was able to accomplish this by $.ajax
after the constructor within the onload function where //website/jsonItem
is a json-encoded list of all items, and //website/jsonItemUser
is a json-encoded list of all items attached to user. I used //
to keep the https/http
consistent between calls.
$(document).ready(function(){
$('.selectitem').select2({
minimumInputLength:0
,multiple: true
,ajax: {
url: "//website/jsonItem"
,dataType: 'jsonp'
,data: function (term, page) {
return {
q: term, // search term
limit: 20,
page: page
};
}
,results: function (data, page) {
var more = (page * 20) < data.total;
return {
results: data.objects, more: more
};
}
}
,initSelection: function(element, callback){
var items=new Array();
$.ajax({
url: "//website/jsonItemUser"
});
callback(items);
}
});
$.ajax({
url: "//website/jsonItemUser"
,dataType: 'jsonp'
,success: function(items, status, ob) {
$('.selectitem').select2('data',items);
}
});
});
这篇关于多对多 Ajax 表单(Symfony2 表单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!