Yii 1.1.17 Select2-选择的值不会更新数据库

时间:2019-05-17 15:58:45

标签: php yii yii1.x

我正在尝试使用select2查询数据库,因为有30000条记录,这实际上是我能想到的唯一有效的方法。

我的问题是,如果我只提交此表单,pole_id不会更新。

有人可以帮助jQuery获取ID或告诉我为什么选择框不更新数据库中的pole_id字段吗?

<?php
/* @var $this JpapolesController */
/* @var $model Jpapoles */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'jpapoles-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'pole_id'); ?>
        <?php //echo $form->textField($model,'pole_id',array('id'=>'pole_id')); ?>

        <?php 
        $list = CHtml::listData(Poles::model()->findAll(array('order' => 'pole_number')), 'id', 'pole_number');
        //echo $form->dropDownList($model, 'pole_id', $list, array('class'=>"js-example-basic-multiple", 'name'=>'pole_id', 'multiple'=>'multiple'));
        //
        echo CHtml::hiddenField('selectbox_pole_id', '', array('class' => 'span5'));

        $this->widget('ext.select2.ESelect2',array(
            'id'=>'myselect',

            'selector' => '#selectbox_pole_id',
            'options'  => array(
                'allowClear'=>true,
                'placeholder'=>'Select a Pole',
                'minimumInputLength' => 3, 

                   'ajax' => array(
                    'url' => Yii::app()->createUrl('jpapoles/poles'),
                     'type'=>'GET',
                    'dataType' => 'json',
                    'quietMillis'=> 100,
                    'data' => 'js: function(text,page) {
                    return {
                                q: text, 
                                page_limit: 10,
                                page: page,
                            };

                        }',

                    'results'=>'js:function(data,page) { var more = (page * 10) < data.total; return {results: data, more:more }; 

                    }

                    ',
                      ),

                   ),

        ));


        ?>
        <?php echo $form->error($model,'pole_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'member_id'); ?>
        <?php //echo $form->textField($model,'member_id'); ?>
        <?php 
        $list = CHtml::listData(Members::model()->findAll(array('order' => 'abriviation')), 'id', 'abriviation');
        echo $form->dropDownList($model, 'member_id', $list);
        ?>
        <?php echo $form->error($model,'member_id'); ?>
    </div>

    <div class="row">
        <?php //echo $form->labelEx($model,'jpa_id'); ?>
        <?php echo $form->hiddenField($model,'jpa_id', array('value'=>$_GET['jpano'])); ?>
        <?php echo $form->error($model,'jpa_id'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->
<script type="javascript">

$("#myselect").on("change", function (e) { 
    var id = $("#myselect").select2("data")[0].id;

   // Now you can work with the selected value, i.e.:
   //$("#pole_id").val(id);
   alert(id);
});



</script>

2 个答案:

答案 0 :(得分:0)

两个部分:

我决定在控制器操作中使用var_dump:

这是导致我发现ID为:selectbox_pole_id的隐藏字段的地方。

因此,我重命名了该ID以匹配数据库ID pole_id,并在保存之前添加了以下行:$ model-> pole_id = $ _POST ['pole_id'];

public function actionCreate()
{
    $model=new Jpapoles;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Jpapoles']))
    {
        $model->attributes=$_POST['Jpapoles'];
        $model->pole_id = $_POST['pole_id'];
        if($model->save())
            //$this->redirect(array('jpas/view','id'=>$_GET['jpano']));
            var_dump($_REQUEST);
        //echo $_POST['pole_id'];
    }

    $this->renderPartial('create',array(
        'model'=>$model
    ),false,true);
}
/* @var $this JpapolesController */
/* @var $model Jpapoles */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'jpapoles-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'pole_id'); ?>
        <?php 

        echo CHtml::hiddenField('pole_id', '', array('class' => 'span5'));
        $this->widget('ext.select2.ESelect2',array(
            'id'=>'myselect',
            'selector' => '#pole_id',
            'model'=>$model,
            'attribute'=>'pole_id', 
            'options'  => array(
                'allowClear'=>true,
                'placeholder'=>'Select a Pole',
                'minimumInputLength' => 3, 

                   'ajax' => array(
                    'url' => Yii::app()->createUrl('jpapoles/poles'),
                     'type'=>'GET',
                    'dataType' => 'json',
                    'quietMillis'=> 100,
                    'data' => 'js: function(text,page) {
                    return {
                                q: text, 
                                page_limit: 10,
                                page: page,
                            };

                        }',

                    'results'=>'js:function(data,page) { var more = (page * 10) < data.total; return {results: data, more:more }; 

                    }

                    ',
                      ),

                   ),

        ));


        ?>
        <?php echo $form->error($model,'pole_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'member_id'); ?>
        <?php //echo $form->textField($model,'member_id'); ?>
        <?php 
        $list = CHtml::listData(Members::model()->findAll(array('order' => 'abriviation')), 'id', 'abriviation');
        echo $form->dropDownList($model, 'member_id', $list);
        ?>
        <?php echo $form->error($model,'member_id'); ?>
    </div>

    <div class="row">
        <?php //echo $form->labelEx($model,'jpa_id'); ?>
        <?php echo $form->hiddenField($model,'jpa_id', array('value'=>$_GET['jpano'])); ?>
        <?php echo $form->error($model,'jpa_id'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->
<script type="text/javascript">
// Or for single select elements:
$("#select").select2({
    initSelection : function (element, callback) {
        var data = {id: element.val(), text: element.val()};
        callback(data);
    }
});

</script>











答案 1 :(得分:0)

您可以仅使用CActiveForm::hiddenField()生成此隐藏的输入:

<div class="row">
    <?php echo $form->labelEx($model, 'pole_id'); ?>

    <?php
    echo $form->hiddenField($model, 'pole_id', ['id'=>'pole_id']);

    $this->widget('ext.select2.ESelect2', [
        'id' => 'myselect',
        'selector' => '#' . CHtml::activeId($model, 'pole_id'),
        'options' => [
            'allowClear' => true,
            'placeholder' => 'Select a Pole',
            'minimumInputLength' => 3,

            'ajax' => [
                'url' => Yii::app()->createUrl('jpapoles/poles'),
                'type' => 'GET',
                'dataType' => 'json',
                'quietMillis' => 100,
                'data' => 'js: function(text,page) {
                    return {
                        q: text, 
                        page_limit: 10,
                        page: page,
                    };
                }',
                'results' => 'js:function(data,page) { 
                    var more = (page * 10) < data.total; return {results: data, more:more }; 
                }',
            ],
        ],
    ]);
    ?>
    <?php echo $form->error($model, 'pole_id'); ?>
</div>

它将正确处理现有数据,并且在控制器中不需要特殊操作即可处理此输入-$model->attributes = $_POST['Jpapoles']会将pole_id加载为任何其他属性。

但是此扩展也应该非常适用于模型,因此您可以使用:

$this->widget('ext.select2.ESelect2', [
    'model' => $model,
    'attribute' => 'pole_id',
    'options' => [
        // ...
    ],
]);

您无需自己创建隐藏文件。