欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

laravel-admin解決表單select聯(lián)動(dòng)時(shí),編輯默認(rèn)沒選上的問題

 更新時(shí)間:2019年09月30日 17:22:42   作者:佛布朗斯基  
今天小編就為大家分享一篇laravel-admin解決表單select聯(lián)動(dòng)時(shí),編輯默認(rèn)沒選上的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

今天在開發(fā)公司一個(gè)功能時(shí),公司開發(fā)環(huán)境用的是laravel-admin,因?yàn)樾枰蒙蟬elect聯(lián)動(dòng),所以根據(jù)文檔說明進(jìn)行開發(fā),并成功的使用上了,代碼我就不重復(fù),大家可以去參考laravel-admin官網(wǎng)的說明。

首先我們找到select的js,路徑:跟目錄/vendor/encore/laravel-admin/src/Form/Field下的Select.php文件,找到下面代碼:

$script = <<<EOT
$(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');
 });
});
 
EOT;

并修改成以下代碼:

$script = <<<EOT
$(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');
 });
});
 
$('{$this->getElementClassSelector()}').trigger('change');
 
EOT;

我們在原有代碼中加入這句:

$('{$this->getElementClassSelector()}').trigger('change');

作用就是在初始化的時(shí)候觸發(fā)一次聯(lián)動(dòng)。

然后在我們的表單中,我們再來定義編輯初始時(shí)候的值,代碼如下:

$form->select('hezuo', "合作模式")->options(function () {
 $record = request()->route()->parameters();
 $record = $record["chanpin"];
 $data = ChanpinModel::where('id', $record)->first();
 $hezuoList = array(
  "1" => '測試1',
  "2" => '測試2',
  "3" => '測試3',
  );
 $hezuo = $data->hezuo;
 return [$hezuo => $hezuoList[$hezuo]];
});

這段代碼是根據(jù)當(dāng)前記錄的值,去獲取對應(yīng)所屬那個(gè)選項(xiàng),這樣便使select聯(lián)動(dòng)編輯時(shí),能夠默認(rèn)選上我們的值。

以上這篇laravel-admin解決表單select聯(lián)動(dòng)時(shí),編輯默認(rèn)沒選上的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論