简体   繁体   English

设置带剔除的select2 ajax的初始值

[英]Set initial value of select2 ajax with knockout

I have a problem to set initial value of select2 ajax control. 我在设置select2 ajax控件的初始值时遇到问题。 I'm using CI and knockout in this case. 在这种情况下,我使用CI和淘汰赛。 My code will add one row to table body when pressed and i use select2 ajax in table cell. 当按下时,我的代码将向表主体添加一行,而我在表单元格中使用select2 ajax。

My HTML: 我的HTML:

<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>

My KO model: 我的KO模型:

function Row(id_item, qty, price) {
    var self = this;
    self.id_item = ko.observable(id_item);
    self.qty = ko.observable(qty);
    self.price = ko.observable(price);

    ko.computed(function () {
        var item = self.id_item();

        get_satuan(item).done(function (data) {
            if (!isNaN(data.price)){
                self.harga(format_number(data.price, ''));
            }
        });
    });
}

var RowModel = function(rows) {
    var self = this;

    var default_array = ko.observableArray();
    if (detail.length > 0){
        $.each(detail, function(key, value){
            default_array.push(new Row(value.id_item, value.qty, value.price));
        });
    }

    self.rows = default_array;
    self.addRow = function() {
        self.rows.push(new Row('TUT',"","1"));
    };

    self.removeRow = function(row) { self.rows.remove(row) }

    self.list_item = function (query) {
        $.ajax({
            url: 'link to get json',
            type: 'POST',
            dataType: 'json',
            data: {q: query.term},
            success: function (data) {
                query.callback({
                    results: data
                });
            }
        });
    };
    self.itemFormatResult = function(item) {
        var markup = "<table class='movie-result'><tr><td><div class='movie-title'>";
        markup += "<b>" + item.id + "</b>";
        markup += "<br>" + item.item_name;
        markup += "<br>" + item.unit;
        markup += "</div></td></tr></table>";
        return markup;
    }
    self.itemFormatSelection = function (item) {
        return item.item_name;
    };
};

ko.applyBindings(new RowModel());

Everything works well: 1. adding new row 2. selecting item using select2 works fine 3. after selecting item, the price will change based on item selection, works fine 一切正常:1.添加新行2.使用select2选择项目效果很好3.选择项目后,价格将根据项目选择而变化,效果很好

The problem is in this code: 问题出在以下代码中:

self.addRow = function() {
        self.rows.push(new Row('TUT',"","1"));
};

After setting the value of select2, select2 not displaying the item_name based on itemFormatSelection . 设置select2的值后,select2不基于itemFormatSelection显示item_name。 Please help me, thanks a lot. 请帮助我,非常感谢。 Sorry for my bad english 对不起,我的英语不好

Problem solved, I use select2's initSelection to do this. 问题解决了,我使用select2的initSelection来做到这一点。 When calling initSelection do an ajax call to get the value in JSON. 调用initSelection时,请执行ajax调用以获取JSON中的值。

This is my JS Code: 这是我的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);
        }
    });
};

Add this select2 data-bind: 添加此select2数据绑定:

initSelection: $root.init_item

That's all 就这样

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

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