Easyui the display and additions and deletions of datetime fields in the DataGrid

Source: Internet
Author: User

Exception display for datetime fields in the DataGrid:

If you have used the Easyui datagrid, you should know that if the fields in the database are of the DateTime type, the bindings will not display properly when the DataGrid is explicitly displayed, and you generally need to use formatter to format the time format

  {title:  "  activity start time  , Field:  '  

The specific implementation of Common.datetimeformatter is as follows:

varCommon = {    //Easyui formatted with a DataGrid by dateTimeformatter:function(value, REC, index) {if(Value = =undefined) {            return""; }        /*JSON format time to JS time format*/value= Value.substr (1, value.length-2); varobj = eval (' (' + ' {date:new ' + value + '} ' + ') '); varDateValue = obj["Date"]; if(Datevalue.getfullyear () < 1900) {            return""; }        varval = Datevalue.pattern ("Yyyy-mm-dd hh:mm"); returnVal; }, DateTimeFormatter:function(value, REC, index) {if(Value = =NULL|| Value = = ") {            return‘‘; }        varDT; if(ValueinstanceofDate) {DT=value; }        Else{DT=NewDate (value); if(IsNaN (dt)) {value= Value.replace (/\/date\ ((-?\d+) \) \//, ' $ ');//The Red section is the key code that converts the date value of the long string to the normal JS date formatDT =NewDate ();            Dt.settime (value); }        }        returnDt.pattern ("Yyyy-mm-dd hh:mm"); },    //Easyui formatted with a DataGrid by dateDateformatter:function(value, REC, index) {if(Value = =undefined) {            return""; }        /*JSON format time to JS time format*/value= Value.substr (1, value.length-2); varobj = eval (' (' + ' {date:new ' + value + '} ' + ') '); varDateValue = obj["Date"]; if(Datevalue.getfullyear () < 1900) {            return""; }        returnDatevalue.pattern ("Yyyy-mm-dd"); }};
View Code
Date.prototype.pattern =function(FMT) {varo = {        "m+": This. GetMonth () + 1,//Month"D+": This. GetDate (),//Day"H +": This. GetHours ()% 12 = = 0? 12: This. GetHours ()% 12,//hours"H +": This. GetHours (),//hours"m+": This. getminutes (),//points"S+": This. getseconds (),//seconds"q+": Math.floor (( This. GetMonth () + 3)/3),//Quarterly"S": This. Getmilliseconds ()//milliseconds    }; varWeek = {        "0": "/u65e5",        "1": "/u4e00",        "2": "/u4e8c",        "3": "/u4e09",        "4": "/u56db",        "5": "/u4e94",        "6": "/U516D"    }; if(/(y+)/. Test (FMT)) {FMT= Fmt.replace (Regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length)); }    if(/(e+)/. Test (FMT)) {FMT= Fmt.replace (regexp.$1, (Regexp.$1.length > 1)? (Regexp.$1.length > 2?) "/u661f/u671f": "/u5468"): "") + week[ This. GetDay () + ""]); }     for(varKincho) {if(NewRegExp ("(" + K + ")"). Test (FMT)) {FMT= Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr ("" +o[k])); }    }    returnFMT;}
View Code

Looking at so many JS is still very cumbersome, I use a small trick here, is to convert the datetime format in the database field type to the string format value unchanged, but eliminates the front-end formatting process

{title: ' Activity Start time ', field: ' Begintimestr ', width:300, editor: {type: ' Datetimebox ', options: {Required:true}}}

Conversions of specific datetime types can be implemented at the database level or C # code level; I'm here to convert at the database level, and then directly specify the string type of begintimestr as the bound field

SELECT id,activityitemid,created,convert (varchar, EndTime, a) endtimestr,convert (varchar, BeginTime, 20) BEGINTIMESTRFROM dbo. Activityitemidinfo
DataGrid Rowedit:

First step: Specify editor

Columns: [[                {                    title: ' Link id ', field: ' Activityitemid ', width:200, editor: {                        type: ' Numberbox ', options: {requ Ired:true}}}                ,                {title: ' Activity Start time ', field: ' Begintimestr ', width:300, editor: {type: ' Datetimebox ', options : {required:true}},                {                    title: ' Active End Time ', field: ' Endtimestr ', width:300, editor: {type: ' Datetimebox ', optio NS: {required:true}}            ]]

The second step defines the appropriate action method:

varEditindex =undefined;functionendediting () {if(Editindex = = undefined) {return true }    if($ (' #ActivityItemIDInfolist '). DataGrid (' Validaterow '), Editindex)) {        vared = $ (' #ActivityItemIDInfolist '). DataGrid (' Geteditor ', {index:editindex, Field: ' Activityitemid ') }); $(' #ActivityItemIDInfolist '). DataGrid (' EndEdit ', Editindex); Editindex=undefined; return true; } Else {        return false; }}functionOnclickrow (index) {if(Editindex! =index) {        if(Endediting ()) {$ (' #ActivityItemIDInfolist '). DataGrid (' SelectRow ', Index). DataGrid (' BeginEdit ', index); Editindex=index; } Else {            $(' #ActivityItemIDInfolist '). DataGrid (' SelectRow ', Editindex); }    }}functionappend () {if(Endediting ()) {$ (' #ActivityItemIDInfolist '). DataGrid (' Appendrow ', {activityitemid: ' 0 ' }); Editindex= $ (' #ActivityItemIDInfolist '). DataGrid (' GetRows '). length-1; $(' #ActivityItemIDInfolist '). DataGrid (' SelectRow ', Editindex). DataGrid (' BeginEdit ', Editindex); }}functionRemoveit () {if(Editindex = = undefined) {return; } $(' #ActivityItemIDInfolist '). DataGrid (' CancelEdit ', Editindex). DataGrid (' DeleteRow ', Editindex); Editindex=undefined;}functionAccept () {if(Endediting ()) {$ (' #ActivityItemIDInfolist '). DataGrid (' AcceptChanges '); }}functionreject () {$ (' #ActivityItemIDInfolist '). DataGrid (' RejectChanges '); Editindex=undefined;}functiongetChanges () {varrows = $ (' #ActivityItemIDInfolist '). DataGrid (' GetChanges '); Alert (Rows.length+ ' rows are changed! ');}
View Code

Step three: Specify the time to edit the row index, where the timing and row click events are associated

Fourth step: Detect changes in the DataGrid and submit changes to the background processing via Ajax

functionSavedatatoserver () {endediting (); varrows = $ (' #ActivityItemIDInfolist '). DataGrid (' GetChanges '); if(Rows.length > 0) {        varinserted = $ (' #ActivityItemIDInfolist '). DataGrid (' GetChanges ', "inserted"); vardeleted = $ (' #ActivityItemIDInfolist '). DataGrid (' GetChanges ', "deleted"); varUpdated = $ (' #ActivityItemIDInfolist '). DataGrid (' GetChanges ', "updated"); varEffectrow =NewObject (); if(inserted.length) {effectrow["Inserted"] =json.stringify (inserted); }        if(deleted.length) {effectrow["deleted"] =json.stringify (deleted); }        if(updated.length) {effectrow["Updated"] =json.stringify (updated); } $.ajaxtext ('/shopactivitys/saveshopactivityitemsdata ', Effectrow,function(data) {if(data = = 0) {$.messager.alert (' Save ', ' save Success ', ' info ');            Mygrid.databind (); } Else{alert ("Save failed, please try again");    }        }); }    Else{alert ("No modifications detected");return; }}
View Code

Fifth step: Backstage to get to the front of the post to the data for warehousing and other related operations

Easyui the display and additions and deletions of datetime fields in the DataGrid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.