JavaScript does not require compilation to run, which makes applications built by JavaScript very flexible. We can dynamically load JavaScript scripts from the server as needed to create and control the UI to interact with users. The following is a combination of Ext JS to illustrate how to dynamically load JS scripts from the server to create a form dynamically.
1 Project structure:
The project structure is as follows: The GetJSUI general handler is used to grab UI configuration from the database table and return it to the client; HTML files and JS libraries are used in the Contents folder.
2 Database table structure
You can use the following SQL to create a table in MSSQL, where the JavaScriptContent field stores specific JS scripts.
CREATE TABLE [dbo].[Ext_Dynamic_Form]( [ID] [nvarchar](50) NOT NULL, [UniName] [nvarchar](50) NULL, [JavaScriptContent] [nvarchar](4000) NULL, [Memo] [nvarchar](200) NULL, CONSTRAINT [PK_Ext_Dynamic_Form] PRIMARY KEY CLUSTERED ( [ID] ASC)) ON [PRIMARY]
After creation, you can initialize the data:
After creation, you can initialize the data:
4 GetJSUI programming
using System;using System.Collections.Generic;using System.Linq;using System.Web;using CMCloudDBHelper;namespace extjs6.Services{ /// <summary> /// author:jackwangcumt /// </summary> public class GetJSUI : IHttpHandler { public void ProcessRequest(HttpContext context) { string js = ""; context.Response.ContentType = "text/plain"; //context.Response.ContentType = "text/javascript"; CMCDataAccess da = new CMCDataAccess(); string SQLForJS = "select * FROM Ext_Dynamic_Form where ID='006'"; System.Data.DataTable dt= da.GetDataTable(SQLForJS); if(dt!=null) { if(dt.Rows.Count==1) { js = dt.Rows[0]["JavaScriptContent"].ToString(); } } //utf-8 context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.Write(js); } public bool IsReusable { get { return false; } } }}5 main interface html
<html><head> <title>Dynamically generate forms from server javascript</title> <!-- Library Files --> <meta http-equiv="X-UA-Compatible" content="IE=edge" charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <script type="text/javascript" src="ext6/ext-all-debug.js"></script> <link rel="stylesheet" type="text/css" href="ext6/classic/theme-triton/resources/theme-triton-all-debug.css"> <script type="text/javascript" src="ext6/classic/theme-triton/theme-triton-debug.js"></script> <script type="text/javascript"> //load *.js file from server function loadScript(url, callback) { var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState) { //IE script.onreadystatechange = function() { if (script.readyState == "loaded" || script.readyState == "complete") { script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function() { callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script); } //load js text from server function loadScriptSrc(js, callback) { var script = document.createElement("script") script.type = "text/javascript"; //script.async = true; if (script.readyState) { //IE script.onreadystatechange = function() { if (script.readyState == "loaded" || script.readyState == "complete") { script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function() { callback(); }; } script.text = js; console.log(script); document.getElementsByTagName("head")[0].appendChild(script); //Cannot callback(); } //Ext JS 6 Ext.onReady(function() { //https://www.sencha.com/forum/showthread.php?268193-How-to-load-content-dynamically-for-tabpanel var pmain = Ext.widget('panel', { renderTo: document.body, height: 800, width: 800, layout: 'border', items: [{ title: 'West', region: 'west', width: 200, collapse: true }, { xtype: 'tabpanel', region: 'center', items: [{ title: 'First Tab', itemId: 'tab01', }, { title: 'Second Tab', layout: 'fit', loader: { url: 'Form.json', autoLoad: true, renderer: 'component' } }] }] }); //ajax config var reqConfig = { url: '../Services/GetJSUI.ashx', method: 'get', callback: function (options, success, response) { // var msg = ['success:', success, '/n', 'data:', response.responseText]; // alert(msg.join('')); loadScriptSrc(response.responseText, function() { Ext.Msg.alert("loaded js","Loaded JS from server"); var gp = Ext.create("gpView"); Ext.ComponentQuery.query('#tab01')[0].add(gp); }); } }; Ext.Ajax.request(reqConfig); //loadScript("dynamicLoadJS2.js", function() { // Ext.Msg.alert("loaded"); // var gp = Ext.create("gpView"); // //console.log(gp); // //gp.body.renderTo(pmain); // // Ext.ComponentQuery.query('#tab01')[0].add({ // // html: 'Oh, Hello.' // // }); // Ext.ComponentQuery.query('#tab01')[0].add(gp); //}); }); </script></head><body></body></html>6 Run
In this way, we can make a main framework, and then build a general system such as menus and permissions. By configuring menus and UI in the database, we can dynamically expand the application.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.