Php 在视图页面中插入带有优先级字段的Datatable,并根据优先级对表进行排序

Php 在视图页面中插入带有优先级字段的Datatable,并根据优先级对表进行排序,php,ajax,codeigniter,datatable,Php,Ajax,Codeigniter,Datatable,我在datatable中添加了一个列优先级,我想在查看datatable时在datatable中插入优先级。。。当我插入优先级时,它应该将优先级值插入数据库优先级字段。当我添加优先级时,datatable应该根据此优先级编号进行排序,同时为每个产品添加优先级。。我想在codeigniter中使用ajax实现这一点 你知道我该怎么做吗。?谢谢您: 这是我当前的datatable代码 <html> <head> <link rel="styles

我在datatable中添加了一个列优先级,我想在查看datatable时在datatable中插入优先级。。。当我插入优先级时,它应该将优先级值插入数据库优先级字段。当我添加优先级时,datatable应该根据此优先级编号进行排序,同时为每个产品添加优先级。。我想在codeigniter中使用ajax实现这一点 你知道我该怎么做吗。?谢谢您: 这是我当前的datatable代码

<html> 
   <head>  
      <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
      <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
      <script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
   </head>  
   <body>
   <table id="displayy" class="display" style="width:100%">  
      <thead> 
      <tr>  
         <td>Product ID</td>  
         <td>Product Name</td> 
         <td>Product Description</td> 
         <td>Category</td>  
         <td>Sub Category</td> 
         <td>Image</td>
         <td>Priority</td>
      </tr>  
      </thead>
         <?php  
         foreach ($h->result() as $row)  
         {  ?>
            <tr>  
               <td><?php echo $row->p_id;?></td> 
               <td><?php echo $row->product_name;?></td>  
               <td><?php echo $row->product_des;?></td>
               <td><?php echo $row->category;?></td>
               <td><?php echo $row->subcat;?></td>

               <td><?php $imageURL = base_url().'/images/'.$row->image;?>
                    <img src="<?php echo $imageURL; ?>" alt="" width="100" height="100"  /></td>
               <td>
                  <input type="text" name="priority" id="priority">
               </td>
            </tr>  
         <?php }  ?>  
   </table>  
<script type="text/javascript">
   $(document).ready(function() {
      $('#displayy').dataTable( {
        "lengthMenu": [[3,5,8,10, 25, 50, -1], [3,5,8,10, 25, 50, "All"]]
      });
   });
</script>
</body>
</html>

您可以尝试将此作为解决方案:

在HTML端:

<html> 
   <head>  
       <link rel="stylesheet" type="text/css" 
       href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
 <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"> 
 </script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

   </head>  
   <body>
   <div id="content">
   <table id="display" class="display" style="width:100%">  

        <thead> 
         <tr>  
            <td>Product ID</td>  
            <td>Product Name</td> 
            <td>Product Description</td> 
            <td>Category</td>  
            <td>Sub Category</td> 
            <td>Image</td>
            <td>Priority</td>
         </tr>  
         </thead>
         <?php  
         foreach ($h->result() as $key=>$row)  
         {  
            ?><tr>  
                <td><?php echo $row->p_id;?></td> 
            <td><?php echo $row->product_name;?></td>  
            <td><?php echo $row->product_des;?></td>
            <td><?php echo $row->category;?></td>
            <td><?php echo $row->subcat;?></td>

            <td><?php $imageURL = base_url().'/images/'.$row->image;?>
                    <img src="<?php echo $imageURL; ?>" alt="" width="100" height="100"  />
                </td>


           <td>
            <form method="POST" class="dataUpdate">
            <input type="text" name="priority" id="priority"/>
            <input type="hidden" name="pid" value="<?php echo $row->p_id;?>"/> 
            <input type="submit" value"Update" />
            </form>


           </td>

            </tr>  
         <?php }  
         ?>  
   </table> 
  </div> 

</body>
</html>
在test.php上:

$id=$_POST['pid'];
$prior=$_POST['priority'];
mysqli_query("update your_db set priority='".$prior."' where pid='".$id."');
$getdata=mysqli_query("select * from your_db order by priority DESC");
$response="
 <table id='display' class='display' style='width:100%'>  
 <thead> 
     <tr>  
        <td>Product ID</td>  
        <td>Product Name</td> 
        <td>Product Description</td> 
        <td>Category</td>  
        <td>Sub Category</td> 
        <td>Image</td>
        <td>Priority</td>
     </tr>  
     </thead>";
    $result=mysqli_fetch_array($getdata);
    foreach($result as $row)
    {
         $imageURL = base_url().'/images/'.$row->image;
         $response=$response."<tr>  
            <td>".$row->p_id."</td> 
        <td>".$row->product_name."</td>  
        <td>".$row->product_des."</td>
        <td>".$row->category."</td>
        <td>".echo $row->subcat."</td>

        <td><img src='".$imageURL."' alt='' width='100' height='100'  />
            </td>


       <td>
        <form method='POST' class='dataUpdate'>
        <input type='text' name='priority' id='priority' value='".$row->priority."'/>
        <input type='hidden' name='pid' value='".$row->p_id."'/> 
        <input type='submit' value='Update' />
        </form>


       </td>

        </tr> ";
     }

  $response=$response."</table";
 return $response;

我希望这会对您有所帮助,这里的datatable count列索引总是以0开始,并且根据优先级列位于索引6上

$(document).ready(function() {
    $('#displayy').DataTable( {
        "order": [[ 6, "desc" ]]
    } );
} );

所以我得到了答案,如果将来有人想要的话

将onchange函数添加到文本框 value=id=priority onchange=update_priority'p_id?>,this.value>

在控制器添加中:

public function priorityadd() 
{ 
 //echo $this->input->post('priority');
 $rowId = $this->input->post("rowId"); 
 $priorityCount = $this->input->post("priority"); 

 $this->load->database(); 
 $this->load->model('PriorityModel'); 
 if($this->PriorityModel->insertp($rowId,$priorityCount)==true){
  echo 1;
 } else{
  echo 0;
 }
}
在模型中添加以下内容:

公共功能选择2 {

}

同样在查看页面中,此ajax查询::

<script> 


function update_priority(rowId,val){
  jQuery.ajax({
  url: "<?=base_url()?>Userlog/priorityadd",
  method:"POST",
  data:{rowId:rowId,priority:val},
  success: function(result){
    if(result==1){
     // alert("Priority Updated");
      location.reload();
    }else{
      alert("Error");
    }
  }
  });

}

</script>

只需使用该行主id更新优先级,并按优先级字段对asc排序。您是否编写了在db中更新的代码?不,我不知道如何实现这一点……我只是想知道如何实现这一点thing@DevsiOdedrai要在视图页面中输入priority时插入数据库的优先级,只需在keyupi上调用ajax即可就像你的努力talhalet让我试试这个对不起我现在才看到这个
     $this->db->from('product_details');
     $this->db->order_by('Priority','ASC');
     $query = $this->db->get(); return $query; 
<script> 


function update_priority(rowId,val){
  jQuery.ajax({
  url: "<?=base_url()?>Userlog/priorityadd",
  method:"POST",
  data:{rowId:rowId,priority:val},
  success: function(result){
    if(result==1){
     // alert("Priority Updated");
      location.reload();
    }else{
      alert("Error");
    }
  }
  });

}

</script>