顯示具有 EasyUI 標籤的文章。 顯示所有文章
顯示具有 EasyUI 標籤的文章。 顯示所有文章

2025年9月5日 星期五

用 EasyUI for jQuery 與 Django 建構網站 (二)

在前一篇測試中已下載 EasyUI for jQuery 並建立好 easyuicms 專案的基本目錄結構, 接下來要將解壓縮好的 EasyUI 程式與樣式檔放進專案的 static 目錄下, 建立網頁模板檔案後放到 templates 目錄下, 然後建立一個 App 來回應客戶端請求.

本系列之前的測試文章參考 :



4. 配置靜態檔案 : 

首先將解壓縮 EasyUI for jQuery 後會得到 EasyUI 主程式 jquery.easyui.min.js, 以及其所依賴的 jquery.min.js, 需將這兩個程式檔複製到 static/js 目錄下. 接著在 js 下新建一個 locale 子目錄, 把解壓縮目錄 locale 下的繁體中文語言檔 easyui-lang-zh_TW.js 放在 locale 下. EasyUI 的核心程式碼和預設資源檔案 (例如 jquery.easyui.min.js) 內建的文字提示與日期格式等都是英文, 這個語言檔可以讓系統顯示中的文字改為繁體中文. 

EasyUI 提供許多主題布景樣式, 全部放在解壓縮目錄 themes 下, 初期為了簡單考量, 僅使用 default 預設主題, 所以在 themes 目錄下新建一個 default 子目錄, 然後將解壓縮目錄 themes/default 下的 easyui.css 複製到 /static/themes/default 下. 另外, 解壓縮目錄 themes 下的 icon.css 也要複製到 static/themes 下, 這是 EasyUI 元件使用的圖示 (例如按鈕上的小圖標, 樹狀結構的箭頭等) 之樣式, 此為所有主題共用, 不隨主題改變. 

用 tree 指令檢視目前目錄結構 : 

D:\django>tree easyuicms /f    
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\DJANGO\EASYUICMS
│  db.sqlite3
│  manage.py
│
├─easyuicms
│  │  asgi.py
│  │  settings.py
│  │  urls.py
│  │  wsgi.py
│  │  __init__.py
│  │
│  └─__pycache__
│          settings.cpython-310.pyc
│          urls.cpython-310.pyc
│          wsgi.cpython-310.pyc
│          __init__.cpython-310.pyc
│
├─static
│  ├─images
│  ├─js
│  │  │  jquery.easyui.min.js
│  │  │  jquery.min.js
│  │  │
│  │  └─locale
│  │          easyui-lang-zh_TW.js
│  │
│  └─themes
│      │  icon.css
│      │
│      └─default
│              easyui.css
│
└─templates
        
此架構保留了未來需要套用其它主題樣式時之彈性, 其它主題可在 static/themes 下建立目錄並複製其 easyui.css 在該主題之目錄下. 另外若要支援多語系, 將語系檔複製到 locale 下即可.


5. 編輯網頁模板 : 

網頁模板用來讓 Django 動態渲染網頁輸出, Django 的模板系統採用 Jinja2, 可以一層一層用 extends 繼承, 用法參考 :


首先編輯基礎模板 base.htm : 

<!-- base.htm -->
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>{% block title %}{% endblock %}</title>
  {% block link %}{% endblock %}
  {% block script %}{% endblock %}
  <style>
  {% block style %}{% endblock %}
  </style>
</head>
<body>
  {% block body %}{% endblock %}
</body>
</html>

基礎模板中定義了 title, link, script, style, 以及 body 五個區塊 (block) 讓子模板繼承時去填充. 接下來編輯繼承 base.htm 的子模板 easyui.htm : 

<!-- easyui.htm -->
{% extends "base.htm" %}
{% load static %}
{% block link %}
  <link id="theme" rel="stylesheet" href='{% static "themes/default/easyui.css" %}'>
  <link rel="stylesheet" href='{% static "themes/icon.css" %}'>
{% endblock %}
{% block script %}
  <script src='{% static "js/jquery.min.js" %}'></script>
  <script src='{% static "js/jquery.easyui.min.js" %}'></script>
  <script src='{% static "js/locale/easyui-lang-zh_TW.js" %}'></script>
{% endblock %}

子模板必須在開頭使用 extends 指令繼承父模板. 此 easyui.htm 模板因為要引入靜態檔案, 所以需在引入前用模板標籤 {% load static %} 告訴 Django 的模板引擎載入靜態檔案相關的模板標籤 {% static}, 這樣才能使用 {% static %} 標籤來生成靜態檔案的正確路徑. 

這樣就可以繼承 easyui.htm 來撰寫 App 要渲染的網頁模板了, 結構如下 :

<!-- demo.htm -->
{% extends "easyui.htm" %}
{% block body %}
<!-- 網頁內容寫在下面 -->
<b>Hello World!</b>
{% endblock %}

將此三個模板檔放到 templates 下備用. 

用 tree 指令檢視目前目錄結構 : 

D:\django>tree easyuicms /f
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\DJANGO\EASYUICMS
│  db.sqlite3
│  manage.py
│
├─easyuicms
│  │  asgi.py
│  │  settings.py
│  │  urls.py
│  │  wsgi.py
│  │  __init__.py
│  │
│  └─__pycache__
│          settings.cpython-310.pyc
│          urls.cpython-310.pyc
│          wsgi.cpython-310.pyc
│          __init__.cpython-310.pyc
│
├─static
│  ├─images
│  ├─js
│  │  │  jquery.easyui.min.js
│  │  │  jquery.min.js
│  │  │
│  │  └─locale
│  │          easyui-lang-zh_TW.js
│  │
│  └─themes
│      │  icon.css
│      │
│      └─default
│              easyui.css
│
└─templates
        base.htm
        demo.htm
        easyui.htm


6. 建立應用程式 (App) : 

App 是 Django 專案中具有獨立功能的可插拔元件, 亦即可將網頁專案的服務邏輯按照功能切割為數個 App 以符合軟體工程中元件可重用 (reusable) 之要求, 每一個 App 可以在最小程度的調整後移植到其他專案中使用.

此處要建立一個 app 來展示 EasyUI 的常用元件功能, 建立 App 的指令格式如下 :

python manage.py startapp <AppName>  

在上層專案目錄下, 用下列指令建立一個名為 demo 的 app :

D:\django\easyuicms>python manage.py startapp demo   

跳到上一層用 tree 檢視目錄結構 :

D:\django\easyuicms>cd .. 
D:\django>tree easyuicms /f   
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\DJANGO\EASYUICMS
│  db.sqlite3
│  manage.py
│
├─demo
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py
│  │  views.py
│  │  __init__.py
│  │
│  └─migrations
│          __init__.py
│
├─easyuicms
│  │  asgi.py
│  │  settings.py
│  │  urls.py
│  │  wsgi.py
│  │  __init__.py
│  │
│  └─__pycache__
│          settings.cpython-310.pyc
│          urls.cpython-310.pyc
│          wsgi.cpython-310.pyc
│          __init__.cpython-310.pyc
│
├─static
│  ├─images
│  ├─js
│  │  │  jquery.easyui.min.js
│  │  │  jquery.min.js
│  │  │
│  │  └─locale
│  │          easyui-lang-zh_TW.js
│  │
│  └─themes
│      │  icon.css
│      │
│      └─default
│              easyui.css
│
└─templates
        base.htm
        demo.htm
        easyui.htm

可見在上層專案目錄下多了一個 demo 子目錄, 其下之檔案用途說明如下表 : 


 App 檔案目錄 說明
 migrations 紀錄與資料庫版本與資料模型變更相關之檔案 (用來同步模型與資料庫)
 __init__.py 空檔案, 使 App 目錄形式上符合 Python 套件要求
 admin.py 用來註冊資料模型以便能於管理網頁中管理資料表
 apps.py 儲存與此 App 相關之設定
 models.py 儲存 App 資料模型定義以及資料間之關係
 test.py 儲存測試程式碼
 views.py 為 urls.py 處理 HTTP 要求與回應之程式檔


其中只有 admin.py, models.py, 以及 views.py 這三個檔案需要修改, 其餘都不用改. 


7. 修改專案設定檔 settings.py  : 

完成上面靜態檔案與網頁模板配置, 以及 App 的新增後, 必須修改下層專案目錄下的 settings.py 才會生效, 用文字編輯器開啟 settings.py, 首先找到 Application definition 區段, 在 INSTALLED_APPS 串列末端添加上面新增的 demo 應用程式  : 

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'demo',
]

同時將此區段之 TEMPLATES 串列中的 DIRS 屬性值由預設之空串列改為  [BASE_DIR / 'templates'], 這樣 Django 渲染網頁時才知道網頁模板在哪裡 : 

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

其次是將 Internationalization 區段的語言 (LANGUAGE_CODE) 與時區 (TIME_ZONE) 變數值改為繁體中文 (zh-Hant) 與台灣時區 (Asia/Taipei) :  

# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = 'zh-Hant'

TIME_ZONE = 'Asia/Taipei'

USE_I18N = True

USE_TZ = True

最後在最底下的 Static files 區段中, 於 STATIC_URL 變數底下添加一個 STATICFILES_DIRS 串列來指定靜態目錄之絕對路徑 : 

STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static',]   

修改完畢將 settings.py 存檔. 


7. 請求處理與路由分派 : 

Django 對控制邏輯提供了彈性的選擇, 可視專案複雜度來選擇請求處理與路由分派的配置模式, 簡單的專案可以直接在下層專案目錄中自行添加請求處理器 views.py 即可; 複雜的專案則可以採用專案路由器 (建立專案時會自動產生) 與 App 路由器 (新增 App 時不會自動產生, 需自行建立) 分離的結構, 這樣做的好處是 App 與專案的耦合度 App 低 (可插拔設計), 可方便移植到別的專案中使用. 

具體作法是在 App 目錄下自行建立 App 自己的路由器 urls.py 來分派屬於此 App 的請求給自己的請求處理器 views.py, 然後在專案路由器中用 django.urls.include() 將該 App 的路由器含括進來, 這樣專案路由器的內容就很清爽, 可一目了然看出專案內有哪些 App; 當 App 要移植到別的專案中使用時只要將 App 目錄複製過去, 然後於新專案的專案路由器中 include 該 App 的路由器即可, 架構如下圖所示 :




搭配此架構的 RESTful 風格網址結構如下 :

Protocol://Host address/AppName/AppURL/param1/param2/param3...

協定 (Protocol) 為 http 或 https, 主機位址是 IP (例如 127.0.0.1) 或域名 (localhost, www.abc.com 等), 接下來是 App 名稱與 App 位址 (在 App 的 urls.py 中映射到 views.py 中的函式名稱), 最後跟著的是參數群 (可有可無). 

RESTful 網址會由左至右被一層一層剝掉 (striping), 當 HTTP 伺服器收到請求時, 首先協定與位址會先被伺服器剝除, 剩下 AppName/AppURL/param1/param2/param3... 被送到 Django 的專案路由器處理, 它會在取出 AppName 後將剩下的路徑送給該 App 的路由器, 然後取出的 AppURL (圖中的 url1) 會被映射到 views.py 中的函數 fun1 處理 (同時也會將參數群傳入該函數) :




參考 :



8. 編輯 App 的請求處理器 views.py : 

上面建立應用程式 demo 時, 會在 demo 目錄下自動建立一個請求處理器 views.py, 它負責處理路由器 urls.py 所分派的請求, 執行特定邏輯產生回應給客戶端, 其預設內容僅有匯入 django.shortcuys.render() 函式一行而已, 此 render() 函式用來透過模板來渲染網頁 :

from django.shortcuts import render

# Create your views here.

我們需在此定義函式, 撰寫運算邏輯以便產生回應, 例如在瀏覽器輸入網址 http://127.0.0.1:8000/demo/hello/Tony 就會回應 'Hello Tony, 現在時間是 2025-09-05 12:30:00' 的 views.py 可以這麼寫 :

# views.py of App=demo 
from django.shortcuts import render
from datetime import datetime

def hello(request, name):
    now=datetime.now()
    params={'name': name, 'now': now}
    return render(request, 'hello.htm', params)

此函式 hello() 預設接收 request 物件參數, 後面可接收任意多的網址參數 (此處為 name), 將 name 參數與日期時間組成字典後傳給 render(), 指定由網頁模板 hello.htm 來取得此參數並渲染回應網頁. 此網頁可以繼承上面的 easyui.htm 模板來簡化結構 : 

<!-- hello.htm -->
{% extends "easyui.htm" %}
{% block body %}
<--! 網頁內容寫在下面 -->
  <p class="msg">Hello, {{ name }}, 現在的時間是 {{ now }}</p>
{% endblock %}

將此 hello.htm 存入 templates 目錄下即可. 


9. 編輯 App 路由器 urls.py : 

檢視上面的目錄結構樹狀圖, 在下層專案目錄 easyuicms 下有一個 urls.py, 這就是專案路由器, 在建立專案時 Django 會自動產生此檔案, 其預設內容如下 : 

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

可見 urlpatterns 串列中預設只有管理應用程式 admin 而已. 

由於新增 App 時 Django 並不會自動為 App 建立自己的路由器 (因為配置方式是可自訂的), 所以先將專案路由器 urls.py 複製到 App 目錄下編輯, 保留 path 並去除屬於專案的 admin 部分, 添加從 App=demo 匯入 views 模組, 內容修改如下 :

# urls.py of App=demo
from django.urls import path
from demo import views

app_name="demo"

urlpatterns = [
    path('hello/<name>/', views.hello),
]

此處 app_name 就是 RESTful 網址中的 AppName 層, 而 path() 則是將去除 AppName 後的路徑 hello/<name>/ 指派給 views.py 中的 hello() 函式處理. 


10. 編輯專案路由器 urls.py :   

最後來編輯位於下層專案目錄 easyuicms 下的專案路由器 urls.py, 如上所述, 我們需要會匯入 django.urls.include() 函式來把 App 的路由器含括進來, 修改如下 :

# urls.py of Project=easyuicms
from django.contrib import admin
from django.urls import path
from django.urls import include   

urlpatterns = [
    path('admin/', admin.site.urls),
    path('demo/', include('demo.urls')),
]

此處 path() 的第一參數就是 RESTful 網址中的 AppName (後面須加斜線), 第二參數用 django.urls.include() 函式把 App 路由器含括進來, 也就是專案路由器不直接處理 demo 路徑的請求, 而是交由 App=demo 自行分派, 這樣專案路由器本身就不會亂糟糟, 而且可清楚知道此專案有哪些 App. 

最後用 tree 指令檢視整個目錄結構 :

D:\django>tree easyuicms /f
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\DJANGO\EASYUICMS
│  db.sqlite3
│  manage.py
│
├─demo
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py
│  │  urls.py
│  │  views.py
│  │  __init__.py
│  │
│  └─migrations
│          __init__.py
│
├─easyuicms
│  │  asgi.py
│  │  settings.py
│  │  urls.py
│  │  wsgi.py
│  │  __init__.py
│  │
│  └─__pycache__
│          settings.cpython-310.pyc
│          urls.cpython-310.pyc
│          wsgi.cpython-310.pyc
│          __init__.cpython-310.pyc
│
├─static
│  ├─images
│  ├─js
│  │  │  jquery.easyui.min.js
│  │  │  jquery.min.js
│  │  │
│  │  └─locale
│  │          easyui-lang-zh_TW.js
│  │
│  └─themes
│      │  icon.css
│      │
│      └─default
│              easyui.css
│
└─templates
        base.htm
        demo.htm
        easyui.htm
        hello.py

這樣就完成所有網站配置了.


11. 做 migration 與啟動開發伺服器 :   

在前一篇測試中啟動瀏覽器時會出現與資料庫同步有關的 "migration" 訊息, 雖然目前還沒有用到資料庫, 但還是按步驟先用下列指令建立資料庫 migration 資料檔, 並與資料庫同步 :

python manage.py makemigrations     (建立遷移檔)
python manage.py migrate    (與資料庫同步)

D:\django\easyuicms>python manage.py makemigrations    
No changes detected
D:\django\easyuicms>python manage.py migrate     
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

這樣在啟動開發伺服器時就不會出現要做 "migration" 的提醒了 :

D:\django\easyuicms>python manage.py runserver  
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
September 05, 2025 - 13:51:09
Django version 4.2.4, using settings 'easyuicms.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

瀏覽下列網址就可以看到上面配置之結果網頁了 : 

# http://127.0.0.1:8000/demo/hello/Tony/




以上測試的整個專案放在 GitHub :


2025年9月3日 星期三

用 EasyUI for jQuery 與 Django 建構網站 (一)

今天回顧了之前學習 EasyUI 的筆記, 覺得過去花了好多時間學習現在都沒在用好可惜, 上網查了一下 EasyUI 官網, 發現它依然健在且與時俱進, 除了原本支援的 jQuery 外, 還支援了目前主流的三大前端框架 Vue, Angular, 與 React, 參考 :


我計畫在 Mapleboard 上使用 Django + EasyUI 重新建構我的量化投資網站 (因為多年來都只用 Python, 我的 PHP 功力已嚴重退化矣), 仍然使用我熟悉的 jQuery 作為前端框架 (實話是 : 三大主流前端框架我一個也不會). 

目前最新的 EasyUI for jQuery v1.11.3 版下載網址如下 :


解壓縮 zip 檔後根目錄下有網站必要的兩個 js 檔 : jquery.min.js 與 jquery.easyui.min.js, 主題資料夾 themes, 多語系資料夾 locale, 外掛資料夾 plug-in, 以及展示資料夾 demo 等 :




新版 EasyUI 引進了許多新元件與新功能, 教學文件與展示網站參考 :


我在 2014~2017 年間曾使用 EasyUI 來打造網站前端 (搭配 PHP 作為後端), 那時寫下一些筆記, 雖然有些用法可能過時了, 但基本用法還是相同的, 參考 : 


之前有在 Django 上使用 EasyUI, 參考 :


另外也曾在 Google App Engine 上開發與佈署以 EasyUI 為基礎的小型 CMS 系統, 參考 :


當時能跑 Python 的網站平台不多, 我知道的大概只有 GAE 與 Heroku, 而且只能用 Python 2.7 (GAE 其實就是谷歌雲端客製化的 Django 1.x). 

前陣子完成 Mapleboard 的 serverless 建置後, 正徬徨下一步是要做甚麼. 今天複習了 EasyUI 後舊確定了, 那就是結合 EayUI 建置 Django 網站平台, 建構新版的 EasyUICMS 系統. 以前花了很多時間看教學文件後透過反覆測試學到 Know-how 後再來寫專案, 現在有 AI 幫忙, 不論是學習還是 debug 與找解決方案都能很快搞定 (難怪印度經濟的強項 IT 外包現在沒搞頭了). 

以下測試都在 LG Gram 筆電上進行, 完成後再佈署至 Mapleboard. 


1. 建立 Django 專案 : 

先檢查 Django 版本 : 

D:\django>python -m django --version   
4.2.4

建立一個名為 easyuicms 的專案 : 

D:\django>django-admin startproject easyuicms   

用 tree 檢視專案的目錄結構 : 

D:\django>tree easyuicms /f   
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\DJANGO\EASYUICMS
│  manage.py
│
└─easyuicms
        asgi.py
        settings.py
        urls.py
        wsgi.py
        __init__.py

其中 manage.py 是 Django 的管理程式, 用來管理網站組態與接收處理命令列指令. 


2. 添加模版目錄 (templates) 與靜態檔案目錄 (static) : 

在上層專案目錄 easyuicms 下用 mkdir 指令建立模版目錄 (templates) 與靜態檔案目錄 (static) 來存放網頁模板與圖檔等靜態檔案 :

D:\django>cd easyuicms  
D:\django\easyuicms>mkdir templates  
D:\django\easyuicms>mkdir static  

切換至 static 目錄, 在其下建立 images, css, 與 js 放置全站之圖檔, CSS 樣式檔, 與 Javascript 檔 :

D:\django\easyuicms>cd static   
D:\django\easyuicms\static>mkdir images   (放圖檔)
D:\django\easyuicms\static>mkdir themes   (放布景主題之 CSS 樣式檔)
D:\django\easyuicms\static>mkdir js   (放 Javascript 檔)

跳回 django 目錄下用 tree 檢視目錄結構 :

D:\django\easyuicms\static>cd .. 
D:\django\easyuicms>cd .. 
D:\django>tree easyuicms /f    
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\DJANGO\EASYUICMS
│  manage.py
│
├─easyuicms
│      asgi.py
│      settings.py
│      urls.py
│      wsgi.py
│      __init__.py
│
├─static
│  ├─themes
│  ├─images
│  └─js
└─templates


3. 啟動開發伺服器 : 

切換到上層專案目錄 easyuicms 下, 執行下列指令啟動開發伺服器 :

D:\django\easyuicms>python manage.py runserver   
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 04, 2025 - 11:52:34
Django version 4.2.4, using settings 'easyuicms.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

[04/Sep/2025 11:53:08] "GET / HTTP/1.1" 200 10664
Not Found: /favicon.ico
[04/Sep/2025 11:53:08] "GET /favicon.ico HTTP/1.1" 404 2113

上面出現 "You have 18 unapplied migration(s)" 訊息是因為尚未進行資料庫同步的緣故, 由於目前並未用到資料庫, 因此不同步也不會影響網站功能. 用瀏覽器訪問 127.0.0.1:8000 顯示 Django 預設網頁表示網站已順利運行 :




這樣便完成初步測試了. 

這時再用 tree 檢視目錄結構 :

D:\django>tree easyuicms /f   
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\DJANGO\EASYUICMS
│  db.sqlite3
│  manage.py
│
├─easyuicms
│  │  asgi.py
│  │  settings.py
│  │  urls.py
│  │  wsgi.py
│  │  __init__.py
│  │
│  └─__pycache__
│          settings.cpython-310.pyc
│          urls.cpython-310.pyc
│          wsgi.cpython-310.pyc
│          __init__.cpython-310.pyc
│
├─static
│  ├─css
│  ├─images
│  └─js
└─templates

可見第一次啟動開發伺服器時就會自動在上層專案目錄下產生一個資料庫檔 db.sqlite3 了. 

2023年12月9日 星期六

jQuery EasyUI v1.10 學習筆記 : 核取盒群組 CheckGroup

在上一篇 EasyUI 文章中我們測試了 Checkbox 元件, 本篇要來測試它的進階簡化版 CheckGroup (核取盒群組), 本系列文章索引參考 :


核取盒群組顧名思義就是將一群核取盒組成一個群組, 這是一個利用空的 div 元素當容器所包覆合成的元件, 這個 div 元素透過 class 初始化後會在裡面生成多個 type="checkbox" 的核取盒 input 元素, 功能與自行用 checkbox 手刻相同, 但使用起來更簡潔. 

基本語法如下 : 

<div class="easyui-checkgroup" name="名稱" dir="v" data-options="data: optionsData"></div>

Div 元素必須填入 easyui-checkgroup 樣式類別才能初始化為 CheckGroup 元件, name 參數是一個字串, 用來給群組內的每個 checkbox 設定相同的名稱; dir 則是設定選項的擺放方式, 預設為水平 "h". 如果要讓群組內的 checkbox 垂直排列, 可以用 dir='v' 設定 (預設是 'h' 水平排列).

選項變數 optionsData 為一個定義各選項內容的物件陣列變數, 每個選項就是一個物件, 包含必要的 label, value, 與可有可無的 disabled 屬性, 用來設定每個 checkbox 的標籤 (label) 與選項值 (value). 全部選項物件組成一個陣列 optionsData :

var optionsData=[{value: "選項1", value: "值1"}, {value: "選項2", value: "值2"}, ...]

注意, optionsData 必須定義於 <script> 標籤內的最上層, 不要放在等待文件載入完成的 $(function({ })) 裡面, 否則會無法載入變數值. 

其實 name 與 dir 屬性也可以放在 data-options 裡面, 語法如下 :

<input class="easyui-checkgroup" data-options="name: '名稱', data: optionsData, dir: 'v'">

CheckGroup 常用屬性如下表 : 


 CheckGroup 常用屬性 說明
 class 必須填入 easyui-checkgroup 才能初始化
 name 選項 checkbox 的共同名稱
 labelPosition 標題位置=before (預設) / after / top
 labelAlign 標題對齊方式=left (預設) / right
 labelWidth 標題寬度 (px)
 value 設定每個選項的 value 的陣列
 data 設定每個選項的物件陣列, 可用屬性 : label, value, disabled
 itemStyle 選項 checkbox 的樣式 (物件), 例如 {height: 30}
 dir 設定選項的布局方向 (字串), 可用 'v' (垂直) 或 'h' (水平, 預設)


注意, data, value 與 itemStyle 屬性為 Javascript 之陣列或物件字面值, 必須在 data-options 屬性中設定, 無法放在 div 元素的 data 與 value 屬性中. 其次, CheckGroup 的選項參數 data 中沒有 checked 屬性, 所以無法設定預設已勾選之選項. 



預設的 CheckGroup 範例如下 :



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div class="easyui-checkgroup" name="language" data-options="data: optionsData" style="margin-bottom:10px"></div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      var optionsData=[{value:'Python', label:'Python'},
                       {value:'Java', label:'Java'},
                       {value:'C#', label:'C#'}];
      $(function(){
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

結果如下 : 



可見預設選項是水平排列的. 添加 dir="v" 屬性可使選項垂直排列 :

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div class="easyui-checkgroup" name="language" dir="v" data-options="data: optionsData" style="margin-bottom:10px"></div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      var optionsData=[{value:'Python', label:'Python'},
                       {value:'Java', label:'Java'},
                       {value:'C#', label:'C#'}];
      $(function(){
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

此例只是在上面範例的核取盒群組 div 元素中添加 dir="v", 結果如下 :




name 與 dir 屬性均可放在 data-options 屬性中, 但要注意單雙引號需交替使用, 例如 :



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div class="easyui-checkgroup" data-options="data: optionsData, name:'language', dir:'v'" style="margin-bottom:10px"></div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      var optionsData=[{value:'Python', label:'Python'},
                       {value:'Java', label:'Java'},
                       {value:'C#', label:'C#'}];
      $(function(){
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

此例是將上例的 name 與 dir 都移到 data-options 內, 結果與上例相同. 

除了用 easyui-checkgroup 樣式類別來初始化 CheckGroup 外, 也可以使用 div 元素的 jQuery 物件的 checkgroup() 方法, 在下面範例中, 我們將 div 元素的 class 屬性拿掉, 但添加有 id 屬性以便能取得其 jQuery 物件, 例如 : 



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div id="chkgroup1" style="margin-bottom:10px"></div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      $(function(){
        $("#chkgroup1").checkgroup({
          name: "language",
          data: [{value:'Python', label:'Python'},
                 {value:'Java', label:'Java'},
                 {value:'C#', label:'C#'}],
          dir: "v"
          });
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

此例將 optionsData 等屬性放在字典中傳入 checkgroup() 來初始化 CheckGroup 元件, 結果與上面相同. 

下面是添加 labelPosition: "before" 屬性的範例 : 



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div id="chkgroup1" style="margin-bottom:10px"></div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      $(function(){
        $("#chkgroup1").checkgroup({
          name: "language",
          data: [{value:'Python', label:'Python'},
                 {value:'Java', label:'Java'},
                 {value:'C#', label:'C#'}],
          dir: "v",
          labelPosition: "before"
          });
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

結果如下 : 




可見選項的標籤已變成在前面 (預設是後面). 

雖然 CheckGroup 比用 CheckBox 手刻要簡潔, 但它不能使用 checked 屬性來指定那些選項預設已勾選, dataOptions 只能用 label, value, 與 disable 這 3 個屬性. 下面是將 C# 這個選項設為 disabled 使其不可選的範例 :



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div id="chkgroup1" style="margin-bottom:10px"></div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      $(function(){
        $("#chkgroup1").checkgroup({
          name: "language",
          data: [{value:'Python', label:'Python'},
                 {value:'Java', label:'Java'},
                 {value:'C#', label:'C#', disabled: true}],
          dir: "v"
          });
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

結果如下 :




可見 C# 這一項顏色變淡無法選取.

2023年12月8日 星期五

jQuery EasyUI v1.10 學習筆記 : 核取盒 CheckBox

本篇記錄 jQuery EasyUI 的核取盒 CheckBox 測試, 本系列文章索引參考 :


核取盒其實就是 HTML 中 type="checkbox" 的 input 元素, 其功能是讓使用者能勾選多個選項, HTML5 的核取盒外觀較原始, EasyUI 則透過 CSS 加以美化並與其他元件介面一致. 

CheckBox 基本語法如下 : 

<input class="easyui-checkbox" name="名稱1" label="選項1" value="值1" ....>

Input 元素必須填入 easyui-checkbox 才能初始化為 CheckBox 元件, 它有許多可設定的參數, 可以分別放在 input 屬性中, 除了 name 以外也可以都放在 HTML5 的 data-options 屬性中 (中間用逗號隔開) :

<input class="easyui-checkbox" name="名稱1" data-options="label='選項1', value='值1', ....">

常用屬性如下表 : 


 CheckBox 常用屬性 說明
 class 必須填入 easyui-checkbox 才能初始化
 label 文字輸入欄位的標題
 labelPosition 標題位置=before (預設) / after / top
 labelAlign 標題對齊方式=left (預設) / right
 labelWidth 標題寬度 (px)
 value 選項所綁定之值
 checked 是否被勾選=true / false (預設)
 width 寬度 (px)
 height 高度 (px)
 disabled 是否可使用=true (預設) / false



下面是原始的 HTML5 核取盒外觀 :



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div style="margin-bottom:10px">
      <label>Python</label>
      <input type="checkbox" name="language" value="Python">
    </div>
    <div style="margin-bottom:10px">
      <label>Java</label>
      <input type="checkbox" name="language" value="Java">
    </div>
    <div style="margin-bottom:10px">
      <label>C#</label>
      <input type="checkbox" name="language" value="C#">
    </div>
        <div style="margin-bottom:10px">
        <button id="ok">確定</button>
    </div>
    <script>
      $(function(){
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

此例使用原始的 HTML5 核取盒, 所以需要 label 元素實現選項的標題, 當按下 "確定" 鈕透過 jQuery 處理 click 事件抓取所有被勾選的選項, 然後用 forEach 迴圈將其 value 丟到陣列裡, 最後顯示陣列組合結果. 關於 jQuery 選擇器用法參考 :


結果如下 :




用 EasyUI 改寫上面範例如下 : 



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" name="language" label="Python" value="Python">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" name="language" label="Java" value="Java">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" name="language" label="C#" value="C#">
    </div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      $(function(){
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

此例僅將核取盒與按鈕改為 EasyUI 版, 按鈕事件處理程式不變, 結果如下 :




可見外觀比上面的原始 HTML5 版較好看多了. 

下面範例則是將 easyui-checkbox 改用 data-options 屬性來設定 : 



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" name="language" data-options="label:'Python', value:'Python', labelPosition:'after'">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" name="language" data-options="label:'Java', value:'Java', labelPosition:'after'">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" name="language" data-options="label:'C#', value:'C#',  labelPosition:'after'">
    </div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      $(function(){
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

此例將核取盒設定放在 data-options 屬性中, 添加 labelPosition 屬性將標題改為放在核取盒後面, 結果如下 :




除了用 data-options 屬性來初始化核取盒外, 也可以使用 EasyUI 的 jQuery 函式 checkbox(), 例如 :



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <h2>擅長的程式語言</h2>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" id="cbx1" name="language">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" id="cbx2" name="language">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-checkbox" id="cbx3" name="language">
    </div>
    <div style="margin-bottom:10px">
      <a href="#" id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">確定</a>
    </div>
    <script>
      $(function(){
        $('#cbx1').checkbox({
          label: 'Python',
          value: 'Python',
          labelPosition: 'after'
          });
        $('#cbx2').checkbox({
          label: 'Java',
          value: 'Java',
          labelPosition: 'after'
          });
        $('#cbx3').checkbox({
          label: 'C#',
          value: 'C#',
          labelPosition: 'after'
          });
        $("#ok").on("click", function(){
          selected=[];
          $("input[name=language]:checked").each(function(){
            selected.push($(this).val());        
            });
          alert(selected.join());
          });
        });
    </script>
  </body>
</html>

此例把 input 元素中的 data-options 拿掉, 但添加 id 屬性以便能取得其 jQuery 物件, 然後其 checkbox() 函式來初始化, 結果與上面一樣. 

2023年12月4日 星期一

jQuery EasyUI v1.10 學習筆記 : 文字盒 TextBox

本篇記錄 jQuery EasyUI 的文字盒 TextBox 測試, 本系列文章索引參考 :


文字盒 TextBox 與標籤盒 TagBox 一樣都是 type=text 的 Input 元素, 主要差別是 TagBox 套用了 easyui-tagbox 樣式類別, 而 TextBox 則是套用了 easyui-textbox 樣式類別, 且可用的屬性不同. 文字盒主要是放在表單內讓使用者輸入單行文字或多行文字 (即兼具 textarea 功能). 

TextBox 基本語法如下 : 

<input class="easyui-textbox" label="欄位1" ....>

Input 元素必須填入 easyui-textbox 才能初始化為 TagBox 元件, 它有許多可設定的參數, 可以分別放在 input 屬性中, 也可以全部都放在 HTML5 的 data-options 屬性中 (中間用逗號隔開) :

<input class="easyui-textbox" data-options="label='欄位1', prompt='請輸入', ....">

常用屬性如下表 : 


 TextBox 常用屬性 說明
 class 必須填入 easyui-textbox 才能初始化
 label 文字輸入欄位的標題
 prompt 欄位內的提示語
 labelPosition 標題位置=before (預設) / after / top
 labelAlign 標題對齊方式=left (預設)/right
 labelWidth 標題寬度 (px)
 value 預設值
 type 欄位類型, text(預設) / password (密碼欄位)
 width 寬度 (px)
 height 高度 (px)
 multiline 是否為多行文字欄位=true / false (預設)
 iconClass 圖示樣式類別
 iconAlign 圖示對齊方式 : left / right (預設)
 iconWidth 圖示寬度 (px)



例如下面只有 class 與 label 屬性的測試範例 :


<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <div style="margin-bottom:10px">
      <input class="easyui-textbox" label="姓名">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-textbox" label="Email">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-textbox" label="密碼" type="password">
    </div>
    <div style="margin-bottom:10px">
      <a href="#" class="easyui-linkbutton">登入</a>
    </div>
    <script>
      $(function(){
        });
    </script>
  </body>
</html>

結果如下 : 




可見預設的文字盒寬度都很短, 可以在 data-options 屬性中以 width 與 prompt 調整, 同時把 label 也放進 data-options 屬性中 :


測試 2 : 設定 label, type, width 與 prompt 等屬性 [看原始碼]

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <div style="margin-bottom:10px">
      <input class="easyui-textbox" data-options="width:300, prompt:'請輸入姓名',  label:'姓名'">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-textbox" data-options="width:300, prompt:'請輸入 Email 信箱', label:'Email'">
    </div>
    <div style="margin-bottom:10px">
      <input class="easyui-textbox" type="password" data-options="width:300, prompt:'請輸入密碼', label:'密碼'">
    </div>
    <div style="margin-bottom:10px">
      <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">登入</a>
    </div>
    <script>
      $(function(){
        });
    </script>
  </body>
</html>
 
此例前 3 個欄位都在 data-options 屬性中設定了 label, width 與 prompt, 密碼欄位; 按鈕則設定一個 iconCls=ok 的圖示, 關於 icon 圖示用法參考 :


結果如下 :




輸入密碼欄位時會以黑點代替 :




除了使用 class="easyui-textbox" 樣式類別來初始化外, 也可以用 jQuery 物件的 textbox() 方法, 但每一個 input 文字欄位元素必須添加 id 屬性以便能建立 jQuery 物件, 語法如下 :

$("#input 元素 id").textbox();

上面範例可以改寫為 : 


<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <div style="margin-bottom:10px">
      <input id="tb1">
    </div>
    <div style="margin-bottom:10px">
      <input id="tb2">
    </div>
    <div style="margin-bottom:10px">
      <input id="tb3">
    </div>
    <div style="margin-bottom:10px">
      <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">登入</a>
    </div>
    <script>
      $(function(){
        $("#tb1").textbox({
          width:300, 
          prompt:'請輸入姓名',
          label:'姓名'
          });
        $("#tb2").textbox({
          width:300, 
          prompt:'請輸入 Email 信箱', 
          label:'Email'
          });
        $("#tb3").textbox({
          width:300, 
          prompt:'請輸入密碼', 
          label:'密碼', 
          type:'password'
          });
        });
    </script>
  </body>
</html>

此例移除了 input 上的 class 與 data-options 屬性, 由其 jQuery 物件於呼叫 textbox() 方法時
初始化 Texbox 元件, 結果與上面範例一樣. 


標籤的位置可以用 labelPosition 屬性設定為 'top', 這樣標籤與輸入欄位就會從水平變成垂直了, 也可以用 value 屬性填入預設值, 例如 : 



<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="themes/default/easyui.css">
    <link rel="stylesheet" href="themes/icon.css">
    <script src="jquery.min.js"></script>
    <script src="jquery.easyui.min.js"></script>
    <script src="locale/easyui-lang-zh_TW.js"></script>
  </head>
  <body>
    <div style="margin-bottom:10px">
      <input id="tb1">
    </div>
    <div style="margin-bottom:10px">
      <input id="tb2">
    </div>
    <div style="margin-bottom:10px">
      <input id="tb3">
    </div>
    <div style="margin-bottom:10px">
      <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">登入</a>
    </div>
    <script>
      $(function(){
        $("#tb1").textbox({
          width:300, 
          prompt:'請輸入姓名',
          label:'姓名',
          labelPosition:'top'
          });
        $("#tb2").textbox({
          width:300, 
          prompt:'請輸入 Email 信箱', 
          label:'Email',
          labelPosition:'top',   
          value:'@'   
          });
        $("#tb3").textbox({
          width:300, 
          prompt:'請輸入密碼', 
          label:'密碼', 
          type:'password',
          labelPosition:'top'    
          });
        });
    </script>
  </body>
</html>

結果如下 :




可見如果用 value 設定預設值後, prompt 就失去作用了.