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

标签 php laravel laravel-4 laravel-blade

在一个 Controller 函数中,我提取了所有的属性和我已经使用过的属性。

所有属性:

$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 的过程。

话虽如此,您有几种方法可以解决这个问题:

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

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 在您的 View 中可用您可以像这样使用您的自定义宏

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

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

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

第三::在您的 View 中,您可以吐出一个 JavaScript 数组,其中包含需要禁用的已选属性,其余的客户端使用 jQuery 或 vanilla JS 完成。

关于php - 如何在 Laravel 中设置禁用的选择选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24843547/

相关文章:

javascript - 如何将 img src 传递到 Bootstrap 模式中?

php - SQLSTATE[HY000] : General error: 1205 Lock wait timeout exceeded on INSERT query

css - Laravel + Angular : Securing Angular Pages

Laravel Homestead : Nginx failing to start on Vagrant. 需要root密码才能访问Nginx日志

php - 如何在 Laravel 4 上路由子资源?

php - 设置 IF 语句

php - 将Mysql数据库转换成Json

php - 全栈与非全栈 MVC PHP 框架——有什么区别?

mysql - 检索 Laravel 模型查询匹配的条件

php - Laravel 从 ROOT 外部调用/public 文件夹。获取 NotFoundHttpException