如何在index.php的表单中使用cjuiautocomplete?

时间:2015-11-17 18:30:05

标签: php database yii autocomplete dropdown

我有我的index.php,我正在实现搜索功能,功能正常,用户输入一些商业名称,并输入一些城市名称,提交表格后,业务从数据库中检索。现在我的下一个任务是使用cjuiautocomplete实现表单提交。就像用户开始输入业务名称一样,企业应该在下拉列表中下载。我的主要障碍是我在index.php。我正在关注此http://www.yiiframework.com/wiki/162/a-simple-action-for-cjuiautocomplete/,但这是针对控制器的视图文件。我怎样才能在index.php中实现这一点。以下是我在index.php中的表格。

<form action="business/searchingtesting" method="GET">                     
  <div class="form-group form-group-lg">
    <h2 class="title">Find the best places to eat, drink, shop, or visit in Islamabad. </h2>
    <div class="col-sm-5 col-md-5 col-lg-5 col-md-offset-1">
      <input type="text" class="form-control" name="business" id="lg" placeholder="I'm looking for....">
    </div>
    <div class="col-sm-3 col-md-3 col-lg-3">
      <input type="text" class="form-control" id="sm" name="city"  placeholder="Islamabad">
    </div>
    <div class="col-sm-3 col-md-3 col-lg-3">
      <input type="submit" class="btn btn-primary btn-lg" value="submit">
    </div>
  </div>
</form> 

如果我按照上面的链接并在我的表单中使用以下代码,我会收到此错误“未定义的变量模型”。

<?php 
  $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
      'attribute' => 'my_name',
        'model' => $model,
        'sourceUrl' => array('my/aclist'),
        'name' => 'business_name',
        'options' => array(
          'minLength' => '3',
        ),
        'htmlOptions' => array(
          'size' => 45,
          'maxlength' => 45,
        ),
  )); ?>

2 个答案:

答案 0 :(得分:0)

首先read documentation。你可以使用CJuiAutoComplete和模型一样没有它。要与模型一起使用,您需要指定两个参数:modelattribute。如果您在没有模型的情况下使用它,那么只有name。我可以看到你没有在表单中使用模型,所以这个例子适合你:

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'name' => 'my_name',
    'sourceUrl' => array('/my/aclist'), // you need first slash if you want properly working URL from web root
));

答案 1 :(得分:0)

<form action="business/searchname" method="GET">                     
    <div class="form-group form-group-lg">
        <h2 class="title">
            Find the best places to eat, drink, shop, or visit in Islamabad. 
        </h2>
        <div class="col-sm-5 col-md-5 col-lg-5 col-md-offset-1">
        <?php 
        $model = Business::model()->findAll();
        $modelcity = Address::model()->findAll(array(
            'select' => 't.city',
            'group' => 't.city', //selecting distinct values as many businesses hass same cities, so the drop down was filled with only one city
            'distinct' => true,
        ));

        foreach ($model as $mo) {
            $store[] = $mo->business_name;
        }
        foreach ($modelcity as $c) {
            $city[] = $c->city;
        }

        $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
            'name' => 'business',
            'source' => array_values($store),
            // additional javascript options for the autocomplete plugin
            'options' => array(
            'minLength' => '2',
        ),
        'htmlOptions' => array(
            'style' => 'height:45px;width:415px;',
            'placeholder' => '          I am Looking for................ ',
        ),
        ));?>
    </div>
    <div class="col-sm-3 col-md-3 col-lg-3">
        <?php 
        $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
            'name' => 'city',
            'source' => array_values($city),
            // additional javascript options for the autocomplete plugin
            'options' => array(
                'minLength' => '2',
            ),
            'htmlOptions' => array(
                'style' => 'height:45px; width:250px;',
                'placeholder'=>'          City................ ',
            ),
        ));
        ?>
    </div>
    <div class="col-sm-3 col-md-3 col-lg-3">
        <input type="submit" class="btn btn-primary btn-lg" value="submit"/>
    </div>
</div>
</form>