如何在Laravel中设置禁用的选择选项

如何在Laravel中设置禁用的选择选项

本文介绍了如何在Laravel中设置禁用的选择选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制器功能中,我提取所有attributes和已经使用的属性.

In a controller function, I extract all the attributes and the attributes that I have already used.

所有属性:

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

已经使用的属性:

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

如何将selectedAttributeNames设置为disable?

这是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.

Unfortunately Laravel's Form::select() helper method doesn't provide a way to tap into the process of building html for select's options.

话虽这么说,您有几种解决方法:

That being said you have several ways to go about it:

首先:您可以创建自己的表单宏.这是过分简化的版本

First: You can create your own form macro. Here is oversimplified version

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转换为简单的键数组

Given that you first get convert Illuminate Collection with already selected items into a plain array of keys

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

并在您的视图中同时创建$attributeNames$disabled,您可以像这样使用自定义宏

and maker both $attributeNames and $disabled available in your view you can use your custom macro like this

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

第二步:,您只需从选项阵列中删除(例如,使用array_diff_key())已选择的项目,而不是禁用它们:

Second: you can just remove (e.g. with array_diff_key()) already selected items from your array of options instead of disabling them:

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

第三步:在您看来,您可以吐出已经选择的需要禁用的属性的JavaScript数组,并使用jQuery或Vanilla JS进行其余的客户端操作.

Third: in your view you can spit a JavaScript array of already selected attributes that need to be disabled and do the rest client-side with jQuery or vanilla JS.

这篇关于如何在Laravel中设置禁用的选择选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:01