加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 如何在Laravel中设置禁用选择选项?

发布时间:2020-12-14 19:56:18 所属栏目:大数据 来源:网络整理
导读:在控制器功能中,我提取了我已经使用过的所有属性和属性. 所有属性: $attributeNames = array('' = 'Select Attribute Name') + AttributeName::lists('name','id'); 已经采取的属性: $selectedAttributeNames = $xmlDocument-masterInformation-masterAttr
在控制器功能中,我提取了我已经使用过的所有属性和属性.

所有属性:

$attributeNames = array('' => 'Select Attribute Name') + AttributeName::lists('name','id');

已经采取的属性:

$selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes;

如何将selectedAttributeNames设为禁用?

这是var_dump($selectedAttributeNames)的输出:

object(Illuminate Database Eloquent Collection) #312 (1) {
    ["items":protected]= > array(1) {
        [0] => object(MasterAttribute) #310 (20) {
            ["table":protected]= > string(16) "master_attribute"
            ["guarded": protected] => array(1) {
                [0] => string(2) "id"
            }
            ["connection": protected] => NULL["primaryKey": protected] => string(2) "id"
            ["perPage": protected] => int(15)["incrementing"] => bool(true)["timestamps"] => bool(true)["attributes": protected] => array(7) {
                ["id"] => int(1)["xpath"] => string(17)
                "this is the xpath"
                ["attribute_name_id"] => int(1)["master_information_id"] => int(6)["default_value"] => string(25) "This is the default value"
                ["created_at"] => string(19) "2014-07-19 17:53:55"
                ["updated_at"] => string(19) "2014-07-19 17:53:55"
            }
            ["original": protected] => array(7) {
                ["id"] => int(1)["xpath"] => string(17) "this is the xpath"
                ["attribute_name_id"] => int(1)["master_information_id"] => int(6)["default_value"] => string(25) "This is the default value"
                ["created_at"] => string(19) "2014-07-19 17:53:55"
                ["updated_at"] => string(19) "2014-07-19 17:53:55"
            }
            ["relations": protected] => array(0) {}
            ["hidden": protected] => array(0) {}
            ["visible": protected] => array(0) {}
            ["appends": protected] => array(0) {}
            ["fillable": protected] => array(0) {}
            ["dates": protected] => array(0) {}
            ["touches": protected] => array(0) {}
            ["observables": protected] => array(0) {}
            ["with": protected] => array(0) {}
            ["morphClass": protected] => NULL["exists"] => bool(true)
        }
    }
}
不幸的是,Laravel的Form :: select()辅助方法没有提供一种方法来进入为select选项构建html的过程.

话虽如此,你有几种方法可以解决它:

第一:您可以创建自己的表单宏.这是过度简化的版本

Form::macro('select2',function($name,$list = [],$selected = null,$options = [],$disabled = []) {
    $html = '<select name="' . $name . '"';
    foreach ($options as $attribute => $value) {
        $html .= ' ' . $attribute . '="' . $value . '"';
    }
    $html .= '">';
    foreach ($list as $value => $text) {
        $html .= '<option value="' . $value . '"' .
            ($value == $selected ? ' selected="selected"' : '') .
            (in_array($value,$disabled) ? ' disabled="disabled"' : '') . '>' .
            $text . '</option>';
    }
    $html .= '</select>';
    return $html;
});

你可以在start.php中注册.

鉴于您首先将已经选择的项目的Illuminate Collection转换为一个简单的键组

$selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes;
$disabled = $selectedAttributeNames->toArray();

并且在您的视图中可以使用$attributeNames和$disabled,您可以像这样使用自定义宏

{{ Form::select2('mydropdown',$attributeNames,null,[],$disabled) }}

第二:您可以从您的选项数组中删除(例如使用array_diff_key())已选择的项目,而不是禁用它们:

{{ Form::select('mydropdown2',array_diff_key($attributeNames,$disabled),[]) }}

第三:在您的视图中,您可以吐出需要禁用的已选择属性的JavaScript数组,并使用jQuery或vanilla JS执行其余客户端.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读