Ted*_*Ted 4 jquery jquery-select2 x-editable
我有以下几个绑定多个锚元素与类'团队'是x可编辑的select2输入.每个a.team都有一个不同的数据源(传递给Web服务的ID不同,以便返回适用的团队列表),但不幸的是,一旦绑定了第一个a.team,就会使用该数据源URL对于页面上的所有后续a.team输入(因此,错误的团队列表将绑定到每个后续的x-editable select2输入).
是否可以"重置"select2的data属性,以便它尊重每个a.team元素的每个数据源?或任何其他解决方案?
$('a.team').editable({
ajaxOptions: {
dataType: 'json',
type: 'POST'
},
emptytext: 'TBD',
placement: 'bottom',
success: function (response, newValue) {
return editableResponse(response, newValue);
},
select2: {
allowClear: true,
placeholder: 'Select a team',
width: '200px',
id: function (item) {
return item.id;
},
ajax: {
dataType: 'json',
results: function (data, page) {
return { results: data };
}
},
}
});
Run Code Online (Sandbox Code Playgroud)
多个a.team锚定在页面上,如下所示:
<a href="#" class="ur-team label label-inverse" data-type="select2" data-pk="@match.Id" data-source="@Url.Action("GetTeams", "Teams", new { scheduleId = match.ScheduleId })" data-value="@match.AwayTeamId" data-text="@match.AwayTeam" data-name="away" data-title="Update away team" data-url="@Url.Action("UpdateTeam", "AdminMatches")">@match.AwayTeam</a>
Run Code Online (Sandbox Code Playgroud)
注意:我已经验证只有第一个x-editable select2输入的ID用于所有其他select2 AJAX调用.换句话说,它不是数据缓存问题(它是"一旦绑定,所有其他数据源引用被忽略"问题).
更新:这是一个复制问题的快速而肮脏的小提琴:http://jsfiddle.net/ovalsquare/k9b3d/8/.请注意,最终都绑定到list2而不是list,然后是list2.
不确定这是否是您已经考虑过的工作之一,或者这可能会如何影响应用程序的性能.但是.team单独初始化每个元素都有效.
我将再研究一下,看看我是否能找到更好的方法,但这是目前的解决方案:
$('a.team').each(function(){
$(this).editable({
ajaxOptions: {
dataType: 'json',
type: 'POST'
},
emptytext: 'TBD',
placement: 'bottom',
success: function (response, newValue) {
return editableResponse(response, newValue);
},
select2: {
allowClear: true,
placeholder: 'Select a team',
width: '200px',
id: function (item) {
return item.id;
},
ajax: {
dataType: 'json',
results: function (data, page) {
return { results: data };
}
},
}
});
});
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/trevordowdle/k9b3d/11/
更好的解决方法是在editable没有源数据属性的情况下进行初始化:
<a href="#" class="team" data-type="select2" data-pk="1" data-getSource="/list" data-value="100" data-text="Team A" data-name="home" data-title="Update home team" data-url="/post">Team A</a>
Run Code Online (Sandbox Code Playgroud)
然后.team通过设置源通过初始化为每个添加它option:
$('a.team').each(function(){
$(this).editable('option','source',$(this).data('getSource'));
});
Run Code Online (Sandbox Code Playgroud)
但不幸的是,当设置source这种方式时它不起作用.我能找到的最佳解决方案是上面的解决方案.
看起来这里有一些可编辑的错误:
初始化多个具有不同的选择可编辑时sources.并使用该option方法设置源.