【问题标题】:Proper way of creating date fields in jsGrid在 jsGrid 中创建日期字段的正确方法
【发布时间】:2016-08-09 12:25:31
【问题描述】:

所以我是一个初学者使用jsGrid制作日历网格,看起来像这样 http://i.stack.imgur.com/gU4V9.png

我已经创建了这样的标题字段:

var headerFields = [{ 
    name: "name",
    title: "", type: "text", 
    width: 60 
}];

for(var i = 1; i <= 31; i++){
    headerFields[i] = { 
        name: String(i), 
        title: String(i), 
        type: "text", 
        width: 20, 
        sorting: false, 
        inserting: false
     };
}

headerFields.push({ type: "control", width: 24, editButton: false });

然后在 jsGrid 本身中这样初始化:

$("#jsGrid").jsGrid({
    ...
    fields: headerFields,
    ...
 }

除了所有月份都有 31 天之外,我觉得这是一种非常不道德的做法,因为如果我想在某一天引用一个单元格,它就像 "item[17]" 那样模棱两可,感觉它应该有另一个像“item.day(17)”这样的层,但我很难弄清楚如何应用它。

有什么想法吗?

【问题讨论】:

  • 不确定,问题很清楚。您想在哪里以及为什么要访问像 item.day(17) 这样的项目?
  • 例如,rowClick 函数传递一个对象数组,其中包括“item”对象,该对象包含所有字段,如 name:title。这基本上只是'name:ada,1:“”,2:“”,3:“”..etc',并且这些在更新和插入时作为服务器请求变量发送。我意识到我可以将日期重命名为“day1,day2”,并通过 selectedRow.item.day2 访问它们,但这仍然感觉不完整..我希望我可以将这些天放入项目对象中自己的数组,我认为这将是“正确”的做法,呵呵
  • 哦,对不起,伙计,没有意识到你是塔巴林人,但是是的,我有时必须遍历每行的日期列,而且我根据开始的位置设置偏移量似乎很笨拙并以网格结束,然后循环遍历 cellIndexes。但我是新手,我可能过于复杂了,谢谢你的回答。

标签: javascript jquery jsgrid


【解决方案1】:

jsgrid 日期字段的工作示例

var db = {

    loadData: function(filter) {
        return $.grep(this.users, function(user) {
            return (!filter.Name || user.Name.indexOf(filter.Name) > -1);
        });
    },

    insertItem: function(item) {
        this.users.push(item);
    },

    deleteItem: function(item) {
        var index = $.inArray(item, this.users);
        this.users.splice(index, 1);
    }

};

window.db = db;

db.users = [
    {
        "Account": "D89FF524-1233-0CE7-C9E1-56EFF017A321",
        "Name": "Prescott Griffin",
        "RegisterDate": "2011-02-22T05:59:55-08:00"
    },
    {
        "Account": "06FAAD9A-5114-08F6-D60C-961B2528B4F0",
        "Name": "Amir Saunders",
        "RegisterDate": "2014-08-13T09:17:49-07:00"
    },
    {
        "Account": "EED7653D-7DD9-A722-64A8-36A55ECDBE77",
        "Name": "Derek Thornton",
        "RegisterDate": "2012-02-27T01:31:07-08:00"
    },
    {
        "Account": "2A2E6D40-FEBD-C643-A751-9AB4CAF1E2F6",
        "Name": "Fletcher Romero",
        "RegisterDate": "2010-06-25T15:49:54-07:00"
    },
    {
        "Account": "3978F8FA-DFF0-DA0E-0A5D-EB9D281A3286",
        "Name": "Thaddeus Stein",
        "RegisterDate": "2013-11-10T07:29:41-08:00"
    }
];

var MyDateField = function (config) {
    jsGrid.Field.call(this, config);
};

MyDateField.prototype = new jsGrid.Field({

    sorter: function (date1, date2) {
        return new Date(date1) - new Date(date2);
    },

    itemTemplate: function (value) {
        return new Date(value).toDateString();
    },

    insertTemplate: function (value) {
        return this._insertPicker = $("<input class='date-picker'>").datetimepicker();
    },

    editTemplate: function (value) {
        return this._editPicker = $("<input class='date-picker'>").datetimepicker();
    },

    insertValue: function () {
        return this._insertPicker.data("DateTimePicker").useCurrent();
    },

    editValue: function () {
        return this._editPicker.data("DateTimePicker").useCurrent();
    }
});

jsGrid.fields.date = MyDateField;

$("#jsGrid").jsGrid({
    height: "90%",
    width: "100%",

    inserting: true,
    filtering: true,
    editing: true,
    paging: true,

    autoload: true,

    controller: db,

    fields: [
        { name: "Account", width: 150, align: "center" },
        { name: "Name", type: "text" },
        { name: "RegisterDate", type: "date", width: 100, align: "center" },
        { type: "control", editButton: false }
    ]
});

参考:http://jsfiddle.net/tabalinas/L6wbmvfo/

【讨论】:

    【解决方案2】:

    如果您使用 Moment,这对我有用:

            var DateField = function (config) {
                jsGrid.Field.call(this, config);
            };
    
            DateField.prototype = new jsGrid.Field({
                sorter: function (date1, date2) {
                    return moment(date1) - moment(date2);
                },
                itemTemplate: function (value) {
                    return moment(value).locale('en').format('YYYY-MM-DD HH:mm:ss');
                },
            });
    
            jsGrid.fields.date = DateField;
    

    我只是忽略了编辑和插入功能,因为我不需要它们。

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 2017-05-24
      相关资源
      最近更新 更多