【问题标题】:How to add another element to Yii radioButtonList template?如何向 Yii radioButtonList 模板添加另一个元素?
【发布时间】:2023-03-12 03:47:02
【问题描述】:

我需要每个元素后带有工具提示的 radioButtonList。

我使用这个代码:

<?php echo $form->radioButtonList($model, 'Field', CHtml::listData(FieldDataModel::model()->findAll(), 'id', 'name')); ?>

并得到以下结果:

[x] ParamName0
[ ] ParamName1
[ ] ParamName2

我怎样才能得到这样的结果:

[x] ParamName0 [?]-(Link, that shows description. Description is taking from FieldDataModel)
[ ] ParamName1 [?]
[ ] ParamName2 [?]

可能,'template' => '{input} {label}' 之类的模板可以帮助我吗?

【问题讨论】:

    标签: php html templates yii radiobuttonlist


    【解决方案1】:

    这在当前的实现中是不可能的。您必须遍历列表数据并使用CHtml::radioButton()CHtml::activeRadioButton() 手动创建按钮。

    【讨论】:

      【解决方案2】:

      作为替代解决方案,您仍然可以使用CActiveForm 并动态创建带有描述的所需链接。例如,它可能如下所示:

      <?php
      $listModels = FieldDataModel::model()->findAll();
      $form = $this->beginWidget('CActiveForm');
      ?>
      <div>
      <?php
      echo $form->radioButtonList($model, 'Field', CHtml::listData($listModels, 'id', 'name'));
      ?>
      </div>
      <?php
      $this->endWidget();
      
      $descriptions = array();
      foreach ($listModels as $listModel)
          $descriptions['id-' . $listModel->id] = $listModel->description;
      $modelClassName = get_class($model);
      Yii::app()->clientScript->registerScript("customRadioButtonListInit", "
          var descriptions = " . CJavaScript::encode($descriptions) . ";
          jQuery('#" . $modelClassName . "_Field input[type=\"radio\"]').each(function() {
              var valueAttr = jQuery(this).attr('value');
              var jqLabel = jQuery(this).next('label');
              var jqTooltipLink = jQuery('<a></a>')
                  .text('?')
                  .attr('title', descriptions['id-' + valueAttr])
                  .click(function(event) {
                      event.preventDefault();
                      event.stopPropagation();
                      // TODO: implement your tooltip here...
                      alert(jQuery(this).attr('title'));
                  });
              jqLabel.after(jqTooltipLink).after(' ');
          })
      ", CClientScript::POS_READY);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-20
        • 2011-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-15
        相关资源
        最近更新 更多