简体   繁体   中英

Datatables search data on client-side from a server-side processing

I am searching for this problem almost everyday, but I couldn't find the right answer. My problem is like this:

https://www.datatables.net/examples/data_sources/server_side.html

If you search the date with 25th Apr, this doesn't work but if you search 04-25 it works.

on my current program, the searchable is the data id, not the returned value.

Now I want all data in the table to be searchable. How to do it? Thanks in advance.

Here's my server side script.

$table = 'tracks_tb';

// Table's primary key
$primaryKey = 'track_id';

$columns = array(
    array('db' => '`t`.`track_id`', 
          'dt' => 0, 
          'formatter' => function($a){
              return '<a href="tracks-view.php?id='.$a.'">'.$a.'</a>';
          },
          'field' => 'track_id'
    ),
    array('db' => '`t`.`upc`', 
          'dt' => 1, 
          'field' => 'upc'
    ),
    array('db' => '`t`.`isrc`', 
          'dt' => 2,       
          'field' => 'isrc'
    ),
    array('db' => '`t`.`tracktitle`', 
          'dt' => 3, 
          'field' => 'tracktitle'
    ),
    array('db' => '`t`.`artist`', 
          'dt' => 4,
          'formatter' => function($a){
              if($a !=''){
                  $ab = explode(',',$a);
                  $artist ="";
                  $db = new Database();
                  $db->connect();
                  foreach ($ab as $artindex => $artvalue) {
                      $q_a ='SELECT fname FROM rartist WHERE id = '.$artvalue;
                      $r_a = $db->query($q_a);
                      $a_a = $db->fetch_array_assoc($r_a);
                      $a_id = $artvalue;
                      $a_name = $a_a['fname'];
                      if(end($ab)==$artvalue){
                          $artist .= '<span>'.$a_name.'</span>';
                      } else {
                          $artist .= '<span>'.$a_name.'</span>, ';
                      }
                  }
                  return $artist;
              } else {
                  return $artist='';
              }
          },
          'field' => 'artist'
    ),
    array('db' => '`t`.`publisher`', 
          'dt' => 5,
          'formatter' => function($p){
              if($p !=''){
                  $aa = explode(',',$p);
                  $publisher ="";
                  $db = new Database();
                  $db->connect();
                  foreach ($aa as $pubindex => $pubvalue) {
                      $q1 ='SELECT publisher FROM rpublisher WHERE id = '.$pubvalue;
                      $r1 = $db->query($q1);
                      $a1 = $db->fetch_array_assoc($r1);
                      $p_name = $a1['publisher'];
                      $p_id = $pubvalue;
                      if(end($aa)==$pubvalue){
                          $publisher .= '<span>'.$p_name.'</span>';
                      } else {
                          $publisher .= '<span>'.$p_name.'</span>, ';
                      }
                  }
                  return $publisher;
              } else {
                  return $publisher='';
              }
          },
          'field' => 'publisher'
    ),
    array('db' => '`t`.`licensor`', 
          'dt' => 6,
          'formatter' => function($lcs){
              if($lcs != ''){
                  $al = explode(',',$lcs);
                  $licensor ="";
                  $db = new Database();
                  $db->connect();
                  foreach ($al as $licindex => $licvalue) {
                      $q_l ='SELECT licensor FROM rlicensor WHERE licensor_id = '. $licvalue;
                      $r_l = $db->query($q_l);
                      $a_l = $db->fetch_array_assoc($r_l);
                      $l_id = $licvalue;
                      $l_name = $a_l['licensor'];
                      if(end($al)==$licvalue){
                          $licensor .= '<span>'.$l_name.'</span>';
                      } else {
                          $licensor .= '<span>'.$l_name.'</span>, ';
                      }
                  }
                  return $licensor;
              } else { 
                  $licensor = ''; return $licensor;
              }
          },
          'field' => 'licensor'
    ),
    array('db' => '`t`.`genre`', 'dt' => 7,
          'formatter' => function($g){
              if($g != ''){
                  $ag = explode(',',$g);
                  $genre ="";
                  $db = new Database();
                  $db->connect();
                  foreach ($ag as $genindex => $genvalue) {
                      $q2 ='SELECT genre FROM rgenre WHERE id = '.$genvalue;
                      $r2 = $db->query($q2);
                      $a2 = $db->fetch_array_assoc($r2);
                      $g_id = $genvalue;
                      $g_name = $a2['genre'];
                      if(end($ag)==$genvalue){
                          $genre .= '<span>'.$g_name.'</span>';
                      } else {
                          $genre .= '<span>'.$g_name.'</span>, ';
                      }
                  }
                  return $genre;
              } else {
                  $genre = ''; return $genre;
              }
          },
         'field' => 'genre'
    ),
    array('db' => '`t`.`discno`', 
          'dt' => 8, 
          'field' => 'discno'
    ),
    array('db' => '`t`.`trackno`', 
          'dt' => 9, 
          'field' => 'trackno'
    )
);

require('class/datatables_v2.php');

$joinQuery = "FROM `{$table}` AS `t` ";
$extraCondition = "";

echo json_encode(
    SSP::simple($_GET, $db->mysql_details(), $table, $primaryKey, $columns, $joinQuery, $extraCondition)
);

As the example given by you is a server side datatable and the problem of mismatching of date is happening because in back-end the date is stored in 'yyyy-mm-dd' format(which is the date format in mysql database). If you want to search date which is in another format that this format 'yyyy-mm-dd' than you have to convert it at back-end. And you can put the search condition on any number of columns you want.

Ex:

$sSearch = $_REQUEST['sSearch'];

if($position != '')
{
    $position_filter = "col1 LIKE '%".$sSearch."%' or col2 LIKE '%".$sSearch."%'";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM