我在codeigniter中通过ajax使用搜索功能...代码工作正常...它正确搜索所需的匹配但我的代码的问题是它在视图中显示2个东西1.默认值2 .searched值 但我想要的是,当我搜索时,它只显示默认值,然后只显示搜索值而不是默认值。 我的控制器是:
public function seller()
{
$this->load->model('home/Home_model');
$ser = $this->input->post('search_data');
if(isset($search))
{
$data['h'] = $this->Home_model->search_seller($search);
}
else
{
$data['h'] = $this->Home_model->seller();
}
$this->load->view('admin/pages/sellers', $data);
}
我的模特是
public function seller()
{
$query = $this->db->get('wc_seller');
return $query;
}
public function search_seller($search)
{
$this->db->select('*');
$this->db->like('seller_name', $search);
$this->db->like('email', $search);
$query = $this->db->get('wc_seller');
return $query;
} 视图是
foreach ($h->result() as $row)
{
?>
<form action="<?php echo base_url() . "Home/edit_sellers/" . $row->id; ?>" method="POST">
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $row->last_modified;?> </td>
<td><?php echo $row->email; ?></td>
<input type="hidden" name="id" value="<?php echo $row-> id; ?>">
<td><input type="text" name="seller_name" value="<?php echo $row->seller_name;?>" placeholder="<?php echo $row->seller_name;?>" class="form-control"></td>
<td><input type="text" name="mobile_no" value="<?php echo $row->mobile_no;?>" placeholder="<?php echo $row->mobile_no;?>" class="form-control"></td>
<td>
</form>
<?php } ?>
我的ajax脚本是
<?php $this->load->view('admin/templates/header'); ?>
<div id="page-wrapper">
<div class="graphs">
<h3 class="blank1">All Sellers</h3>
<div class="something">
<input name="search_data" id="search_data" type="text" onkeyup="ajaxSearch();">
<div id="suggestions">
<div id="autoSuggestionsList">
</div>
</div>
</div>
<div class="xs tabls tabls1">
<div class="panel-body1">
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Registered On</th>
<th>Email</th>
<th>Full Name</th>
<th>Mobile No</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($h->result() as $row)
{
?>
<form action="<?php echo base_url() . "Home/edit_sellers/" . $row->id; ?>" method="POST">
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $row->last_modified;?> </td>
<td><?php echo $row->email; ?></td>
<input type="hidden" name="id" value="<?php echo $row-> id; ?>">
<td><input type="text" name="seller_name" value="<?php echo $row->seller_name;?>" placeholder="<?php echo $row->seller_name;?>" class="form-control"></td>
<td><input type="text" name="mobile_no" value="<?php echo $row->mobile_no;?>" placeholder="<?php echo $row->mobile_no;?>" class="form-control"></td>
<td>
<button type="submit" value="Update" class="btn btn-primary btn-sm">Update</button>
<a type="button" class="btn btn-danger btn-sm" href="<?php echo base_url() . "Home/delete_seller/" . $row->id; ?>" onclick="return confirm('Are you sure you want to delete it?')"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
<?php if($row->status == '1' ){
$status = 'Deactivate';
}
else{
$status = 'Activate';
}
?>
<button type="button" class="btn btn-success btn-sm" id="<?php echo $row->id; ?>" onclick="savestatus<?php echo $row->id; ?>(<?php echo $row->id; ?>)"><?php echo $status; ?></button>
</td>
</tr>
</form>
<script>
function savestatus<?php echo $row->id; ?>(row_id){
$.ajax({
type: "POST",
url: "<?php echo base_url().'home/activate_seller/'. $row->id; ?>",
data:{},
success:function( data )
{
//$("#"+row_id).html("Deactivate");
$("#"+row_id).text().trim()==="Activate"? $("#"+row_id).text("Deactivate"):$("#"+row_id).text("Activate")
}
});
}
</script>
<?php
$i++;
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
function ajaxSearch() {
var input_data = $('#search_data').val();
if (input_data.length === 0) {
$('#suggestions').hide();
//$('.tabls1').show();
} else {
var post_data = {
'search_data': input_data,
'<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
};
$.ajax({
type: "POST",
url: "<?php echo base_url().'home/seller/' ?>",
data: post_data,
success: function(data) {
// return success
if (data.length > 0) {
$('#suggestions').show();
//$('.tabls1').hide();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data);
}
}
});
}
}
</script>
<?php $this->load->view('admin/templates/footer'); ?>
答案 0 :(得分:0)
原因是您正在将搜索结果添加到同一个视图文件中。因此,请尝试以下方法: -
Create one new view file someName.php and paste the following code.
<div class="panel-body1">
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Registered On</th>
<th>Email</th>
<th>Full Name</th>
<th>Mobile No</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($h->result() as $row)
{
?>
<form action="<?php echo base_url() . "Home/edit_sellers/" . $row->id; ?>" method="POST">
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $row->last_modified;?> </td>
<td><?php echo $row->email; ?></td>
<input type="hidden" name="id" value="<?php echo $row-> id; ?>">
<td><input type="text" name="seller_name" value="<?php echo $row->seller_name;?>" placeholder="<?php echo $row->seller_name;?>" class="form-control"></td>
<td><input type="text" name="mobile_no" value="<?php echo $row->mobile_no;?>" placeholder="<?php echo $row->mobile_no;?>" class="form-control"></td>
<td>
<button type="submit" value="Update" class="btn btn-primary btn-sm">Update</button>
<a type="button" class="btn btn-danger btn-sm" href="<?php echo base_url() . "Home/delete_seller/" . $row->id; ?>" onclick="return confirm('Are you sure you want to delete it?')"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
<?php if($row->status == '1' ){
$status = 'Deactivate';
}
else{
$status = 'Activate';
}
?>
<button type="button" class="btn btn-success btn-sm" id="<?php echo $row->id; ?>" onclick="savestatus<?php echo $row->id; ?>(<?php echo $row->id; ?>)"><?php echo $status; ?></button>
</td>
</tr>
</form>
</tbody>
</table>
</div>
更改您的默认视图文件: -
<?php $this->load->view('admin/templates/header'); ?>
<div id="page-wrapper">
<div class="graphs">
<h3 class="blank1">All Sellers</h3>
<div class="something">
<input name="search_data" id="search_data" type="text" onkeyup="ajaxSearch();">
</div>
<div class="xs tabls tabls1" id="suggestions">
<div class="panel-body1">
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Registered On</th>
<th>Email</th>
<th>Full Name</th>
<th>Mobile No</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($h->result() as $row)
{
?>
<form action="<?php echo base_url() . "Home/edit_sellers/" . $row->id; ?>" method="POST">
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $row->last_modified;?> </td>
<td><?php echo $row->email; ?></td>
<input type="hidden" name="id" value="<?php echo $row-> id; ?>">
<td><input type="text" name="seller_name" value="<?php echo $row->seller_name;?>" placeholder="<?php echo $row->seller_name;?>" class="form-control"></td>
<td><input type="text" name="mobile_no" value="<?php echo $row->mobile_no;?>" placeholder="<?php echo $row->mobile_no;?>" class="form-control"></td>
<td>
<button type="submit" value="Update" class="btn btn-primary btn-sm">Update</button>
<a type="button" class="btn btn-danger btn-sm" href="<?php echo base_url() . "Home/delete_seller/" . $row->id; ?>" onclick="return confirm('Are you sure you want to delete it?')"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
<?php if($row->status == '1' ){
$status = 'Deactivate';
}
else{
$status = 'Activate';
}
?>
<button type="button" class="btn btn-success btn-sm" id="<?php echo $row->id; ?>" onclick="savestatus<?php echo $row->id; ?>(<?php echo $row->id; ?>)"><?php echo $status; ?></button>
</td>
</tr>
</form>
<script>
function savestatus<?php echo $row->id; ?>(row_id){
$.ajax({
type: "POST",
url: "<?php echo base_url().'home/activate_seller/'. $row->id; ?>",
data:{},
success:function( data )
{
//$("#"+row_id).html("Deactivate");
$("#"+row_id).text().trim()==="Activate"? $("#"+row_id).text("Deactivate"):$("#"+row_id).text("Activate")
}
});
}
</script>
<?php
$i++;
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
function ajaxSearch() {
var input_data = $('#search_data').val();
if (input_data.length === 0) {
$('#suggestions').hide();
//$('.tabls1').show();
} else {
var post_data = {
'search_data': input_data,
'<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
};
$.ajax({
type: "POST",
url: "<?php echo base_url().'home/seller/' ?>",
data: post_data,
success: function(data) {
// return success
if (data.length > 0) {
$('#suggestions').show();
//$('.tabls1').hide();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data);
}
}
});
}
}
</script>
<?php $this->load->view('admin/templates/footer'); ?>
在控制器中
public function seller()
{
$this->load->model('home/Home_model');
$ser = $this->input->post('search_data');
if(isset($search))
{
$data['h'] = $this->Home_model->search_seller($search);
$this->load->view('admin/pages/someName', $data);
}
else
{
$data['h'] = $this->Home_model->seller();
$this->load->view('admin/pages/sellers', $data);
}
}