Domanda

I was implementing an interface with uses DataTables and excel files. Motive is upload an excel and then show it in DataTables. As I didn't find any javascript library which parse the excel and provide it to DataTables. The only option is ot send the data to the front end struts/ java and send back the json. Is it possible to have DataTables waiting for the json by Ajax without sending the request. .

È stato utile?

Soluzione

i think, it is possible in html5 and javascript. run and test the code below.

<html>
</head>
<script>
  function readBlob() {

    var files = document.getElementById('files').files;

    if (!files.length) {
      alert('Please select a file!');
      return;
    }

    var file = files[0];
    var start = 0;
    var stop = file.size;

    var reader = new FileReader();

    // If we use onloadend, we need to check the readyState.
    reader.onloadend = function(evt) {
      if (evt.target.readyState == FileReader.DONE) { // DONE == 2
        document.getElementById('byte_content').textContent = evt.target.result;
      }
    };

    var blob = file.slice(start, stop);
    reader.readAsBinaryString(blob);
  }

</script> 
</head>

<body>
<input type="file" id="files" name="file" accept=".csv"  />
<span class="readBytesButtons">
  <button onclick="readBlob()">entire file</button>
</span>
<div id="byte_range"></div>
<div id="byte_content"></div>
</body>

</html>

we can extract the excel data as text by FileReader in javascript. if you want to display the data in datatable, please apply your logic.

Altri suggerimenti

Yes off course,

I think you want to try something like this

  1. upload file to server

    • read the excel file in strut action, save it on disk in temp folder and return file name to your JSP.
  2. when first step done then make Ajax Call to struts action

    • pass file name as parameter fom datatable ajax call
    • load file from disk read it and populate in Pojo or VO object
    • create Json from Pojo using gosn or jakson api
    • finally return json.

further see data table ajax source

Yes this is possible, but no Ajax is need in your case. How would i do it? See below:

Have a form to upload the excel file to my struts implementation. Then do the processing of the excel file and return the contents in pure html/table format. Then do a simple datatables initialization on page load and your are finished!

UPDATE

For pagination, datatables support ajax like this:

var oTable = "";
$(document).ready(function() {
  oTable = $('#htmltableID').dataTable({
    "sPaginationType": "full_numbers",
    "bServerSide": true,
    "sAjaxSource": "/myAjaxSource.jsp?page="+pageNumber+",
    "sServerMethod": "POST",
    "iDisplayLength": 50
  });
}

Then in your myAjaxSource.jsp you get the parameter page sent by the ajax URL and return the appropriate data (json)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top