Easyui datagrid editor為combobox時指定數據源
當在datagrid行內部應用添加編輯操作時,引入combobox是非常方便的操作,我在引入combobox時對數據源這快做個總結,在做demo的過程中遇到個問題,就是當你選擇了下拉框的值后點擊保存,此時顯示的是value值,而不是text值,這時使用格式化函數解決此問題。
var Address = [{ "value": "1", "text": "CHINA" }, { "value": "2", "text": "USA" }, { "value": "3", "text": "Koren" }]; function unitformatter(value, rowData, rowIndex) { if (value == 0) { return; } for (var i = 0; i < Address.length; i++) { if (Address[i].value == value) { return Address[i].text; } } } function GetTable() { var editRow = undefined; $("#Student_Table").datagrid({ height: 300, width: 450, title: '學生表', collapsible: true, singleSelect: true, url: '/Home/StuList', idField: 'ID', columns: [[ { field: 'ID', title: 'ID', width: 100 }, { field: 'Name', title: '姓名', width: 100, editor: { type: 'text', options: { required: true } } }, { field: 'Age', title: '年齡', width: 100, align: 'center', editor: { type: 'text', options: { required: true } } }, { field: 'Address', title: '地址', width: 100, formatter: unitformatter, align: 'center', editor: { type: 'combobox', options: { data: Address, valueField: "value", textField: "text" } } } ]], toolbar: [{ text: '添加', iconCls: 'icon-add', handler: function () { if (editRow != undefined) { $("#Student_Table").datagrid('endEdit', editRow); } if (editRow == undefined) { $("#Student_Table").datagrid('insertRow', { index: 0, row: {} }); $("#Student_Table").datagrid('beginEdit', 0); editRow = 0; } } }, '-', { text: '保存', iconCls: 'icon-save', handler: function () { $("#Student_Table").datagrid('endEdit', editRow); //如果調用acceptChanges(),使用getChanges()則獲取不到編輯和新增的數據。 //使用JSON序列化datarow對象,發送到后台。 var rows = $("#Student_Table").datagrid('getChanges'); var rowstr = JSON.stringify(rows); $.post('/Home/Create', rowstr, function (data) { }); } }, '-', { text: '撤銷', iconCls: 'icon-redo', handler: function () { editRow = undefined; $("#Student_Table").datagrid('rejectChanges'); $("#Student_Table").datagrid('unselectAll'); } }, '-', { text: '刪除', iconCls: 'icon-remove', handler: function () { var row = $("#Student_Table").datagrid('getSelections'); } }, '-', { text: '修改', iconCls: 'icon-edit', handler: function () { var row = $("#Student_Table").datagrid('getSelected'); if (row != null) { if (editRow != undefined) { $("#Student_Table").datagrid('endEdit', editRow); } if (editRow == undefined) { var index = $("#Student_Table").datagrid('getRowIndex', row); $("#Student_Table").datagrid('beginEdit', index); editRow = index; $("#Student_Table").datagrid('unselectAll'); } } else { } } }, '-', { text: '上移', iconCls: 'icon-up', handler: function () { MoveUp(); } }, '-', { text: '下移', iconCls: 'icon-down', handler: function () { MoveDown(); } }], onAfterEdit: function (rowIndex, rowData, changes) { editRow = undefined; }, onDblClickRow: function (rowIndex, rowData) { if (editRow != undefined) { $("#Student_Table").datagrid('endEdit', editRow); } if (editRow == undefined) { $("#Student_Table").datagrid('beginEdit', rowIndex); editRow = rowIndex; } }, onClickRow: function (rowIndex, rowData) { if (editRow != undefined) { $("#Student_Table").datagrid('endEdit', editRow); } } }); }
效果圖:

自定義驗證。更多驗證規則參考:https://www.cnblogs.com/lansy/p/4627649.html
$.extend($.fn.validatebox.defaults.rules, { english: {// 驗證英語 validator: function (value) { return /^[A-Za-z]+$/i.test(value); }, message: '請輸入英文字母' } })
required: "必選字段", remote: "請修正該字段", email: "請輸入正確格式的電子郵件", url: "請輸入合法的網址", date: "請輸入合法的日期", dateISO: "請輸入合法的日期 (ISO).", number: "請輸入合法的數字", digits: "只能輸入整數", creditcard: "請輸入合法的信用卡號", equalTo: "請再次輸入相同的值", accept: "請輸入擁有合法后綴名的字符串", maxlength: jQuery.format("請輸入一個長度最多是 {0} 的字符串"), minlength: jQuery.format("請輸入一個長度最少是 {0} 的字符串"), rangelength: jQuery.format("請輸入一個長度介於 {0} 和 {1} 之間的字符串"), range: jQuery.format("請輸入一個介於 {0} 和 {1} 之間的值"), max: jQuery.format("請輸入一個最大為 {0} 的值"), min: jQuery.format("請輸入一個最小為 {0} 的值") missingMessage:未填寫時顯示的信息 validType:驗證類型見下示例 invalidMessage:無效的數據類型時顯示的信息 data-options="required:true,validType:'length[1,3]'" ;//輸入字符長度1-3位 boolen b=$('#txt_Name').validatebox("isValid");//驗證結果 注意日期格式驗證必須自己重寫,參考如下 $.extend($.fn.validatebox.defaults.rules, { idcard: {// 驗證身份證 validator: function (value) { return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value); }, message: '身份證號碼格式不正確' }, minLength: { validator: function (value, param) { return value.length >= param[0]; }, message: '請輸入至少(2)個字符.' }, length: { validator: function (value, param) { var len = $.trim(value).length; return len >= param[0] && len <= param[1]; }, message: "輸入內容長度必須介於{0}和{1}之間." }, phone: {// 驗證電話號碼 validator: function (value) { return /^((\d2,3)|(\d{3}\-))?(0\d2,3|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value); }, message: '格式不正確,請使用下面格式:020-88888888' }, mobile: {// 驗證手機號碼 validator: function (value) { return /^(13|15|18)\d{9}$/i.test(value); }, message: '手機號碼格式不正確' }, intOrFloat: {// 驗證整數或小數 validator: function (value) { return /^\d+(\.\d+)?$/i.test(value); }, message: '請輸入數字,並確保格式正確' }, currency: {// 驗證貨幣 validator: function (value) { return /^\d+(\.\d+)?$/i.test(value); }, message: '貨幣格式不正確' }, qq: {// 驗證QQ,從10000開始 validator: function (value) { return /^[1-9]\d{4,9}$/i.test(value); }, message: 'QQ號碼格式不正確' }, integer: {// 驗證整數 可正負數 validator: function (value) { //return /^[+]?[1-9]+\d*$/i.test(value); return /^([+]?[0-9])|([-]?[0-9])+\d*$/i.test(value); }, message: '請輸入整數' }, age: {// 驗證年齡 validator: function (value) { return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i.test(value); }, message: '年齡必須是0到120之間的整數' }, chinese: {// 驗證中文 validator: function (value) { return /^[\Α-\¥]+$/i.test(value); }, message: '請輸入中文' }, english: {// 驗證英語 validator: function (value) { return /^[A-Za-z]+$/i.test(value); }, message: '請輸入英文' }, unnormal: {// 驗證是否包含空格和非法字符 validator: function (value) { return /.+/i.test(value); }, message: '輸入值不能為空和包含其他非法字符' }, username: {// 驗證用戶名 validator: function (value) { return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value); }, message: '用戶名不合法(字母開頭,允許6-16字節,允許字母數字下划線)' }, faxno: {// 驗證傳真 validator: function (value) { // return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value); return /^((\d2,3)|(\d{3}\-))?(0\d2,3|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value); }, message: '傳真號碼不正確' }, zip: {// 驗證郵政編碼 validator: function (value) { return /^[1-9]\d{5}$/i.test(value); }, message: '郵政編碼格式不正確' }, ip: {// 驗證IP地址 validator: function (value) { return /d+.d+.d+.d+/i.test(value); }, message: 'IP地址格式不正確' }, name: {// 驗證姓名,可以是中文或英文 validator: function (value) { return /^[\Α-\¥]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value); }, message: '請輸入姓名' }, date: {// 驗證姓名,可以是中文或英文 validator: function (value) { //格式yyyy-MM-dd或yyyy-M-d return /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value); }, message: '清輸入合適的日期格式' }, msn: { validator: function (value) { return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value); }, message: '請輸入有效的msn賬號(例:abc@hotnail(msn/live).com)' }, same: { validator: function (value, param) { if ($("#" + param[0]).val() != "" && value != "") { return $("#" + param[0]).val() == value; } else { return true; } }, message: '兩次輸入的密碼不一致!' } }); //驗證實例 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="easyui1.2.4/jquery-1.6.min.js" type="text/javascript"></script> <script src="easyui1.2.4/jquery.easyui.min.js" type="text/javascript"></script> <!--自定義驗證--> <script src="easyui1.2.4/validator.js" type="text/javascript"></script> <link href="easyui1.2.4/themes/default/easyui.css" rel="stylesheet" type="text/css" /> <script> $(function () { //設置text須要驗證 $(""input[type=text]"").validatebox(); }) </script> </head> <body> 郵箱驗證:<input type="text" validtype="email" required="true" missingMessage="不克不及為空" invalidMessage="郵箱格局不正確" /><br /> 網址驗證:<input type="text" validtype="url" invalidMessage="url格局不正確[http://www.example.com]" /><br /> 長度驗證:<input type="text" validtype="length[8,20]" invalidMessage="有效長度8-20" /><br /> 手機驗證:<input type="text" validtype="mobile" /><br /> 郵編驗證:<input type="text" validtype="zipcode" /><br /> 賬號驗證:<input type="text" validtype="account[8,20]" /><br /> 漢子驗證:<input type="text" validtype="CHS" /><br /> 長途驗證:<input type="text" validtype="remote[""checkname.aspx"",""name""]" invalidMessage="用戶名已存在"/> </body> </html> 本身寫的validator.js //擴大easyui表單的驗證 $.extend($.fn.validatebox.defaults.rules, { //驗證漢子 CHS: { validator: function (value) { return /^[\u0391-\uFFE5]+$/.test(value); }, message: ""只能輸入漢字"" }, //移下手機號碼驗證 mobile: {//value值為文本框中的值 validator: function (value) { var reg = /^1[3|4|5|8|9]\d{9}$/; return reg.test(value); }, message: ""輸入手機號碼格局不正確."" }, //國內郵編驗證 zipcode: { validator: function (value) { var reg = /^[1-9]\d{5}$/; return reg.test(value); }, message: ""郵編必須長短0開端的6位數字."" }, //用戶賬號驗證(只能包含 _ 數字 字母) account: {//param的值為[]中值 validator: function (value, param) { if (value.length < param[0] || value.length > param[1]) { $.fn.validatebox.defaults.rules.account.message = ""用戶名長度必須在"" + param[0] + ""至"" + param[1] + ""局限""; return false; } else { if (!/^[\w]+$/.test(value)) { $.fn.validatebox.defaults.rules.account.message = ""用戶名只能數字、字母、下划線構成.""; return false; } else { return true; } } }, message: """" } }) ``````````````````````````````````` 0、調用數據驗證方法 return $("#form1").form('validate'); 示例: <asp:Button ID="btn_Save" runat="server" Text="保存" OnClick ="btn_Save_Click" OnClientClick="return $("#form1").form('validate');" /> 1、驗證是否必填 class="easyui-validatebox" missingMessage="xxx必須填寫" 2、驗證字符串長度 class="easyui-validatebox" missingMessage="xxx必須填寫少於10個字符" validType="length[0,2]" invalidMessage="不能超過2個字符!" 示例: <input class="easyui-validatebox" required="true" missingMessage="姓名必須填寫" size="10" type="text" name="ARealName"></input> 3、驗證數字必須是5.5-20之間 precision="2"表示是2為小數 class="easyui-numberbox" min="5.5" max="20" precision="2" required="true" missingMessage="必須填寫5.5~10之間的數字" 4、驗證必須是日期yyyy-MM-dd(只能選擇不可編輯) <script> $.fn.datebox.defaults.formatter = function (date) { var y = date.getFullYear(); var m = date.getMonth() + 1; var d = date.getDate(); return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d); }; $.fn.datebox.defaults.parser = function (s) { if (s) { var a = s.split('-'); var d = new Date(parseInt(a[0]), parseInt(a[1]) - 1, parseInt(a[2])); return d; } else { return new Date(); } }; </script> class="easyui-datebox" required="true" missingMessage="日期必須填寫" editable="false" //如果需要填寫其他格式的類型請自行修改formatter函數 5、驗證必須是郵件 class="easyui-validatebox" missingMessage="郵件必須填寫" validType="email" invalidMessage="請填寫正確的郵件格式" 6、頁面時間段判斷 name為s1的時間必須大於name為s2的時間 s3必須大於s2 <script> $.extend($.fn.validatebox.defaults.rules,{ TimeCheck:{ validator:function(value,param){ var s = $("input[name="+param[0]+"]").val(); //因為日期是統一格式的所以可以直接比較字符串 否則需要Date.parse(_date)轉換 return value>=s; }, message:'非法數據' } }); </script> <input name="s1" class="easyui-datebox" required="true" missingMessage="日期必須填寫" editable="false"></input> <input name="s2" class="easyui-datebox" required="true" validType="TimeCheck['s1']" invalidMessage="s1必須大於s2" editable="false"></input> <input name="s3" class="easyui-datebox" required="true" validType="TimeCheck['s2']" editable="false"></input> 7、詢問對話框提交: function Confirmbtn(msg, control) { $.messager.confirm('確認', msg, function (r) { if (r) { __doPostBack("ctl00$ContentPH_Main$Button1", ""); //alert('aa'); } }); return false; } OnClientClick="return Confirmbtn('確認嗎?', this);
