วันอังคารที่ 8 มิถุนายน พ.ศ. 2553

การใช้ form widget ใน OFBiz - การสร้าง find form widget

ใน OFBiz สามารถนำ form แสดงผลใน screen ได้ โดย form ที่นำมาแสดง สามารถแบ่งประเภทได้ดังนี้
1. find form เป็นฟอร์มที่ใช้ค้นหา item หรือ content ต่างๆ ใน module
โดย form ชนิดนี้จะมี type เป็น single


2. list form เป็นฟอร์มที่ใช้แสดงรายการ โดยจะลิสต์แสดงรายการออกมาเป็น table
form ชนิดนี้จะมี type เป็น list


3. display and edit เป็นฟอร์มที่ใช้แสดงรายการแก้ไข โดยจะสามารถแก้ไขข้อความใน field ที่อนุญาตให้แก้ไขได้ หากต้องการให้ field ไหนแก้ไขได้ สามารถ เซ็ต type เป็น input หรือ textbox หรือแล้วแต่ชนิดของ field ที่ต้องการแก้ไข แต่หากไม่ต้องการให้สามารถแก้ไขได้ ให้เซ็ต type เป็น display จะเป็นการสั่งให้แสดงผลเท่านั้น แต่ไม่สามารถแก้ไขได้



ขั้นตอนการสร้าง find form widget สามารถทำได้ดังนี้

1. ทำการ define form เข้าไปในไฟล์ *Form.xml ดังนี้
หมายเหตุ *Form.xml คือชื่อของ Component ต่อด้วย Form.xml จะอยู่ในโฟลเดอร์ widget

<form default-entity-name="Example" name="FindExample" target="example" type="single">
<auto-fields-entity default-field-type="find" entity-name="Example">
        <field name="noConditionFind"><hidden value="Y"></hidden>
        <field name="searchButton" title="Search"><submit button-type="button"></submit>
    </field></field></auto-fields-entity></form>

อธิบายโค้ด
form name="FindExample" ตั้งชื่อฟอร์มว่า  FindExample
type="single" target="example" ให้เป็นชนิด single และ route ไปที่ example controller
default-entity-name="Example" ชื่อ entity ที่เรียกใช้ข้อมูลคือ Example

auto-fields-entity entity-name="Example" ทำการเรียกข้อมูลใน entity ชื่อ Example มาแสดงโดยอัติโนมัติทุก field โดยที่ไม่ต้องระบุชื่อฟิลด์
default-field-type="find" ระบุชนิดของ field ใน form นี้ให้เป็น find

field name="searchButton" title="Search"
submit button-type="button"เพิ่มปุ่ม Search ให้ฟอร์มนี้

2. ทำการเรียก form ที่ define ไว้ใน *Form.xml มาแสดงผลใน *Screen.xml
<screen name="findExample">
        <section>
            <widgets>
                <decorator-screen location="component://culinary/widget/CommonScreens.xml" name="CommonDecorator">
                    <decorator-section name="body">
                        <container id="content">
                             <container>
                                   <container>
                                        <include-form location="component://culinary/widget/CulinaryForms.xml" name="FindExample">
                                   </include-form>
                             </container>
                        </container>
                    </container>
                </decorator-section>
            </decorator-screen>
        </widgets>
    </section></screen>
อธิบายโค้ด
include-form location="component://culinary/widget/CulinaryForms.xml" name="FindExample" ทำการ include form ชื่อ FindExample ไปแสดงผลที่ screen widget ที่ชื่อ findExample

3. ทำการกำหนด request-map ให้กับไฟล์ controller.xml

<request-map uri="example">
        <security auth="true" https="false">
        <response name="success" type="view" value="example"></response>
    </security>
</request-map>

4.สร้าง view ให้กับ example controller

<view-map name="example" page="component://culinary/widget/CulinaryScreens.xml#findExample" type="screen"></view-map>

5. ทดลองเรียกใช้ url http://localhost:8080/culinary/control/example จะปรากฏ find form ดังนี้

ซึ่งฟอร์มนี้จะแสดงเฉพาะฟอร์มการค้นหา ไม่สามารถแสดงผลการค้นหาได้ เนื่องจากยังไม่กำหนด actions ให้กับ screen
ในบทความต่อไปจะแสดงผลลัพธ์จากการค้นหาที่ต่อเนื่องจากการสร้างฟอร์มในบทความนี้

ไม่มีความคิดเห็น:

แสดงความคิดเห็น