gpt4 book ai didi

javascript - 如何在提交表单后显示下拉菜单过滤器选定的值?

转载 作者:行者123 更新时间:2023-12-03 11:44:33 26 4
gpt4 key购买 nike

我正在开发一个带有下拉菜单过滤器的搜索表单。该网站是由 Yii 创建的。

我的过滤表单在提交之前如下所示:PIC 1 filter form

选择过滤器时的表单:PIC 2 form while select the filters

但是在我单击filter按钮后,它又出现如下:PIC3 after form is submitted

但我希望在提交表单后显示结果时,表单应保留为 PIC 2

我的表格是:

<div class="row">
<div style="float: left">
<label class="form_controller required">By Brand</label>

<select style="width: 148px;" id="select_options" name="SCDcustomer_contacts_cms[brand_select_option]">
<option value="none">Select an option</option>
<option value="brand_preference">Brand Preference</option>
<option value="brand_purchased">Brand Purchased</option>
</select>
</div>
<div id="select_a_brand" name="select_a_brand">
<?php echo $form->dropDownList($model,'brand_id', gGetBrandList('brand_id', 'brand_id, brand_name'), array('prompt'=>'Select a brand')); ?>
</div>

<script type="text/javascript">
$(document).ready(function(){
jQuery('#select_a_brand').hide();

$("#select_options").change(function(){
$( "select option:selected").each(function(){

if(($(this).attr("value")=="brand_preference") || ($(this).attr("value")=="brand_purchased") ){
$("#select_a_brand").show();
}

if($(this).attr("value")=="none"){
$("#select_a_brand").hide();
}

});
});
});
</script>
</div>

规则函数是:

public function rules()
{
return array(
array('scd_cust_id,cust_id,order_first_name,order_surname,order_email,order_postcode, brand_id, brand_select_option', 'safe', 'on'=>'search'),
);
}

表单过滤器是:

if(!empty($filter)) {
if ($this->brand_select_option == "brand_preference") {
$criteria->select .= ', COUNT(*) as brand_preference';
$criteria->join .= ' INNER JOIN order_table ot ON ot.billing_contactid = t.contactid';
$criteria->join .= ' INNER JOIN order_item oi ON ot.scd_order_id = oi.scd_order_id';
$criteria->join .= ' INNER JOIN johnanthony_rstest.product p ON p.product_id = oi.product_id';
$criteria->condition .= (!empty($criteria->condition) ? ' AND ' : '') . 'p.brand_id=:brand_id';
$criteria->group = 't.contactid';
$criteria->order = 'COUNT(*) DESC';
$criteria->params = array(':brand_id'=>$this->brand_id);
$paging['pagination']['route']='report/index?SCDcustomer_contacts_cms[brand_id]=' . $this->brand_id;//For ajax pagination URL
}

if ($this->brand_select_option == "brand_purchased") {
$criteria->select .= ', SUM(product_price) AS brand_purchased';
$criteria->join .= ' INNER JOIN order_table ot ON ot.billing_contactid = t.contactid';
$criteria->join .= ' INNER JOIN order_item oi ON ot.scd_order_id = oi.scd_order_id';
$criteria->join .= ' INNER JOIN johnanthony_rstest.product p ON p.product_id = oi.product_id';
$criteria->condition .= (!empty($criteria->condition) ? ' AND ' : '') . 'p.brand_id=:brand_id';
$criteria->group = 't.contactid';
$criteria->order = 'SUM(product_price) DESC';
$criteria->params = array(':brand_id'=>$this->brand_id);
$paging['pagination']['route']='report/index?SCDcustomer_contacts_cms[brand_id]=' . $this->brand_id;//For ajax pagination URL
}
}

Ajax 文件是:

if (!empty($model->brand_id) && ($model->brand_select_option == "brand_preference")) {
array_push($options['columns'], array(
'name'=>'brand_preference',
'filter'=>false,
'header'=>'Brand Preference (No of purchased items)',
'type'=>'raw',
'value'=>'$data->brand_preference',

));
}
if (!empty($model->brand_id) && ($model->brand_select_option == "brand_purchased")) {
array_push($options['columns'], array(
'name'=>'brand_purchased',
'filter'=>false,
'header'=>'Brand Purchased (Sum of purchased items)',
'type'=>'raw',
'value'=>'$data->brand_purchased',

));
}

最佳答案

主要问题是 DOM 刷新页面,因为可能会定期提交。没有提交方法就无法说出更具体的内容。如果你使用ajax,你会得到json数据持有者中的所有数据。然后将其解析为对象和 html 所需的部分,并将其放置到要显示结果的位置。所有过滤等都必须在模型中进行。 Controller 仅将值传递给它。

将过滤器按钮设为ajaxButton:

<?php echo CHtml::ajaxSubmitButton(
'Filter',
Yii::app()->createUrl('report/index'),
array(
'type'=>'POST',
'data'=> 'js:{"SCDcustomer_contacts_cms[brand_select_option]": $('#select_options').val(), "SCDcustomer_contacts_cms[brand_id]": $('#select_brand').val() }',
'success'=>'js:function(data){ var data = JSON.parse(data);
$('#search-result-holder').html(data.search-results); }'
)); ?>

更新:2014 - 09 - 30 一些用于处理的额外数据。

您应该使用 html 制作一些您想要的结果的额外 View 。通过 renderPartial 将属性值传递给该 View (这必须在 Controller 内发生)。例如:

//Controller part which passes posted data to model

<?php
public function actionIndex() {
....

if(Yii::app()->request->isAjaxRequest){
$attributes = Yii::app()->request->getParam('SCDcustomer_contacts_cms');
$model->attributes = $attributes;
}

...
//Part of the controller which returns resulting model after specific functions
$model = $model->getDataByFilterQuery();
$search_results = $this->renderPartial('report/extra-views/search-results', array('model'=>$model), true);
echo CJSON::encode(array('search-results'=>$search_results));
...
}

?>

//In model
public function brandPreference() {
$criteria = new CDbCriteria;
$criteria->select .= ', COUNT(*) as brand_preference';
$criteria->join .= ' INNER JOIN order_table ot ON ot.billing_contactid = t.contactid';
$criteria->join .= ' INNER JOIN order_item oi ON ot.scd_order_id = oi.scd_order_id';
$criteria->join .= ' INNER JOIN johnanthony_rstest.product p ON p.product_id = oi.product_id';
$criteria->condition .= (!empty($criteria->condition) ? ' AND ' : '') . 'p.brand_id=:brand_id';
$criteria->group = 't.contactid';
$criteria->order = 'COUNT(*) DESC';
$criteria->params = array(':brand_id'=>$this->brand_id);
$paging['pagination']['route']='report/index?SCDcustomer_contacts_cms[brand_id]=' . $this->brand_id;//For ajax pagination URL
return new CActiveDataProvider($this, array(
'criteria' => $criteria, $paging
));

}

public function brandPurchased() {
$criteria = new CDbCriteria;
$criteria->select .= ', SUM(product_price) AS brand_purchased';
$criteria->join .= ' INNER JOIN order_table ot ON ot.billing_contactid = t.contactid';
$criteria->join .= ' INNER JOIN order_item oi ON ot.scd_order_id = oi.scd_order_id';
$criteria->join .= ' INNER JOIN johnanthony_rstest.product p ON p.product_id = oi.product_id';
$criteria->condition .= (!empty($criteria->condition) ? ' AND ' : '') . 'p.brand_id=:brand_id';
$criteria->group = 't.contactid';
$criteria->order = 'SUM(product_price) DESC';
$criteria->params = array(':brand_id'=>$this->brand_id);
$paging['pagination']['route']='report/index?SCDcustomer_contacts_cms[brand_id]=' . $this->brand_id;//For ajax pagination URL
return new CActiveDataProvider($this, array(
'criteria' => $criteria, $paging
));
}



public function getDataByFilterQuery() {
if($this->brand_select_option == 'brand_preference')
return $this->brandPreference();
elseif($this->brand_select_option == 'brand_purchased')
return $this->brandPurchased();
else
return null //or whatever you want
}


//Search result file
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'brand',
'product_title',
'description' => array(
'name' => 'description',
'value' => html_entity_decode(CHtml::decode($model->description)), //this is only for example purposes
),
'price',
'date',
),
));?>

//Index file
...
<?php echo CHtml::ajaxSubmitButton(
'Filter',
Yii::app()->createUrl('report/index'),
array(
'type'=>'POST',
'data'=> 'js:{"SCDcustomer_contacts_cms[brand_select_option]": $('#select_options').val(), "SCDcustomer_contacts_cms[brand_id]": $('#select_brand').val() }',
'success'=>'js:function(data){ var data = JSON.parse(data);
$('#search-result-holder').html(data.search-results); }'
)); ?>

...
<div id="search-result-holder"></div>
...

您还必须根据您的情况专门修改它。我怀疑 $paging 变量是否能按原样在那里工作。我更喜欢在 CActiveDataProvider 对象中使用分页参数。在第二个选项中,您可以打开数组并在该数组中打开分页参数数组。或者像这里一样:CPagination documentation这取决于你。

关于javascript - 如何在提交表单后显示下拉菜单过滤器选定的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26112935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com