限制和控制可订购的数据表列?

时间:2016-02-18 17:16:32

标签: php mysql datatables-1.10

我有数据表,其中列是No,LastName,FirstName,Details。目前在所有4列上都出现asc和desc符号。我需要限制LastName列和FirstName的顺序。下面是我的脚本,它执行服务器处理。下面是我用于获取数据并填充到网格中的php代码。问题是第一栏是否定的。因此,当我使用这些代码时,我只是根据列No来订购。如何通过LastName或FirstName进行排序?

$requestData= $_REQUEST;
        $columns = array( 
        // datatable column index  => database column name           
            0 => 'fName',
            1=> 'lName'
        );      

        $query = "Select fName,lName,details From User";

    if(!empty($requestData['search']['value']) ) {   
           $query.=" AND ( fName LIKE :searchKey )";  
    }
    $selectQueryResult1 = $link->prepare($query);       

        $arr = $selectQueryResult1->errorInfo();    
        $totalData=$selectQueryResult1->rowCount();
    $totalFiltered = $totalData;  

    $query.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
    //echo "query : ".$query;

    $selectQueryResult1 = $link->prepare($query);
    if(!empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter

         $searchKey = $requestData['search']['value'];
         $searchKey="%$searchKey%";
                 $selectQueryResult1->bindParam(':searchKey',$searchKey );


    }
    $selectQueryResult1->execute();



    $i=0;
    $i=$requestData['start']+$i;
    while($row = $selectQueryResult1->fetch())
        {
         $nestedData=array(); 
         $i++;

     $nestedData[] = $i;
         $nestedData[] = $row["fName"];
         $nestedData[] = $row["lName"];
         $nestedData[] = $row["details"];

         $data[] = $nestedData;
        }

        if($totalData>0){
        $json_data = array(
            "draw"            => intval( $requestData['draw'] ),   
            "recordsTotal"    => intval( $totalData ),  
            "recordsFiltered" => intval( $totalFiltered ), 
            "data"            => $data   
            );
    }
    else{
        $json_data = array(
            "draw"            => intval( $requestData['draw'] ),   
            "recordsTotal"    => intval( $totalData ),  
            "recordsFiltered" => intval( $totalFiltered ), 
            "data"            => ""   
            );
    }

1 个答案:

答案 0 :(得分:1)

您希望为用户提供仅按姓氏或名字排序的选项,对吗?

这是一个DataTables初始化选项:

columns.orderable启用或禁用此列的排序

请参阅:Options了解详情。