Laravel-admin: select load 二级联动 编辑 时不显示默认值

Created on 18 Apr 2019  ·  4Comments  ·  Source: z-song/laravel-admin

  • Laravel Version: 5.5
  • PHP Version:7.1
  • Laravel-admin: 1.6.11

修改select.php


$(document).off('change', "{$this->getElementClassSelector()}");
$(document).on('change', "{$this->getElementClassSelector()}", function () {
var target = $(this).closest('.fields-group').find(".$class");
$.get("$sourceUrl?q="+this.value, function (data) {
target.find("option").remove();
$(target).select2({
data: $.map(data, function (d) {
d.id = d.$idField;
d.text = d.$textField;
return d;
})
}).trigger('change');

});

});

改成

$(document).off('change', "{$this->getElementClassSelector()}");
$(document).on('change', "{$this->getElementClassSelector()}", function () {
var target = $(this).closest('.fields-group').find(".$class");
$.get("$sourceUrl?q="+this.value, function (data) {
target.find("option").remove();
$(target).select2({
data: $.map(data, function (d) {
d.id = d.$idField;
d.text = d.$textField;
return d;
})
}).trigger('change');

var value = target.data('value') + '';

if (value) {
value = value.split(',');
target.select2('val', value);
}
});
});

Steps To Reproduce:

wontfix

Most helpful comment

as following, it will be ok:

        $script = <<<EOT
$(document).off('change', "{$this->getElementClassSelector()}");
$(document).on('change', "{$this->getElementClassSelector()}", function () {
    var target = $(this).closest('.fields-group').find(".$class");
    $.get("$sourceUrl?q="+this.value, function (data) {
        target.find("option").remove();
        $(target).select2({
            data: $.map(data, function (d) {
                d.id = d.$idField;
                d.text = d.$textField;
                return d;
            })
        });

        if (target.data('value')) {
          var value = target.data('value');
          target.val(value).trigger('change');
        } else {
          target.trigger('change');
        }
    });
});
$('{$this->getElementClassSelector()}').trigger('change');
EOT;

All 4 comments

好像只要是select2,使用ajax加载数据,都会这样不设置edit加载的值。

I have the same problem

as following, it will be ok:

        $script = <<<EOT
$(document).off('change', "{$this->getElementClassSelector()}");
$(document).on('change', "{$this->getElementClassSelector()}", function () {
    var target = $(this).closest('.fields-group').find(".$class");
    $.get("$sourceUrl?q="+this.value, function (data) {
        target.find("option").remove();
        $(target).select2({
            data: $.map(data, function (d) {
                d.id = d.$idField;
                d.text = d.$textField;
                return d;
            })
        });

        if (target.data('value')) {
          var value = target.data('value');
          target.val(value).trigger('change');
        } else {
          target.trigger('change');
        }
    });
});
$('{$this->getElementClassSelector()}').trigger('change');
EOT;

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qcol picture qcol  ·  3Comments

evans-kim picture evans-kim  ·  3Comments

chenyongmin picture chenyongmin  ·  3Comments

MarKco picture MarKco  ·  3Comments

fokoz picture fokoz  ·  3Comments