我有CodeIgniter项目我想“升级”:) 在其中的一部分,我从表中选择一个值(主键)并显示该记录中的所有数据。目前我正在使用下拉菜单进行选择,使用按钮启动查询,然后在同一页面上显示数据。 表格包含以下字段:
claim_id - primary key
repair_type
type
block
condition
defect
symptom
repair
addition
我的模特:
class Claim_m extends MY_Model
{
protected $_table_name = 'claim';
protected $_order_by = 'repair_type';
protected $_primary_key = 'claim_id';
public function __construct()
{
parent::__construct();
}
public $rules_claim = array(
'repair_type'=>array(
'field'=>'claim_id',
'label'=>'ID Claima',
'rules'=>'required'
)
);
public function daj_dropdown() //creates dropdown
{
$rezultat = $this->db->select('claim_id,repair_type')->get('claim')->result_array();
$dropdown = array();
foreach($rezultat as $r)
{
$dropdown[$r['claim_id']] = $r['repair_type'];
}
return $dropdown;
}
}
我的控制器:
class Claims extends Frontend_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('claim_m');
}
public function choice()
{
//validation
$rules = $this->claim_m->rules_claim;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == TRUE)
{
$claim_id = $this->input->post('claim_id');
$this->data['zapisi'] = $this->claim_m->daj($claim_id,TRUE);
}
//dropdown
$this->data['dropdown'] = $this->claim_m->daj_dropdown();
//set subview
$this->data['subview'] = 'claim/zapis';
$this->load->view('_main_layout', $this->data);
}
}
我的观点(zapis.php):
<?php echo form_open();?>
<table class="table">
<tr>
<td>Type of repair:</td>
<td><?php echo form_dropdown('claim_id',$dropdown); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Izaberi', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
<?php
if(!isset($zapisi))
{
echo 'no records';
}
else
{ ?>
<section>
<h2>Claim pattern - <u> <?php echo($zapisi->repair_type); ?> </u></h2>
<i>Defect type: </i><font color="red"><?php echo($zapisi->type); ?></font></br>
<i>Defect block: </i><font color="red"><?php echo($zapisi->block); ?></font></br>
<i>Condition code: </i><font color="red"><?php echo($zapisi->condition); ?></font></br>
<i>Defect code: </i><font color="red"><?php echo($zapisi->defect); ?></font></br>
<i>Symptom code: </i><font color="red"><?php echo($zapisi->symptom); ?></font></br>
<i>Repair code: </i><font color="red"><?php echo($zapisi->repair); ?></font></br>
<i>Other: </i></br><font color="red"><?php echo($zapisi->addition); ?></font></br>
</section>
<?php } ?>
所有这一切都很好,但我需要在视图上没有按钮的情况下进行。基本上我需要在下拉菜单中更改选择时写下所有其他字段,而无需按下按钮。当我需要一个文件时,我发现了教程,但我需要在MySQL数据库表中显示(在下拉菜单中更改后)7个字段(类型,块,条件,缺陷,症状,修复,添加)。
我对jQuery或Ajax了解不多,但我认为它需要这样的东西。如果有人可以帮我这个或指出我正确的教程,我将不胜感激。 提前谢谢!
编辑:来自MY_Model的函数daj()
public function daj($id = NULL, $single = FALSE)
{
if ($id != NULL)
{
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key,$id);
$method = 'row';
}
elseif($single == TRUE)
{
$method = 'row';
}
else
{
$method = 'result';
}
$this->db->order_by($this->_order_by);
return $this->db->get($this->_table_name)->$method();
}
答案 0 :(得分:0)
如果您希望获取下拉列表更改的数据,而无需重新加载页面,则必须使用异步调用。 我建议使用jQuery AJAX,您可以查看文档here。
检查以下示例。请求的详细信息在发送到ajax的对象中设置,它应该非常简单。数据作为对象发送,但您也可以通过传递$('#form_id').serialize()
作为数据来发送整个表单。
然后在done方法中,您可以捕获作为响应发送的任何数据(示例中为msg)并根据需要使用它。
$.ajax({
method: "POST"
url: "test.html",
data: {field1:'Field1', field2:'Field2'}
}).done(function(msg) {
console.log(msg);
});
回到你的问题。您需要设置此类代码段,其中URL应设置为控制器的功能。您应该发送到ajax的数据是下拉框的值。我们可以说该字段的ID是“dropdownId”&#39;。然后数据将为data: {selection: $('#dropdownId').val() }
。
现在你只需要调整控制器的响应 - 回显它就可以从javascript中读取它。
一个好的做法是返回一个JSON,然后在前端解析它。您可以创建一个包含要返回的所有字段的数组,然后在控制器中以JSON编码并回显它。
$arr = ['field1' => 'Val1', 'field2' => 'Val2'];
echo json_encode($arr);
然后在你的ajax调用的done函数中,你将拥有:
var obj = JSON.parse(msg);
您可以访问以下字段:
obj.field1, obj.field2...
然后你只需要设置字段的值(id是field1,field2等):
$('#field1').val(obj.field1);
$('#field2').val(obj.field2);
...
答案 1 :(得分:0)
这是在下拉列表更改时使用JQuery请求新html的一种方法。
将此功能添加到控制器Claims.php
public function new_select() {
$claim_id = $this->input->post('claim_id');
$data['zapisi'] = $this->claim_m->daj($claim_id, TRUE);
//output the sub-subview it will be recieved by the ajax success function
$this->load->view('claim_detail', $data);
}
new_select()
将由稍后显示的JQuery ajax调用使用。
将视图文件zapis.php
更改为此
<div>Type of repair:</div>
<div>
<?php echo form_dropdown('claim_id', $dropdown, NULL, array('id' => 'repair_select')); ?>
</div>
<section id="repair-detail">
<?php
$this->load->view('claim_detail');
?>
</section>
请注意,下拉列表中添加了id
属性。另请注意,上面加载了子视图,这是一个新的视图文件 - claim_detail.php
- 如下所示。
<h2>
Claim pattern - <u><?php echo isset($zapisi) ? $zapisi->repair_type : "No Repair type selected"; ?></u>
</h2>
<div><?php
if(isset($zapisi))
{
?>
<i>Defect type: </i><font color="red"><?php echo $zapisi->type; ?></font><br>
<i>Defect block: </i><font color="red"><?php echo $zapisi->block; ?></font><br>
<i>Condition code: </i><font color="red"><?php echo $zapisi->condition; ?></font><br>
<i>Defect code: </i><font color="red"><?php echo $zapisi->defect; ?></font><br>
<i>Symptom code: </i><font color="red"><?php echo $zapisi->symptom; ?></font><br>
<i>Repair code: </i><font color="red"><?php echo $zapisi->repair; ?></font><br>
<i>Other: </i><br><font color="red"><?php echo $zapisi->addition; ?></font><br>
<?php
}
?></div>
<script>
$(document).ready(function () {
//handle changes to the dropdown
$("#repair_select").on('change', function (event) {
$.ajax({
type: 'POST',
url: "claims/new_select",
data: {claim_id: $(this).val()}, //send new select id to server
dataType: 'html',
success: function (returned) {
//replace the html inside <section id="repair-detail"> with the returned html
$("#repair-detail").html(returned);
}
});
});
});
</script>
新视图有javascript来处理下拉列表更改事件。基本上它注意到下拉列表中的更改并将新选择的claim_id
发送到控制器函数new_select
,该函数通过发回由数据库值填充的html来回复。 ajax函数success:
用<section id="repair-detail">
html替换returned
内的html。该页面使用新内容动态更新!
我没有对此进行测试,请原谅任何语法错误。但这个概念是合理的,并且是这类情况的一种非常常见的解决方案。