Javascript 使用knockout设置select2 ajax的初始值
我在设置select2 ajax控件的初始值时遇到问题。在这种情况下,我使用CI和knockout。我的代码将在按下时向表体添加一行,并且在表格单元格中使用select2 ajax 我的HTML:Javascript 使用knockout设置select2 ajax的初始值,javascript,jquery,ajax,codeigniter,knockout.js,Javascript,Jquery,Ajax,Codeigniter,Knockout.js,我在设置select2 ajax控件的初始值时遇到问题。在这种情况下,我使用CI和knockout。我的代码将在按下时向表体添加一行,并且在表格单元格中使用select2 ajax 我的HTML: <tbody data-bind="foreach: rows"> <tr> <td><input name="id_item[]" class="form-control big-drop" type="hidden" data-bind="valu
<tbody data-bind="foreach: rows">
<tr>
<td><input name="id_item[]" class="form-control big-drop" type="hidden" data-bind="value: id_item,select2: { minimumInputLength: 1, query: $root.list_item,formatResult: $root.itemFormatResult,formatSelection: $root.itemFormatSelection, allowClear: true}"></td>
<td><input placeholder="qty" name="qty[]" class="form-control number text-right" data-bind="value: qty,number: qty, numberOptions: {value: true, number_of_comma: 0}, valueUpdate: 'afterkeydown'"></td>
<td><input placeholder="price" name="price[]" class="form-control number text-right" data-bind="value: price,number: price, numberOptions: {value: true, number_of_comma: 2}, valueUpdate: 'afterkeydown', formatNoMatches: no_match_format"></td>
<td><button data-bind="click: $root.removeRow" class="btn btn-danger"><i class="fa fa-minus"></i></button></td>
</tr>
</tbody>
设置select2的值后,根据itemFormatSelection选择2不显示项目名称。
请帮帮我,非常感谢。抱歉,我的英语不好问题已解决,我使用select2的initSelection来完成此操作。调用initSelection时,执行ajax调用以获取JSON中的值 这是我的JS代码:
self.init_item = function (id_item, callback) {
var id = $(id_item).val();
$.ajax({
url: 'link to get JSON',
type: 'POST',
dataType: 'json',
data: {id: id},
success: function (data) {
callback(data);
}
});
};
添加此select2数据绑定:
initSelection: $root.init_item
就这些
self.init_item = function (id_item, callback) {
var id = $(id_item).val();
$.ajax({
url: 'link to get JSON',
type: 'POST',
dataType: 'json',
data: {id: id},
success: function (data) {
callback(data);
}
});
};
initSelection: $root.init_item