automatic row ID addition in datatables (jQuery)
This is the PHP code given
<?php
$aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
$sTable = "ajax";
$gaSql['user'] = "";
$gaSql['password'] = "";
$gaSql['db'] = "";
$gaSql['server'] = "localhost";
include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" );
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS id, ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(".$sIndexColumn.")
FROM $sTable
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
// Add the row ID and class to the object
$row['DT_RowId'] = 'row_'.$aRow['id'];
$row['DT_RowClass'] = 'grade'.$aRow['grade'];
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
?>
I need to develop a program in java as like PHP code (I am unable to understand PHP code), This is my java code(Partial).
public class DTSerializedObject implements java.io.Serializable
{
public int iTotalRecords;
public int iTotalDisplayRecords;
public ArrayList<Object> aaData;
}
inside a function
ArrayList<Object> jsonarray = new ArrayList<Object>();
DTSerializedObject dts = new DTSerializedObject();
dts.iTotalRecords = dts.iTotalDisplayRecords = 30;
开发者_开发技巧 String category=null;
while(rs.next())
{
category="Normal";
ArrayList<Object> ja = new ArrayList<Object>();
int topicno=rs.getInt("topicno");
ja.add(topicno);
ja.add(rs.getString("filename"));
ja.add(rs.getString("letterno"));
ja.add(rs.getString("date"));
if((rs.getString("category")).equals("2"))
category="Urgent";
ja.add("<center>"+category+"</center>");
ja.add(rs.getString("office"));
ja.add(rs.getString("subject")+"....");
ja.add("<center><a class='anc' href=\"/Spandana2/specifiedTopicDetailsAndReplies.do?topicno="+topicno+"&purpose=view&jsessionid="+java.util.UUID.randomUUID().toString().replace("-","").toUpperCase()+"\">View</a></center>");
jsonarray.add(ja);
}
dts.aaData = jsonarray;
How to add DT_RowId,DT_RowClass option to my java code. What is the expected JSON output format for the above PHP code?
Thanks in advance
crate an object as like this
class Bean{
public String col1;
public String col2;
public String DT_RowClass;
}
while getting the data from db created an object(Bean) and assigned the values to that object properties for each and every row.
ArrayList al = new ArrayList();
while(rs.next){
Bean b = new Bean();
b.col1(rs.getString(1));
b.col2(rs.getString(2));
if(rs.getString(3))
b.DT_RowClass = "class1";
else
b.DT_RowClass = "class2";
al.add(b);
}
created a class which represents the json format required to display the data in webpage. create object (obj) of that class and assigned the appropriate values to iTotalRecords & iTotalDisplayRecords & aaData = al;
public class DTSerializedObject implements java.io.Serializable
{
public int iTotalRecords;
public int iTotalDisplayRecords;
public ArrayList<Object> aaData;
}
Finally serialize that object using flexjson.
String result = new flexjson.JSONSerializer().exclude("class").serialize(obj);
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-store");
response.getWriter().print(result);
精彩评论