Jquery ile Dinamik Tablo oluşturma

HTML Sayfası:

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>

<!DOCTYPE html>
<html lang=”en”>

<head>
<meta charset=”utf-8″>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<meta name=”description” content=””>
<meta name=”author” content=””>

<title>Signal</title>
<jsp:include page=”head.jsp” />
<link href=”vendor/morrisjs/morris.css” rel=”stylesheet” type=”text/css”/>
<script src=”vendor/jquery/jquery.min.js” type=”text/javascript”></script>

</head>

<body>

<!– /.panel-heading –>

<div id=”page-wrapper”>
<!– /.row –>
<div class=”row”>
<div class=”col-lg-12″>
<div class=”panel panel-default”>
<div class=”panel-heading”>
Veriler
</div>
<div class=”panel-body”>
<div id=”showData”>

</div>

</div>
</div>
</div>
</div>
</div>

<!– DataTables JavaScript –>
<script src=”vendor/jquery/jquery.min.js”></script>
<script src=”js/tableUpdate.js” type=”text/javascript”></script>
</body>

</html>

.js Sayfası

$(document).ready(function () {

$.show_page = function(elementId) {
$.tabloGuncelle($(“#”+elementId).attr(“name”));
};
});

$.tabloGuncelle = function (callectName) {
$.ajax({
type: “POST”,
url:”selectServlete”,
dataType: “json”,
data:{colName: callectName.toString()},
success: function (data, textStatus, jqXHR) {
console.log(“data:”+data[0]);
if(!data[0].hata){
//Create Table into div (its id is showData. )
$(‘#showData’).html(“<table width=\”100%\” “+
“class=\”table table-striped table-bordered table-hover\””+
“id=\”dataTableDB\”></table>”)

//fill in datatable
$(‘#dataTableDB’).DataTable({
responsive: true,
destroy: true,
data: data[0].dataTable,
columns: data[0].jsonTitle
});
}
else{
alert(“hata: “+ data[0].messageInfo);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.status +”: “+textStatus);
}
});
};

selectServlete.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package servlete;

import com.mongodb.MongoException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Iterator;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.sun.javafx.scene.control.skin.VirtualFlow;
import dbcon.DBConn;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class selectServlete extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8”);

boolean hata = false;
String messageInfo = “”;

String collectionName = request.getParameter(“colName”).toString().trim();
ArrayList<List> dataTable = new ArrayList<List>();
JSONArray jsonTitle = new JSONArray();

try {
DBConn dbcon = new DBConn();
MongoDatabase db = dbcon.connectMLabDB();
MongoCollection<Document> collection = db.getCollection(collectionName);
MongoCursor<Document> cursor = collection.find().iterator();

if(collection.count() > 0){
messageInfo = collectionName + ” collection da toplama ” +
collection.count()+”eleman bulunmaktadır”;

Document doc = null ;
while (cursor.hasNext()) {
doc = cursor.next();
Iterator docKeys = doc.keySet().iterator();
List<String> row = new ArrayList<String>();
while (docKeys.hasNext()){
row.add(doc.get((String)docKeys.next()).toString());
}
dataTable.add(row);
}
Iterator docKeys = doc.keySet().iterator();

while (docKeys.hasNext()){
JSONObject title = new JSONObject();
title.put(“title”, (String)docKeys.next());
jsonTitle.put(title);
}
}else{
hata = true;
messageInfo = “Hiçbir kayıt bulunamadı”;
}

} catch (MongoException e) {
hata = true;
messageInfo = e.toString();
}

JSONArray jsonData = new JSONArray();

try {
JSONObject dataObje = new JSONObject();
dataObje.put(“hata”, hata);
dataObje.put(“messageInfo”, messageInfo);
dataObje.put(“dataTable”, dataTable);
dataObje.put(“jsonTitle”, jsonTitle);
jsonData.put(dataObje);

PrintWriter out =response.getWriter();
out.print(jsonData);
out.flush();

} catch (JSONException e) {
System.out.println(“selectServlete de beklenmedik bir hata oluştu.\n HATA: “+ e.toString());
}

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

}

Betül Çil Kâşif on 11 Aralık 2017 yazdı
Yorum Ekle
  • 0 Yanıt(lar)
  • Yanıtınız

    Cevabınızı göndererek Gizlilik Politikası ve Hizmet Şartlarını kabul ediyorsunuz.