首页 > 代码库 > TableEdit.cs

TableEdit.cs

using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace CMSC.Class.CsSubs
{
    public class TableEdit
    {
        //定义私有变量
        public List<TableEditColumn> Columns = new List<TableEditColumn>();
        public ArrayList TextFields = new ArrayList();
        public StringBuilder AddNewRowScript = new StringBuilder();
        public StringBuilder CheckRowScript = new StringBuilder();

        #region 定义属性

        private bool _isshowsumrow = true;//是否显示合计行
        public bool IsShowSumRow { set { _isshowsumrow = value; } get { return _isshowsumrow; } }

        #endregion

        #region 重写方法

        public StringBuilder BoxHtml()
        {
            StringBuilder html = new StringBuilder();
            AddNewRowScript.Append(@"var CMSCTableEdit = new CMSC.TableEdit(""TableEditView"");");

            html.Append(@"<div id=""ScrollDiv"" class=""scrolldiv"">");
            html.Append(@"<table id=""TableEditView"" class=""tableedit"" border=""0"" cellspacing=""0"" cellpadding=""0"" align=""left"">");

            html.Append(@"<tr class=""header"" height=""26"">");
            html.Append(@"<td class=""headerid lockcell"" width=""20"">&nbsp;</td>");
            html.Append(@"<td class=""center lockcell"" width=""30"">");
            html.Append(@"<input type=""hidden"" id=""SelectRows"" name=""SelectRows"" value=""""/>");
            html.Append(@"<input type=""checkbox"" id=""SelectRowAll"" name=""SelectRowAll"" onclick=""javascript:CMSCTableEdit.selectAllRows(event);"" value="""">");
            html.Append(@"</td>");
            html.Append(@"<td class=""center lockcell"" width=""40"">No</td>");
            foreach (TableEditColumn col in Columns)
            {
                html.Append(@"<td class=""center");
                if (col.IsLock) html.Append(@" lockcell");
                html.Append(@""" width=""" + col.Width + @""">" + col.Title + @"</td>");
            }
            html.Append(@"</tr>");

            StringBuilder js = new StringBuilder();
            js.Append(@"function addNewRow(rowno,rows){");
            for (int row = 1; row <= 5; row++)
            {
                html.Append(@"<tr class=""row rowout"" height=""24"">");
                html.Append(@"<td class=""rowid lockcell"" id=""Row_" + row + @""">&nbsp;</td>");

                StringBuilder selhtml = new StringBuilder();
                selhtml.Append(@"<input type=""checkbox"" name=""SelectRow"" value=""[[ROW]]""/>");
                selhtml.Append(@"<input type=""hidden"" id=""RowCheck_[[ROW]]"" name=""RowCheck_[[ROW]]"" value=""-1""/>");
                selhtml.Append(@"<input type=""hidden"" id=""RowKey_[[ROW]]"" name=""RowKey_[[ROW]]"" value=""""/>");
                foreach (string[] textfield in TextFields)
                {
                    selhtml.Append(@"<input type=""hidden"" ");
                    selhtml.Append(@"id=""" + textfield[0] + @"_[[ROW]]"" ");
                    selhtml.Append(@"name=""" + textfield[0] + @"_[[ROW]]"" ");
                    selhtml.Append(@"value=""" + textfield[1] + @"""/>");
                }
                html.Append(@"<td class=""center lockcell readonly"">");

                StringBuilder selhtml2 = new StringBuilder();
                selhtml2.Append(selhtml);
                html.Append(selhtml2.Replace("[[ROW]]", row.ToString().Trim()));

                html.Append(@"</td>");
                html.Append(@"<td class=""center lockcell readonly"" style=""padding-top:6px;"" valign=""top"">" + row + @"</td>");

                if (row == 1)
                {
                    js.Append(@"var NewRow = CMSCTableEdit.Table.insertRow(CMSCTableEdit.Table.rows.length-1);");
                    js.Append(@"NewRow.height = 24;");
                    js.Append(@"NewRow.className = ""row rowout"";");
                    js.Append(@"var NewCell0 = NewRow.insertCell(-1);");
                    js.Append(@"NewCell0.id = ""Row_""+rows;");
                    js.Append(@"NewCell0.className = ""rowid lockcell"";");
                    js.Append(@"NewCell0.innerHTML = ""&nbsp;"";");
                    js.Append(@"var NewCell1 = NewRow.insertCell(-1);");
                    js.Append(@"NewCell1.className = ""center lockcell readonly"";");
                    js.Append(@"NewCell1.innerHTML = ‘" + selhtml.Replace("[[ROW]]", "‘+rows+‘") + @"‘;");
                    js.Append(@"var NewCell2 = NewRow.insertCell(-1);");
                    js.Append(@"NewCell2.className = ""center lockcell readonly"";");
                    js.Append(@"NewCell2.innerHTML = rowno;");
                }

                int cell = 3;
                foreach (TableEditColumn col in Columns)
                {
                    StringBuilder tdhtml = new StringBuilder();

                    string classStr = "";
                    if (!col.IsEdit) classStr += "readonly";
                    if (col.IsLock) classStr += " lockcell ";
                    html.Append(@"<td valign=""top"" class=""inputcell " + classStr.Trim() + @""">");

                    tdhtml.Append(@"<input type=""text"" style=""width:80%;position: relative;""");
                    tdhtml.Append(@"id=""" + col.Id + @"_[[ROW]]"" name=""" + col.Id + @"_[[ROW]]"" ");
                    tdhtml.Append(@"onkeyup=""javascript:CMSCTableEdit.keyMove(event);"" ");

                    if (col.IsEdit)
                    {
                        tdhtml.Append(@"class=""tinput"" ");
                        tdhtml.Append(@"onfocus=""javascript:CMSCTableEdit.focusCell(event,1);"" ");
                    }
                    else
                    {
                        tdhtml.Append(@"class=""rinput"" ");
                        tdhtml.Append(@"readonly=""true"" ");
                        tdhtml.Append(@"onfocus=""javascript:CMSCTableEdit.focusCell(event,0);"" ");
                    }

                    tdhtml.Append(@"onchange=""javascript:");
                    if (col.DataType == "number") tdhtml.Append(@"CMSCTableEdit.checkNumber(this);");
                    if (col.DataType == "float1") tdhtml.Append(@"CMSCTableEdit.checkFloat1(this);");
                    if (col.DataType == "float2") tdhtml.Append(@"CMSCTableEdit.checkFloat2(this);");
                    if (col.DataType == "float3") tdhtml.Append(@"CMSCTableEdit.checkFloat3(this);");
                    if (col.DataType == "float4") tdhtml.Append(@"CMSCTableEdit.checkFloat4(this);");
                    if (col.DataType == "float5") tdhtml.Append(@"CMSCTableEdit.checkFloat5(this);");
                    if (col.DataType == "float6") tdhtml.Append(@"CMSCTableEdit.checkFloat6(this);");
                    if (col.DataType == "float7") tdhtml.Append(@"CMSCTableEdit.checkFloat7(this);");
                    if (col.DataType == "float8") tdhtml.Append(@"CMSCTableEdit.checkFloat8(this);");
                    if (col.IsSumRow) tdhtml.Append(@"CMSCTableEdit.sumEventRow(event);");
                    tdhtml.Append(@"CMSCTableEdit.checkEditRow([[ROW]]);"" ");

                    string icobtn = col.ActionIco.Trim();
                    string icoscript = col.ActionScript.Trim();
                    if (icobtn != "" && icoscript != "") tdhtml.Append(@"ondblclick=""javascript:" + icoscript + @";"" ");
                    tdhtml.Append(@"style=""text-align:" + col.Align + @";"" ");
                    tdhtml.Append(@"value=""" + col.Value + @""" />");
                    tdhtml.Append(@"<div id=""Ico_" + col.Id + @"_[[ROW]]"" class=""cellico"" style=""position: absolute;width:20px"">");

                    if (icobtn != "" && icoscript != "")
                    {
                        tdhtml.Append(@"<div style=""top:3px;"" class=""fr " + icobtn + @""" onclick=""");
                        tdhtml.Append(@"javascript:" + icoscript + @";");
                        tdhtml.Append(@"""></div>");
                        tdhtml.Append(@"<div class=""clear""></div>");
                    }

                    tdhtml.Append(@"</div>");
                    string tdjs = tdhtml.ToString().Trim();
                    html.Append(tdhtml.Replace("[[ROW]]", row.ToString().Trim()));
                    html.Append(@"</td>");
                    if (row == 1)
                    {
                        tdjs = tdjs.Replace("", "\\‘");
                        tdjs = tdjs.Replace("[[ROW]]", "‘+rows+‘");
                        js.Append(@"var NewCell" + cell + @" = NewRow.insertCell(-1);");
                        js.Append(@"NewCell" + cell + @".className = ""inputcell " + classStr.Trim() + @""";");
                        js.Append(@"NewCell" + cell + @".innerHTML = ‘" + tdjs + @"‘;");
                    }
                    cell++;
                }

                html.Append(@"</tr>");
            }
            js.Append(@"}");
            AddNewRowScript.Append(js);

            html.Append(@"<tr class=""footer"" height=""26"" " + (IsShowSumRow ? "" : @"style=""display:none;""") + ">");
            html.Append(@"<td class=""footerid lockcell"" width=""20"">&nbsp;</td>");
            html.Append(@"<td colspan=""2"" class=""center sumname lockcell"" width=""70"">合 计</td>");

            CheckRowScript.Append(@"function checkRow(row){");
            CheckRowScript.Append(@"var isok = -1;");
            CheckRowScript.Append(@"if(!chkspace(CMSC.$$(""RowKey_""+row).value)){");
            CheckRowScript.Append(@"isok = 0;");
            foreach (TableEditColumn col in Columns)
            {
                html.Append(@"<td id=""Sum_" + col.Id + @""" class=""" + col.Align + @""">");
                if (col.SumValue.Trim() != "")
                    html.Append(col.SumValue);
                else
                    html.Append(@"&nbsp;");
                html.Append(@"</td>");

                if (col.NoEmpty)
                {
                    CheckRowScript.Append(@"if(chkspace(CMSC.$$(""" + col.Id + @"_""+row).value)){");
                    CheckRowScript.Append(@"isok = 1;");
                    CheckRowScript.Append(@"}");
                }
            }
            CheckRowScript.Append(@"}");
            CheckRowScript.Append(@"return isok;");
            CheckRowScript.Append(@"}");

            html.Append(@"</tr>");

            html.Append(@"</table>");
            html.Append(@"</div>");

            return html;
        }

        #endregion

    }

    public class TableEditColumn
    {
        public TableEditColumn(string id, string title, string align, int width, bool noempty)
        {
            this.Id = id;
            this.Title = title;
            this.Align = align;
            this.Width = width;
            this.NoEmpty = noempty;
        }

        #region 定义属性

        private string _id = "";
        private string _title = "";
        private string _align = "";
        private int _width = 50;
        private bool _islock = false;
        private bool _isedit = false;
        private bool _issumrow = false;
        private string _actionico = "";
        private string _actionscript = "";
        private string _value = http://www.mamicode.com/"";
        private string _sumvalue = http://www.mamicode.com/"";
        private string _datatype = "";
        private bool _noempty = true;

        public string Id { set { _id = value; } get { return _id; } }
        public string Title { set { _title = value; } get { return _title; } }
        public string Align { set { _align = value; } get { return _align; } }
        public int Width { set { _width = value; } get { return _width; } }
        public bool IsLock { set { _islock = value; } get { return _islock; } }
        public bool IsEdit { set { _isedit = value; } get { return _isedit; } }
        public bool IsSumRow { set { _issumrow = value; } get { return _issumrow; } }
        public string ActionIco { set { _actionico = value; } get { return _actionico; } }
        public string ActionScript { set { _actionscript = value; } get { return _actionscript; } }
        public string Value { set { _value = http://www.mamicode.com/value; } get { return _value; } }
        public string SumValue { set { _sumvalue = http://www.mamicode.com/value; } get { return _sumvalue; } }
        public string DataType { set { _datatype = value; } get { return _datatype; } }
        public bool NoEmpty { set { _noempty = value; } get { return _noempty; } }

        #endregion

    }
}

 

TableEdit.cs