简体   繁体   English

正确使用ag-grid

[英]Using ag-grid correctly

Is it possible to use Ag-Grid javascript version with ASP.Net MVC Application, If So, Please tell me how to use. 是否可以在ASP.Net MVC应用程序中使用Ag-Grid javascript版本,如果可以,请告诉我如何使用。

I tried the demo given in ag-grid site, AG-Grid . 我尝试了在ag-grid网站AG-Grid上进行的演示。

But it is not working fine with asp.net, I am getting Error says 但是它与asp.net不能正常工作,我收到错误消息

Uncaught TypeError: Cannot read property 'match' of undefined
at e.getTheme (ag-grid.min.js:242)
at e.specialForNewMaterial (ag-grid.min.js:20)
at e.getHeaderHeight (ag-grid.min.js:20)
at e.getGroupHeaderHeight (ag-grid.min.js:20)
at t.setBodyAndHeaderHeights (ag-grid.min.js:74)
at t.init (ag-grid.min.js:74)
at ag-grid.min.js:8
at Array.forEach (<anonymous>)
at ag-grid.min.js:8
at Array.forEach (<anonymous>)

I doubt if I am missing any other packages. 我怀疑是否遗漏了其他软件包。

  var rowData = [ { make: "Toyota", model: "Celica", price: 35000 }, { make: "Ford", model: "Mondeo", price: 32000 }, { make: "Porsche", model: "Boxter", price: 72000 } ]; var columnDefs = [ { headerName: "Make", field: "make" }, { headerName: "Model", field: "model" }, { headerName: "Price", field: "price" } ]; function doProcess() { var gridOptions = { rowData: rowData, columnDefs: columnDefs, onGridReady: function (params) { params.api.sizeColumnsToFit(); } }; new agGrid.Grid("#myGrid", gridOptions); } doProcess(); 
 <!-- Inside the view page --> <h2>Index</h2> <script src="https://unpkg.com/ag-grid/dist/ag-grid.min.noStyle.js"></script> <script src="https://unpkg.com/ag-grid@17.0.0/dist/ag-grid.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script> <link rel="stylesheet" href="https://unpkg.com/ag-grid/dist/styles/ag-grid.css"> <link rel="stylesheet" href="https://unpkg.com/ag-grid/dist/styles/ag-theme-balham.css"> <div id="myGrid" style="height: 131px; width:600px;" ></div> 

Looking at the blog shows that only ag-grid.min.noStyle.js is loaded in that demo, whereas you have loaded ag-grid.min.js as well. 综观博客显示,只有ag-grid.min.noStyle.js被加载在该演示中,而你已经装载ag-grid.min.js为好。 I would guess they are variants of the same thing, and this is almost certainly unnecessary. 我想它们是同一事物的变体,几乎可以肯定这是不必要的。 Also there is no apparent need for jQuery. 同样,也没有明显的jQuery需求。

Lastly though, and most importantly, you created your grid like this: 最后,也是最重要的是,您按以下方式创建了网格:

new agGrid.Grid("#myGrid", gridOptions);

by passing in a selector string directly. 通过直接传递选择器字符串。 However the demo (and no doubt the documentation, if you check it) clearly shows that a DOM element is passed in, created by using document.querySelector, ie 然而,该演示(无疑是文档,如果您对其进行了检查)则清楚地表明,传入了一个通过使用document.querySelector创建的DOM元素,即

var eGridDiv = document.querySelector('#myGrid');
        new agGrid.Grid(eGridDiv, gridOptions);

Since you passed the grid something it didn't understand, it can't load anything into it. 由于您将网格传递给它不了解的内容,因此无法将任何内容加载到其中。

Working demo: 工作演示:

 var rowData = [ { make: "Toyota", model: "Celica", price: 35000 }, { make: "Ford", model: "Mondeo", price: 32000 }, { make: "Porsche", model: "Boxter", price: 72000 } ]; var columnDefs = [ { headerName: "Make", field: "make" }, { headerName: "Model", field: "model" }, { headerName: "Price", field: "price" } ]; function doProcess() { var gridOptions = { rowData: rowData, columnDefs: columnDefs, onGridReady: function (params) { params.api.sizeColumnsToFit(); } }; var eGridDiv = document.querySelector('#myGrid'); new agGrid.Grid(eGridDiv, gridOptions); } doProcess(); 
 <!-- Inside the view page --> <h2>Index</h2> <script src="https://unpkg.com/ag-grid@17.0.0/dist/ag-grid.min.noStyle.js"></script> <link rel="stylesheet" href="https://unpkg.com/ag-grid/dist/styles/ag-grid.css"> <link rel="stylesheet" href="https://unpkg.com/ag-grid/dist/styles/ag-theme-balham.css"> <div id="myGrid" style="height: 131px; width:600px;" ></div> 

NB If you're going to follow a demo, it's usually wise to follow it accurately, and only change things where you understand the consequences and actually need to change them to fulfil your own project's requirement. 注意:如果您要遵循一个演示,通常明智的做法是准确地遵循它,并且仅在了解后果的地方进行更改,并且实际上需要进行更改以满足您自己项目的要求。 In the case of the changes you have made, most of them look unnecessary, including the one which caused the problem. 对于所做的更改,大多数更改看起来都是不必要的,包括引起问题的更改。

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

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