如何使用JQuery在kendo grid中导入和附加excel文件?

问题描述 投票:0回答:1

我有一个剑道网格,我想导入一个 excel 文件并将其附加到我的剑道网格中。例如,如果我的剑道网格中有 3 行,那么导入包含 2 行的 Excel 文件后,网格应该总共有 5 行数据。我尝试导入 Excel 文件,但无法将文件附加到网格。可以这样做吗?任何帮助将非常感激。

我尝试读取 Excel 文件并在网格中逐行加载行,然后在最后这样做。

              var grid = $("#grid").data("kendoGrid");
              var griddataSource = grid.dataSource._data;
              // Assuming the structure of Excel file matches the grid's schema
              for (var i = 1; i < rows.length; i++) {
                var row = rows[i];
                var dataItem = {
                  ProductID: row[0],
                  ProductName: row[1],
                  Price: row[2],
                };
                
                // Add the new data item to the grid's data source
                grid.dataSource.add(dataItem);
              }
              );
              $("#grid").data("kendoGrid").dataSource.data([]);
              $("#grid").data("kendoGrid").dataSource.data([griddataSource]);
              grid.refresh();`
            
I also tried another way using HTML table, with the help of below link but it also didnt work and I also didnt understand how they are using table.

[enter link description here][1]
https://www.aspsnippets.com/Articles/2499/Read-Parse-Excel-File-XLS-and-XLSX-using-jQuery/
javascript jquery kendo-ui kendo-grid import-from-excel
1个回答
0
投票

您需要使用网格方法 setDataSource() 以使网格重新绑定到新数据。请尝试以下方法。

var grid = $("#grid").data("kendoGrid");
var griddataSource = grid.dataSource;  // reference to the dataSource itself rather than its data
// Assuming the structure of Excel file matches the grid's schema
for (var i = 1; i < rows.length; i++) {
  var row = rows[i];
  var dataItem = {
    ProductID: row[0],
    ProductName: row[1],
    Price: row[2],
  };
            
  // Add the new data item to the grid's data source
  griddataSource.add(dataItem);
} 

grid.setDataSource(griddataSource);  // Replace the grid's datasource and rebind it
© www.soinside.com 2019 - 2024. All rights reserved.