What are the minimum files needed for jqGrid to work with MVC?

I uploaded files for jqGrid, but I'm not quite sure which files I need to link to. So far I have these files:

<link href='@Url.Content("~/Content/themes/base/jquery-ui.css")' ... /> <link href='@Url.Content("~/Content/themes/redmond/jquery-ui-1.8.custom.css")' ... /> <link href='@Url.Content("~/Content/themes/redmond/ui.jqgrid.css")' ... /> <link href='@Url.Content("~/Scripts/jquery.jqGrid-4.3.1/css/ui.jqgrid.css")' ... /> <script src='@Url.Content("~/Scripts/jquery-1.7.1.min.js")' ...></script> <script src='@Url.Content("~/Scripts/jquery-ui-1.8.16.min.js")' ...></script> <script src='@Url.Content("~/Scripts/jquery.jqGrid-4.3.1/js/i18n/grid.locale-en.js")' ...></script> <script src='@Url.Content("~/Scripts/jquery.jqGrid-4.3.1/js/jquery.jqGrid.min.js")' ...></script> 

I saw another example, assuming these are files needed in addition to jQuery. Can someone confirm this is all that is needed and in the correct order, or advise if I need to add more. For example, do I need a link to a locale file?

I'm just starting to learn about jqGrid. I looked around and have not yet found a good example of how to use this with MVC3 and Razor. Does anyone have links to links that they found very useful. It just really needs one good link, but most of the links I found with Google were old and didn't use Razor.

+4
source share
2 answers

The minimum list of CSS and JavaScript files that need to be included can be found in the jqGrid documentation . In your case it will be

 <link href='@Url.Content("~/Content/themes/redmond/jquery-ui-1.8.custom.css")' rel='stylesheet' type='text/css' /> <link href='@Url.Content("~/Scripts/jquery.jqGrid-4.3.1/css/ui.jqgrid.css")' rel='stylesheet' type='text/css' /> <script src='@Url.Content("~/Scripts/jquery-1.7.1.min.js")' type='text/javascript'></script> <script src='@Url.Content("~/Scripts/jquery.jqGrid-4.3.1/js/i18n/grid.locale-en.js")' type='text/javascript'></script> <script src='@Url.Content("~/Scripts/jquery.jqGrid-4.3.1/js/jquery.jqGrid.min.js")' type='text/javascript'></script> 
+3
source

I would consider this as a canonical example - a piece of code that represents a simple, runnable - jqGrid with minimal coding , but still powerful enough to show the most important functions (according to this documentation ):

 // see: https://free-jqgrid.imtqy.com/getting-started/ // CDN used: https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid $(function() { $("#grid").jqGrid({ autowidth: true, height: 45, colNames: ['First name', 'Last name', 'Updated?'], colModel: [{name: "firstName"}, {name: "lastName"}, {name: "updated"}], data: [ { id: 10, firstName: "Jane", lastName: "Doe", updated: "no"}, { id: 20, firstName: "Justin", lastName: "Time", updated: "no" } ], loadComplete: function() { // demo - how to access grid data var ids = $(this).jqGrid('getDataIDs'); // ids for each row var gridData = $(this).jqGrid('getGridParam', 'data'); // all rows var gridLen = gridData.length; // number of rows // now get a list from the data var nameList = []; for(var i=0; i<gridLen; i++) { nameList.push(gridData[i].firstName+' [id:'+ ids[i] +']'); } var strList = nameList.join(", "); $("#nameList").html(strList); var rowKey = ids[1]; // rowKey for the operations below // get data from the 2nd row var secondRow=$(this).jqGrid('getRowData', rowKey); $("#getRowData").html(secondRow.firstName + ', ' + secondRow.lastName + ', updated:' + secondRow.updated); // set update flag by modifying row data secondRow.updated = "yes"; $(this).jqGrid('setRowData', rowKey, secondRow); // update single cell (read, modify, write) var lastName=$(this).jqGrid('getCell', rowKey, "lastName"); lastName=lastName.toUpperCase(); // first change the cell in the visible part of grid $(this).jqGrid('setCell', rowKey, "lastName", lastName); // now change the internal local data $(this).jqGrid('getLocalRow', rowKey).lastName = lastName; // make column label of "Updated?" column larger $(this).jqGrid('setLabel', 2, '<h3>Updated?</h3>'); // --- now verify the changes --- gridData = $(this).jqGrid('getGridParam', 'data'); // get rows again var rowList = []; for(var i=0; i<gridLen; i++) { rowList.push(gridData[i].firstName + ', ' + gridData[i].lastName + ', updated:' + gridData[i].updated); } $("#showGridData").html(rowList.join("; ")); } }); }); 
 <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <title>Canonical jqGrid example</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.min.css"/> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.15.2/css/ui.jqgrid.min.css"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.15.2/jquery.jqgrid.min.js"></script> <table id="grid"></table> <br/><b>First names list:</b><div id="nameList"></div> <br/><b>Get data from row#1 (before update):</b><div id="getRowData"></div> <br/><b>Verify changes (after update):</b><div id="showGridData"></div> 

You can use it together with Oleg as a working starting point for your development in your own MVC application (in this case, use the @Url.Content syntax, as in the answer above), and, finally, no less than a code snippet template for further StackOverflow jqGrid related questions.

I added code to show how you can access internal grid data. It addresses issues such as "how can I access data from a specific row?"
However, if you don't need this code in your snippet, you can just delete the entire loadComplete section, the grid demonstration will still work.

If you need paging in your snippet, see this answer.

Note. I spent a lot of time on jqQuery keys (you need to uniquely address the string) and how they work. Here is some information from my experience in a nutshell:

  • Unless you explicitly specify a key in colModel , then JQGrid assumes an "id" as the key field . This is an example of this example: data fills the id field and it is used as a key

  • Otherwise, if you need another key , specify {name: "myKey", hidden:true, key:true} in colModel and "myKey" in colNames (if you forget this, you will get a counter mismatch error). Then you can fill in "myKey" in the data. Important order ! In this case, there is no "id" field, but a "myKey" . Key fields do not have to be hidden. If you omit the hidden attribute (or set false as a value), then the key is displayed as a column in the grid.
    Please note that in case of such key redefinition, jqGrid internally uses _id_ as a property to store the key. You can see that if you use the .jqGrid('getGridParam', 'data') function, then each row contains such a property.

  • Specifying key: true useless several times , because only the last field with this attribute is considered as a key (i.e., the rightmost column of keys). Having said that, you cannot specify compound keys in this way! If you need a composite key, you need to combine the keys into one key field.

  • If you do not specify the key yourself and do not fill in the id field, then jqGrid creates its own values and assigns their field id each row. Usually they are called "jqg1" (for the first line), "jqg2" (for the second), etc.

  • This example also shows how to update a row. See here and there for more details. Please note: if you update the data in this way, it is updated only on the client (in your browser). You must save the value (i.e., send updated data to the server via AJAX, provide the SAVE button, etc.), if you want to make the change permanent, otherwise it will be reset when you reload.

  • Column headers (that is, headers displayed to the user) are determined by the colNames property, do not confuse them with name inside colModel . The name property inside colModel defines field names, which is important for data binding. The order of appearance in colNames and colModel important because they correlate with each other (for example, the 'Last Name' in colNames appears in second position, as well as the corresponding field { name: '"lastName"' ...} inside colModel displayed in position 2 )
    If you want to change the column names later in your code (i.e. after the definition), look here: Dynamically update column headers.


Useful link: Free jqGrid version - getting started

+1
source

Source: https://habr.com/ru/post/1390861/


All Articles