【问题标题】:How to set disabled select option in Laravel?如何在 Laravel 中设置禁用的选择选项?
【发布时间】:2014-09-10 16:28:18
【问题描述】:

在一个控制器函数中,我提取了所有attributes 和我已经使用的属性。

所有属性:

$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)
        }
    }
}

【问题讨论】:

  • this question 是否与您所追求的相似?
  • @halfer 也许我不知道。这个问题是使用刀片代码,但在我的情况下,我不知道如何使用它,因为选定属性的列表在控制器中。我可以将它发送到视图,但你能帮我构建select 标签吗?
  • 没看清楚P
  • @WereWolf-TheAlpha 好的,让我解释一下。我有一个想法来创造一些东西。该视图有一个形式。表单有一个带有选项的选择。当用户想再次使用该表单时,选择元素应禁用已选择的选项。请问你现在找到我了吗?
  • 您能否通过 var_dump($selectedAttributeNames) 显示 $selectedAttributeNames 中的内容?

标签: php laravel laravel-4 blade


【解决方案1】:

不幸的是,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), null, []) }}

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

【讨论】:

  • 谢谢解答,现在有点忙,今晚去查一下,+1
  • 这对我很有帮助。在 select 元素上设置 name 属性的行上有一个小错字。它缺少右双引号,应该是 $html = '
  • @MattyB 很好。谢谢。
猜你喜欢
  • 2015-06-28
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2016-01-21
  • 2021-01-13
  • 2016-05-06
  • 2019-10-04
相关资源
最近更新 更多