从上传的CSV文件创建DataTable

时间:2016-01-12 10:32:36

标签: javascript jquery twitter-bootstrap csv datatables

我是初学程序员。

我正在使用jQuery Data Table with Bootstrap来创建表格。

我需要从上传的CSV文件中创建表格。

有没有办法为json创建csv数据?

'submit form': function (event){
     console.log("Submitting form");
     event.preventDefault();
     var frm = document.getElementById('frm');
     var csvdata = new FormData(event.target);
     console.log("data:: " + csvdata);

     // Code to create dataTable out of csv
     // $('#mytable').DataTable();
}

我该怎么做?

2 个答案:

答案 0 :(得分:3)

正如Rory McCrossan指出的那样,FileReader方法也很有效well

$("#fileinput").on("change", function(evt) {
    var f = evt.target.files[0];
    if (f) {
        var r = new FileReader();
        r.onload = function(e) {
            table.rows.add($.csv.toObjects(e.target.result)).draw();
        }
        r.readAsText(f);
    } else {
        alert("Failed to load file");
    }
});

答案 1 :(得分:1)

而不是将import matplotlib matplotlib.use('TkAgg') from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg import multiprocessing import time import random from Tkinter import * #Create a window window=Tk() def main(): #Create a queue to share data between process q = multiprocessing.Queue() #Create and start the simulation process simulate=multiprocessing.Process(None,simulation,args=(q,)) simulate.start() #Create the base plot plot() #Call a function to update the plot when there is new data updateplot(q) window.mainloop() print 'Done' def plot(): #Function to create the base plot, make sure to make global the lines, axes, canvas and any part that you would want to update later global line,ax,canvas fig = matplotlib.figure.Figure() ax = fig.add_subplot(1,1,1) canvas = FigureCanvasTkAgg(fig, master=window) canvas.show() canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1) canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1) line, = ax.plot([1,2,3], [1,2,10]) def updateplot(q): try: #Try to check if there is data in the queue result=q.get_nowait() if result !='Q': print result #here get crazy with the plotting, you have access to all the global variables that you defined in the plot function, and have the data that the simulation sent. line.set_ydata([1,result,10]) ax.draw_artist(line) canvas.draw() window.after(500,updateplot,q) else: print 'done' except: print "empty" window.after(500,updateplot,q) def simulation(q): iterations = xrange(100) for i in iterations: if not i % 10: time.sleep(1) #here send any data you want to send to the other process, can be any pickable object q.put(random.randint(1,10)) q.put('Q') if __name__ == '__main__': main() 上传到服务器并在那里处理它,然后在DataTable中显示它,如果您有权访问原始数据数据并且您知道其结构,则呈现csv没有碰到服务器。

example使用jquery-csv来解析textarea的内容并填充表格。相关功能是:

cvs

希望有所帮助。