EasyUI Forum
May 03, 2024, 07:52:52 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
  Home Help Search Login Register  
  Show Posts
Pages: [1]
1  General Category / EasyUI for jQuery / Re: Custom AJAX settings on: March 07, 2018, 07:16:44 AM
FWIW, this post is for other newbies like myself looking to this 3-yr old forum topic for ideas on how to consume a pre-existing CRUD REST API with an EasyUI edatagrid.  The following seems to work for me with EasyUI jQuery 1.5.4.1; YMMV.  

Each datagrid row represents a resource; the datagrid itself represents a parent collection resource.  Each of my edatagrid row objects has an "id" property, which is not displayed as a datagrid field.  The id property becomes a REST URL parameter, i.e., the resource id.

I've left the edatagrid url, saveUrl, updateUrl, and destroyUrl option properties undefined, in favor of the following events.  I specify the edatagrid options singleSelect: true and autoSave: true.  I use Axios as an HTTP client with async/await and try/catch promise handling; jQuery Ajax would also work.  I use the queryParams datagrid options property to hold REST URL parameters, i.e., parent resource ID's.

I place a datagrid toolbar with a 'datagrid' attribute pointing to the edatagrid, and two buttons whose onclick functions call edatagrid('addRow') and edatagrid('destroyRow'), respectively.

I use the following edatagrid event handlers.
  • onBeforeLoad:  Download data with Axios (GET), call datagrid('loadData', data) method.
  • onSave:  If the row's "id" property is undefined, it's a new row (POST); else, it's an old row (PUT).
  • onDestroy:  Call Axios.delete (DELETE).

After a POST, the created resource id is available as the last URL component of the POST response's location header.  The created resource id is added to the row object with datagrid.('updateRow').

UPDATE:  For a more RESTful design, give each row a 'url' property instead of an 'id' property, and cache the parent resource url instead of id; treat URL's as opaque.

UPDATE:  This method produces a ton of JavaScript in the client page.  An alternative is to use the EasyUI url, saveUrl, updateUrl, and destroyUrl method, consuming the REST API from the server code.  The EasyUI url's are not dynamic, but you can put request parameters in the datagrid queryParams option.  Parameters appear as query components for a GET request, and are URL encoded in the request body for a POST request.
2  General Category / EasyUI for jQuery / Re: QueryParams not working on: February 17, 2018, 02:06:35 PM
Thanks,

When using method GET, the parameters are included in the GET URL as query components.  When using method POST, the request body contains the url-encoded parameters.

  --Steve
3  General Category / EasyUI for jQuery / QueryParams not working on: February 16, 2018, 07:39:30 AM
I'm having trouble getting queryParams to work.  Help, please.

EasyUI v. 1.5.4.1
Firefox 58.0.2(64 bit)/Windows 7/Node JS (on localhost)

Node JS console:

GET /test.html 200 0.790 ms - 495
GET /easyui/themes/default/easyui.css 304 0.515 ms - -
GET /easyui/themes/icon.css 304 0.919 ms - -
GET /easyui/jquery.easyui.min.js 304 1.082 ms - -
GET /easyui/jquery.min.js 304 1.373 ms - -
GET /easyui/themes/default/images/loading.gif 304 1.128 ms - -
POST /consoles/consoles 200 1.908 ms - 28

(Note -- no query parameters on POST url.)

test.html:

<html>
  <head>
      <link rel="stylesheet" href="easyui/themes/default/easyui.css">
      <link rel="stylesheet" href="easyui/themes/icon.css">
    <script src='easyui/jquery.min.js'></script>
    <script src='easyui/jquery.easyui.min.js'></script>
 </head>
 <body>
     <table class="easyui-datagrid" data-options="url:'consoles/consoles',queryParams:{foo:7}">
       <thead>
           <tr>
             <th data-options="field:'name'">Name</th>
           </tr>
       </thead>
     </table>
 </body>
</html>
Pages: [1]
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!