2013-10-22 90 views
0

在jTable中很難實現搜索方法。 代碼:在jTable中添加搜索方法(jQuery)

generaterecords.php:

<?php 
$page_title = "Generate Reports"; 
require_once('template/header.php'); 
require_once('template/navmenu.php'); 
require_once('template/content-top.php'); 
?> 
<div class="filtering"> 
<form> 
<select name="year" > 
<option value="0000">Year</option> 
<?php 
for($i=date('Y'); $i>2012; $i--) { 
echo '<option value="'.$i.'"'.'>'.$i.'</option>'."\n"; 
} 
?> 

</select> 
<select name="month"> 
<option value="0">Month</option> 
<option value="1">January</option> 
<option value="2">February</option> 
<option value="3">March</option> 
<option value="4">April</option> 
<option value="5">May</option> 
<option value="6">June</option> 
<option value="7">July</option> 
<option value="8">August</option> 
<option value="9">September</option> 
<option value="10">October</option> 
<option value="11">November</option> 
<option value="12">December</option> 
</select> 
<button type="submit" id="LoadRecordsButton">Load records</button> 
</form> 
<div id="PeopleTableContainer" style="width: 850px;"></div> 
<script type="text/javascript"> 
$(document).ready(function() { 

     //Prepare jTable 
     $('#PeopleTableContainer').jtable({ 
      title: 'Payment Records', 
      paging: true, 
      pageSize: 10, 
      sorting: true, 
      defaultSorting: 'payment.paymentdate ASC', 
      actions: { 
       listAction: 'payment.php?action=list' 
      }, 

      fields: { 
       paymentid: { 
        key: true, 
        create: false, 
        edit: false, 
        list: false 
       }, 
       lname: { 
        title: 'Last Name', 
        width: '20%', 
        create: false, 
        edit: false 
       }, 
       fname: { 
        title: 'First Name', 
        width: '20%', 
        create: false, 
        edit: false 

       }, 
       mname: { 
        title: 'Middle Name', 
        width: '20%', 
        create: false, 
        edit: false 
       }, 
       paymenttype: { 
        title: 'Type', 
        width: '20%', 
        create: false, 
        edit: false 
       }, 
       paymentdate: { 
        title: 'Date', 
        width: '20%', 
        create: false, 
        edit: false 
       }, 
       totalrate: { 
        title: 'Total Rate', 
        width: '20%', 
        create: false, 
        edit: false 
       }, 
       paymentamt: { 
        title: 'Amount', 
        width: '20%', 
        create: false 
       }, 
       balance: { 
        title: 'Balance', 
        width: '20%', 
        create: false, 
        edit: false 
       } 
      } 
     }); 

     //Load person list from server 
     //$('#PeopleTableContainer').jtable('load'); 

     //Re-load records when user click 'load records' button. 
     // Backup 
    $('#LoadRecordsButton').click(function (e) { 
     e.preventDefault(); 
     $('#PeopleTableContainer').jtable('load', { 
      year: $('#year').val(), 
      month: $('#month').val() 
     }); 
    }); 



    //Load all records when page is first shown 
    // $('#LoadRecordsButton').click(); 

    }); 

</script> 
<br> 
<br> 
<?php 
// footer 
require_once('template/footer.php'); 
?> 

payment.php:

<?php 
include_once ('database_connection.php'); 
try 
{ 
$con = mysql_connect("localhost","kureido","tnx4standinstillwanker"); 
mysql_select_db("kureido", $con); 
//Getting records (listAction) 
if($_GET["action"] == "list") 
{ 
    //Get record count 
    $offresult = mysql_query("SELECT COUNT(*) AS OLRecordCount FROM official;"); 
    $offrow = mysql_fetch_array($offresult); 
    $allresult = mysql_query("SELECT COUNT(*) AS AllRecordCount FROM lodger;"); 
    $allrow = mysql_fetch_array($allresult); 
    $resresult = mysql_query("SELECT COUNT(*) AS ResRecordCount FROM reservation;"); 
    $resrow = mysql_fetch_array($resresult); 
    $recordCount = $allrow['AllRecordCount'] - $offrow['OLRecordCount'] - $resrow['ResRecordCount']; 

    $year = ""; 
    $month = ""; 
    if (empty($_POST['year']) && empty($_POST['month'])) 
     $year = $month = ""; 
    else 
    { 
     $year = $_POST['year']; 
     $month = $_POST['month']; 
    } 

    //Get records from database 
    $result = mysql_query("SELECT payment.paymentid, lodger.ssn, lodger.lname, lodger.fname, lodger.mname, payment.paymenttype, payment.paymentdate, payment.totalrate, payment.paymentamt, payment.totalrate - payment.paymentamt AS balance FROM payment, lodger WHERE lodger.ssn = payment.lodger_ssn AND YEAR(paymentdate) = '" . $_POST["year"] . "' AND MONTH(paymentdate) = '" . $_POST["month"] . "' ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] .";"); 

    //Add all records to an array 
    $rows = array(); 
    while($row = mysql_fetch_array($result)) 
    { 
     $rows[] = $row; 
    } 

    //Return result to jTable 
    $jTableResult = array(); 
    $jTableResult['Result'] = "OK"; 
    $jTableResult['TotalRecordCount'] = $recordCount; 
    $jTableResult['Records'] = $rows; 
    print json_encode($jTableResult); 
} 
//Creating a new record (createAction) 
/* else if($_GET["action"] == "create") 
{ 
    //Insert record into database 
    $result = mysql_query("INSERT INTO people(Name, Age, RecordDate) VALUES('" . $_POST["Name"] . "', " . $_POST["Age"] . ",now());"); 

    //Get last inserted record (to return to jTable) 
    $result = mysql_query("SELECT * FROM people WHERE PersonId = LAST_INSERT_ID();"); 
    $row = mysql_fetch_array($result); 

    //Return result to jTable 
    $jTableResult = array(); 
    $jTableResult['Result'] = "OK"; 
    $jTableResult['Record'] = $row; 
    print json_encode($jTableResult); 
} */ 
//Updating a record (updateAction) 
else if($_GET["action"] == "update") 
{ 
    //Update record in database 
    $result = mysql_query("UPDATE official SET room_code = '" . $_POST["room_code"] . "', appliancerate = '" . $_POST["appliancerate"] . "', monthlybal = '" . $_POST["monthlybal"] . "' WHERE lodger_ssn = " . $_POST["ssn"] . ";"); 

    //Return result to jTable 
    $jTableResult = array(); 
    $jTableResult['Result'] = "OK"; 
    print json_encode($jTableResult); 
} 
//Deleting a record (deleteAction) 
else if($_GET["action"] == "delete") 
{ 
    //Delete from database 
    $result = mysql_query("DELETE FROM official WHERE lodger_ssn = " . $_POST["ssn"] . ";"); 

    //Return result to jTable 
    $jTableResult = array(); 
    $jTableResult['Result'] = "OK"; 
    print json_encode($jTableResult); 
} 
else if($_GET["action"] == "view") 
{ 
} 

//Close database connection 
mysql_close($con); 

} 
catch(Exception $ex) 
{ 
//Return error message 
$jTableResult = array(); 
$jTableResult['Result'] = "ERROR"; 
$jTableResult['Message'] = $ex->getMessage(); 
print json_encode($jTableResult); 
} 

?> 

有時服務器返回的錯誤。任何想法如何得到這個固定? generaterecords.php是客戶端,而payment.php是服務器端。

回答

0
$('#LoadRecordsButton').click(function (e) { 
    e.preventDefault(); 
    $('#PeopleTableContainer').jtable('load', { 
     year: $('#year').val(), 
     month: $('#month').val() 
    }); 
    }); 
    //remove your comments .. the code below is for running the function above when loadrecords button is clicked 
    $('#LoadRecordsButton').click(); 
+4

嗨歡迎堆棧溢出。你能否提供一些細節,說明爲什麼這是這個問題的答案? – Stefan

+0

請提供某種解釋,爲什麼這是答案解決問題 – rayryeng

+0

凱思真的重新發布了OP發佈的代碼。這也是[jTable文檔]給出的jQuery代碼(http://www.jtable.org/demo/filtering) – andrsnn