简体   繁体   中英

how to disable to select one or many option from dropdown list in cakephp?

I want to disable some options from dropdown list, i have an array like that

  array(
'all' => 'ALL',
'skip1' => 'User Define Groups:',
(int) 43 => '--Usii Group2',
(int) 105 => '--Usii Mailing [ mailing list]',
(int) 106 => '--test [ mailing list]',
'skip2' => 'Dynamic Define Groups:'


i want to disable value of skip1 and skip2, if user click on skip1 and skip2 value it can't be select in dropdown list, this is my view file     


    echo $this->FormManager->input('view',array('label'=>'View ','type'=>'select','options'=>$viewGroup,'default'=>$default)); 

any one can help to do this, it will be appreciated, thanks in advance.

I think you should disable options from client side, ie from Jquery something like this

HTML

<select>
    <option value="all">ALL/option>
    <option value="skip1">User Define Groups:</option>
    <option value="43 ">--Usii Group2</option>
    <option value="105">--Usii Mailing [ mailing list]</option>
    <option value="106">--test [ mailing list]</option>
    <option value="skip2">'Dynamic Define Groups:</option>
</select>

JQuery

$('option[value=skip1]').prop('disabled', true);
$('option[value=skip2]').prop('disabled', true);

To complement an answer from Moyed Ansari: You can use .attr jquery function.

$('option[value=skip1]').attr('disabled', true);
$('option[value=skip2]').attr('disabled', true);

Use an array of arrays.

$values = array(
  'all' => 'all',
  'skip1' => array(
    5 => 'ex',
    6 => 'ex',
    7 => 'ex',
  ),
  'skip2' => array(
    5 => 'ex',
    6 => 'ex',
    7 => 'ex',
  )
)

See here: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select

Try with re-arrange the array by following way:

 array(
      'all' => 'ALL',
      'skip1' => array(
           'name' => 'User Define Groups:',
           'value' => 'skip1',
           'disabled' => true
      )
      (int) 43 => '--Usii Group2',
      (int) 105 => '--Usii Mailing [ mailing list]',
      (int) 106 => '--test [ mailing list]',
      'skip2' => (
           'name' => 'Dynamic Define Groups:'
           'value' => 'skip2',
           'disabled' => true
      )
 )

Or you can simply try this on your view :

echo $this->FormManager->input('view',array('label'=>'View ','type'=>'select','options'=>$viewGroup,'default'=>$default, 'disabled'=>array('skip1','skip2')));

Both of those do not require any JavaScript or jQuery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM