简体   繁体   English

如何为 Kendo UI jQuery 的 SpreadSheet 重新加载数据?

[英]How to reload data for SpreadSheet of Kendo UI jQuery?

I am fetching data from a remote source and displaying the data into a spreadsheet component of Kendo UI jQuery.我正在从远程源获取数据并将数据显示到 Kendo UI jQuery 的电子表格组件中。

The first time the data is fetched and displayed on the screen, everything is fine, however, if the data is fetched a second time a new spreadsheet gets appended to the original one.第一次获取数据并显示在屏幕上时,一切都很好,但是,如果第二次获取数据,则会在原始电子表格上附加一个新电子表格。

What would the proper way to refresh the data in a spreadsheet?刷新电子表格中数据的正确方法是什么?

Please see this sample code here that contains the same issue I've described above.请在此处查看此示例代码,其中包含我在上面描述的相同问题。 Continue clicking the search button and you will see it appends a new spreadsheet to the page.继续单击搜索按钮,您将看到它向页面附加了一个新的电子表格。

https://dojo.telerik.com/ewigOKam https://dojo.telerik.com/ewigOKam

If you create the spreadsheet one time, then afterward upon each Search button click you can refresh the datasource of the spreadsheet.如果您创建了一次电子表格,那么之后每次单击“搜索”按钮时,您都可以刷新电子表格的数据源。 Of course, you'll want to take into account what this does to any changes the user may have made before you refresh the datasource.当然,在您刷新数据源之前,您需要考虑这对用户可能所做的任何更改的影响。

Updated dojo: https://dojo.telerik.com/EGePecal更新 dojo: https://dojo.telerik.com/EGePecal

And the code from that dojo (always better to include the code in questions and not just in links):以及来自 dojo 的代码(最好将代码包含在问题中,而不仅仅是链接中):

  <button class="k-button" onClick="search();">Search</button>
  <div id="spreadsheet"></div>

<script>

  $(function() {
    $("#spreadsheet").kendoSpreadsheet();
  });
  
  function search() {

    var dataSource = new kendo.data.DataSource({
      transport: {
        read:  {
          url: "//demos.telerik.com/kendo-ui/service/Products",
          dataType: "jsonp"
        }
      }
    });

    var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
    var sheet = spreadsheet.activeSheet();
    sheet.setDataSource(dataSource);

    console.log(sheet.dataSource);
  }

</script>

UPDATE : Here is another way to do the same thing which is a little cleaner - this one pulls from the Telerik website sample for DataSource binding for the spreadsheet - https://demos.telerik.com/as.net-core/spreadsheet/datasource更新:这是做同样事情的另一种方法,它更简洁一些 - 这个方法来自 Telerik 网站示例,用于电子表格的数据源绑定 - https://demos.telerik.com/as.net-core/spreadsheet/数据源


<button class="k-button" onClick="search();">Search</button>
<div id="spreadsheet"></div>

<script>

  $(function() {
    
    var dataSource = new kendo.data.DataSource({
      transport: {
        read:  {
          url: "//demos.telerik.com/kendo-ui/service/Products",
          dataType: "jsonp"
        }
      }
    });
    
   
    $("#spreadsheet").kendoSpreadsheet({
      columns: 20,
      rows: 100,
      toolbar: false,
      sheetsbar: false,
      sheets: [{
        name: "Products",
        dataSource: dataSource,
        rows: [{
          height: 40,
          cells: [
            {
              bold: "true",
              background: "#9c27b0",
              textAlign: "center",
              color: "white"
            },{
              bold: "true",
              background: "#9c27b0",
              textAlign: "center",
              color: "white"
            },{
              bold: "true",
              background: "#9c27b0",
              textAlign: "center",
              color: "white"
            },{
              bold: "true",
              background: "#9c27b0",
              textAlign: "center",
              color: "white"
            },{
              bold: "true",
              background: "#9c27b0",
              textAlign: "center",
              color: "white"
            }]
        }],
        columns: [
          { width: 100 },
          { width: 415 },
          { width: 145 },
          { width: 145 },
          { width: 145 }
        ]
      }]
    });

    
    });
  
  function search() {
     var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
     spreadsheet.activeSheet().dataSource.read();
  }

</script>

above code in dojo: https://dojo.telerik.com/ILuxuTeG以上代码在 dojo: https://dojo.telerik.com/ILuxuTeG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM