⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagrid.cs

📁 一种.net实现ajax方法的类库
💻 CS
📖 第 1 页 / 共 2 页
字号:
/** Copyright (c) 2006, All-In-One Creations, Ltd.*  All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:* *     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.*     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.*     * Neither the name of All-In-One Creations, Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.**//** * Project: emergetk: stateful web framework for the masses * File name: DataGrid.cs * Description:  An html datagrid widget. *    * Author: Ben Joldersma *    * @see The GNU Public License (GPL) *//*  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version. *  * This program is distributed in the hope that it will be useful, but  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License  * for more details. *  * You should have received a copy of the GNU General Public License along  * with this program; if not, write to the Free Software Foundation, Inc.,  * 59 */using System;using System.Collections.Generic;using System.Text;using EmergeTk.Model;using System.Reflection;using EmergeTk.Widgets.Html;namespace EmergeTk{    public delegate bool DataGridEventHandler<T>( DataGrid<T> sender, Widget template, int rowIndex, int columnIndex )         where T : AbstractRecord, new() ;    [Flags]    public enum DataGridOptions    {        None = 0,        Editable = 1,        Deletable = 2,        NewRow = 4,        AutoGenerateColumns = 8,        Sortable = 16,        Filterable = 32    }    public class DataGrid<T> : Widget, IDataSourced where T : AbstractRecord, new()    {        private List<DataGridColumn> columns;        public List<DataGridColumn> Columns        {            get { return columns; }            set { columns = value; }        }        private IRecordList<T> dataSource;        public IRecordList<T> DataSource        {            get { return dataSource; }            set { dataSource = value; }        }        public event DataGridEventHandler<T> OnCustomEvent;        private Dictionary<string,Widget> myVar;        public Dictionary<string, Widget> MyProperty        {            get { return myVar; }            set { myVar = value; }        }        private DataGridOptions options = DataGridOptions.AutoGenerateColumns | DataGridOptions.Editable | DataGridOptions.NewRow | DataGridOptions.Deletable;        public DataGridOptions Options        {            get { return options; }            set { options = value; }        }        public bool AllowDeletes        {            get { return ( options & DataGridOptions.Deletable ) == DataGridOptions.Deletable; }            set            {                if (value)                    options |= DataGridOptions.Deletable;                else                    options -= DataGridOptions.Deletable;            }        }        private int pageSize = 10;        public int PageSize        {            get { return pageSize; }            set { pageSize = value; }        }        private bool prevButtonActive = false;        private bool nextButtonActive = false;        private int pageIndex = 0;        public int PageIndex        {            get { return pageIndex; }            set             {                if (value * pageSize >= DataSource.Count || value < 0)                    return;                bool forward = value > pageIndex;                pageIndex = value;                 if (rendered)                {                    string rows = "null";                    if (!pagesSent.ContainsKey(pageIndex))                        rows = DataSource.toJSON(JSON.Type.Array, columns, pageIndex, pageSize);                    InvokeClientMethod("ChangePage", string.Format("{{rows:{0},pageIndex:{1}}}", rows, pageIndex));                    if (forward && !prevButtonActive)                    {                        InvokeClientMethod("AddFooterButton", "{name:'<< Prev',pos:'1'}");                        prevButtonActive = true;                    }                    if (forward && (pageIndex + 1) * pageSize >= DataSource.Count)                    {                        InvokeClientMethod("RemoveFooterButton", "'Next >>'");                        nextButtonActive = false;                    }                    if (!forward && pageIndex == 0)                    {                        InvokeClientMethod("RemoveFooterButton", "'<< Prev'");                        prevButtonActive = false;                    }                    if (!forward && !nextButtonActive)                    {                        InvokeClientMethod("AddFooterButton", "{name:'Next >>',pos:'2'}");                        nextButtonActive = true;                    }                    pagesSent[pageIndex] = true;                }                           }        }        private List<string> autogeneratFieldNames;        public List<string> AutogenerateFieldNames        {            get { return autogeneratFieldNames; }            set { autogeneratFieldNames = value; }        }	        	    const string NewRow = "'New Row'", Prev = "'<< Prev'", Next = "'Next >>'";        private Dictionary<int, bool> pagesSent = new Dictionary<int, bool>();        private int selectedRowIndex = -1;        public int SelectedRowIndex        {            get { return selectedRowIndex; }            set { selectedRowIndex = value; }        }        private int selectedCellIndex;        public int SelectedCellIndex        {            get { return selectedCellIndex; }            set { selectedCellIndex = value; }        }        public int DataRowIndex        {            get { return pageIndex * pageSize + selectedRowIndex; }        }        public bool IsDataBound { get { return dataBound; } set { dataBound = value; } }        public override bool Render(Surface surface)        {            if (!dataBound) DataBind();            List<string> footerButtons = new List<string>();            if ((options & DataGridOptions.NewRow) == DataGridOptions.NewRow)            {                footerButtons.Add(NewRow);            }            if (PageIndex > 0)            {                footerButtons.Add(Prev);                prevButtonActive = true;            }            if (DataSource != null && PageIndex + 1 * PageSize < DataSource.Count)            {                footerButtons.Add(Next);                nextButtonActive = true;            }            ClientArguments["footerButtons"] = JSON.ArrayToJSON(footerButtons);            surface.Write( GetClientCommand() );            pagesSent[pageIndex] = true;            for ( int i = 0; i < columns.Count; i++ )            {                DataGridColumn col = columns[i];                RenderChildwidgets(surface, col.ViewTemplate as Widget, "viewTemplate", i );                RenderChildwidgets(surface, col.EditTemplate as Widget, "editTemplate", i);            }            return true;        }        private void RenderChildwidgets(Surface surface, Widget widget, string parent, int index)        {            if (widget != null && widget.Widgets != null)            {                string parentFormat = "{0}.rowTemplate[{1}].{2}";                for(int i = 0; i < widget.Widgets.Count; i++ )                {                    Widget c = widget.Widgets[i];                    c.ClientArguments["parent"] = string.Format(parentFormat, ClientId, index, parent);                    c.BaseElement = string.Format(parentFormat + ".elem", ClientId, index, parent);                    c.Render(surface);                }            }        }        bool columnsBuilt = false;        private void buildColumns()        {            List<string> cols = new List<string>();            string columnTemplateFormat = "{{name:'{0}',viewTemplate:{1},headerTemplate:{2}";            if ((options & DataGridOptions.Editable) == DataGridOptions.Editable)            {                columnTemplateFormat += ",editTemplate:{3}";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -