BootStrap 智能表單系列 六 表單數據綁定(編輯頁面的數據綁定)

本章介紹如何在生成表單後,將一個model的數據展現到form表單中(通常用於編輯頁面)css

代碼以下(鏈接地址:https://github.com/xiexingen/Bootstrap-SmartForm/blob/master/demo/form4-initData.html):html

<!DOCTYPE html>
<html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>數據綁定</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="../css/bootstrap.css">
        <!--自定義站點樣式-->
        <link rel="stylesheet" href="../css/site.css">
        <script src="../lib/jquery.js"></script>
        <script src="../lib/bootstrap.js"></script>
        <!--工具方法-->
        <script src="../scripts/global.js"></script>
        <!--插件-->
        <script src="../scripts/plugin.js"></script>
    </head>
    
    <body>
        <div class="panel panel-default">
                <div class="panel-heading">
                    <label>數據綁定</label>
                    <div class="pull-right">
                        <button id="btnSubmit" class="btn btn-primary btn-xs">提交表單</button>
                    </div>
                </div>
                
            <div class="panel-body">
                <form action="#" id="formContainer" class="form form-horizontal"></form>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading"><label>介紹</label></div>
            <div class="panel-body">
                <h3 class="lead">表單數據綁定</h3>
                    <blockquote>
                        <p>將json格式的model綁定到表單中,此處使用模擬的model數據,實際環境中應該是與服務器交互取到數據,在配置對象的是須要一個回調方法,在回到方法裏面能夠作表單的一些其餘操做,如添加表單驗證、添加日期插件的支持====</p>
                        <p>note:複選框的數據源爲數組形式</p>
                    </blockquote>
            </div>
        </div>
        <script>
            $(function () {
                var eles=[
                    [
                      {label:{text:'自定義用戶名:'},ele:{type:'text',name:'UserName',title:'用戶名:',required:true}},
                      {ele:{type:'radio',name:'sex',title:'性別:',items:[{text:'男',value:1},{text:'女',value:2}]}},
                      {ele:{type:'checkbox', name:'plant',title:'使用平臺:',items:[{text:'APP',value:'app'},{text:'web',value:'web'}]}}  
                    ],
                    [
                           {ele:{type:'select',name:'province',title:'省份:',withNull:true,items:[{text:'廣東',value:'GD'},{text:'湖南',value:'HN'}]}},
                           {ele:{pre:{text:'<input type="radio">'},type:'text',name:'displayName',title:'顯示名稱:'}},
                           {ele:{type:'search',title:'產品',id:'ProductName'}}
                    ],
                    [
                        {ele:{type:'datetime',name:'FromeDate',title:'有效期:'}},
                        {ele:{type:'datetime',name:'ToDate',title:'~'}},
                    ]
                ];
                //隱藏表單元素主要用於編輯時候後臺能夠區別開來
                var hides = [{ id: 'primaryKey' }];
                var bsForm = new BSForm({ eles: eles, hides: hides, autoLayout: '1,3' }).Render('formContainer',function(bf){
                    var model={primaryKey:1,UserName:'xxg',sex:1,plant:['app','web'],province:'GD',displayName:'TEST',ProductName:'筆記本',FromeDate:'2015-06-10',ToDate:'2015-08-08'}; bf.InitFormData(model);
                });
                
                $("#btnSubmit").bind('click',function () {
                    var postData=bsForm.GetFormData();
                    alert("獲取到的表達數據爲:"+JSON.stringify(postData));
                })
            });
        </script>
    </body></html>

此處使用js建立了一個json類型的model,實際開發狀況下 會跟服務器交互獲得一個model,經過表單插件的InitFormData方法將model顯示到form表單中jquery

效果圖以下:git

定義的數據成功顯示到表單中,github

note:針對複選框多選的狀況下,須要返回的是一個數組web

 

上一章:BootStrap 智能表單系列 五  表單依賴插件處理json

下一章:BootStrap 智能表單系列 七 驗證的支持bootstrap

本系列首頁:BootStrap 智能表單系列 首頁 (已完結)數組

相關文章
相關標籤/搜索