Site icon PlusMagi's Blog By Pitt Phunsanit

DataTable: การค้นหาชั้นสูง

เราสามารถเพิ่มเงื่อนไขให้ DataTables ใช้ในการค้นหาได้ โดยส่วนที่เปลี่ยนไปจากเดิมคือใช้ “beforeSend” ในการรวมข้อมูลจาก form (formA) และเขียน validation ง่าย ๆ โดยบังคับว่าถ้า advanceSearch ถูกติ๊กอยู่จะต้องเลือก geo_id ด้วย ( จริง ๆ คือ ถึงไม่ติ๊ก advanceSearch ก็ใช้ geo_id search ได้เหมือนกัน )
jQuery.DataTables/data.json.external.search.html

<!doctype html>
<html>
 
<head>
 <meta charset="utf-8">
 <meta name="author" content="Pitt Phunsanit">
 <title>DataTables: external.search</title>
 <link href="../vendor/twbs/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
 <link href="../vendor/twbs/bootstrap/dist/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">
 <link href="../vendor/datatables/datatables/media/css/dataTables.bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
 
<body>
 <form action="DataTables.json.php" class="form-inline" id="formA" method="post">
  <div class="form-group">
   <input name="advanceSearch" type="checkbox">
   <label for="advanceSearch">advance Search</label>
  </div>
  <div class="form-group">
   <label for="geo_id">ภูมิภาค:</label>
   <select class="form-control" name="geo_id">
    <option selected="selected" value="">--- Select ---</option>
    <option value="1">ภาคเหนือ</option>
    <option value="2">ภาคกลาง</option>
    <option value="3">ภาคตะวันออกเฉียงเหนือ</option>
    <option value="4">ภาคตะวันตก</option>
    <option value="5">ภาคตะวันออก</option>
    <option value="6">ภาคใต้</option>
   </select>
  </div>
  <button type="submit" class="btn btn-default">Search</button>
 </form>
 <table class="table table-bordered table-hover table-striped" id="tableA" width="100%"></table>
 <script src="../vendor/components/jquery/jquery.min.js"></script>
 <script src="../vendor/datatables/datatables/media/js/jquery.dataTables.min.js"></script>
 <script src="../vendor/datatables/datatables/media/js/dataTables.bootstrap.min.js"></script>
 <script src="data.json.external.search.js"></script>
</body>
 
</html>

jQuery.DataTables/data.json.external.search.js
$(function() {
 
 formA = $('#formA');
 tableA = $('#tableA');
 
 datatable = tableA.DataTable({
  "ajax": {
   "beforeSend": function(jqXHR, settings) {
    /* add value form from to DataTable params */
    settings.data = formA.serialize() + '&' + settings.data;
 
    /* validation */
    var params = new URLSearchParams(settings.data);
 
    /* user must selected region if enable advance search */
    if (params.get('advanceSearch') == 'on' && params.get('geo_id') == '') {
     jqXHR.abort();
     alert('กรุณาเลือกภูมิภาค');
     return false;
    }
 
    return true;
   },
   "dataSrc": function(json) {
    alert('data back ' + json.data.length + ' items');
 
    return json.data;
   },
   "method": "POST",
   "url": "data.json.php",
  },
  "columns": [{
    "orderable": false,
    "render": function(data, type, row, meta) {
     return parseInt(meta.row) + parseInt(meta.settings._iDisplayStart) + 1;
    },
    "title": 'No.',
    "width": "10px",
   },
   {
    "orderable": false,
    "render": function(data, type, row, meta) {
     return '<input type="checkbox" value="' + row.DISTRICT_CODE + '">';
    },
    "title": '<input class="checkAll" type="checkbox">',
    "width": "10px",
   },
   {
    "orderable": false,
    "render": function(data, type, row, meta) {
     if (row.enable == '1') {
      return '<span class="glyphicon glyphicon-ok"></span>';
     } else {
      return '<span class="glyphicon glyphicon-remove"></span>';
     }
    },
    "title": "Enable",
    "width": "10px",
   }, {
    "data": "DISTRICT_CODE",
    "title": "District Code",
    "width": "90px",
   }, {
    "data": "DISTRICT_NAME",
    "title": "District Name",
   }, {
    "data": "PROVINCE_NAME",
    "title": "Province Name",
   }
  ],
  /* default sort */
  "order": [
   [3, "asc"],
   [4, "asc"],
  ],
  "processing": true,
  "serverSide": true,
  "stateSave": true,
 });
 
 $('.checkAll', tableA).click(function() {
  $('input:checkbox', tableA).not(this).prop('checked', this.checked);
 });
 
 formA.submit(function(event) {
  event.preventDefault();
 
  datatable.ajax.reload();
 });
 
});

อ่านเพิ่มเติม

Exit mobile version