【问题标题】:How to create a customized javaScript array?如何创建自定义的 JavaScript 数组?
【发布时间】:2014-03-22 15:31:27
【问题描述】:

我有一个小网络应用程序。我在 .jsp 文件中使用了一个日历(代码来自 here FullCalendar)。 我使用 Spring mvc 架构。因此,当此页面加载时,负责此页面加载的控制器将向model 添加一个名为calendarEventList 的属性。 'calendarEventList' 是一个ArrayList<calObject>。 calObject 是一个包含事件详细信息的类。

我可以通过${calEvent.eventID} 在jsp 中访问这些详细信息,其中calEvent 是该ArrayList 中的一个对象。

在 FullCalendar 中,可以像下面这样调用事件加载

$('#calendar').fullCalendar({
events: [
    {
        title  : 'event1',
        start  : '2010-01-01'
    },
    {
        title  : 'event2',
        start  : '2010-01-05',
        end    : '2010-01-07'
    },
    {
        title  : 'event3',
        start  : '2010-01-09 12:30:00',
        allDay : false // will make the time show
    }
]
});

我想要的如下

$('#calendar').fullCalendar({
   events: //read the `calendarEventList` and create a suitable array 
});

我有title ,start ,end .. Oblect of the calendarEventList 中有详细信息。 所以我想创建我需要提供给fullcalendar 的列表。如何在 JS 中创建这样的数组。 它的结构应与示例中给出的[{},{},{},...,{},{}] 匹配。 {}calendarEventList ArrayList 中包含有关一个对象的详细信息。

任何详细的描述将不胜感激。

【问题讨论】:

    标签: javascript jsp fullcalendar


    【解决方案1】:

    如果我真正理解您的问题(您有对象数组,所有对象都包含不同的字段,但每个对象都包含标题、开始(和结束))。所以你的任务是过滤这个数组。

    解决方案:

    function factory(array){
      return array.map(function(item){ 
        var instance = {title: item.title, start: item.start};
        if(item.end) instance.end = item.end;
        return item;
      });
    }
    var res = factory([{ item: 1, title: 2, start: 4, an: 123, pp: "asdf"}, { item: 2, title: 2, start: 4, end: 5}, { item: 3, title: 2, start: 4}]);
    

    输出将被过滤后的对象数组,包含开始、标题、(结束)字段;

    试试demo

    【讨论】:

    • 在演示页面点击运行按钮,打开控制台查看结果!
    • calendarEventList 是一个包含 java 对象的 Java ArrayList 列表。要将数据插入event :,我需要像列表一样填充[{},{},..,{},{}]。这就是我必须做的。我认为格式应该匹配。你能帮我吗?
    • 我认为您对factory 函数的输入是我需要的输出。 [{ item: 1, title: 2, start: 4}, { item: 2, title: 2, start: 4, end: 5}, { item: 3, title: 2, start: 4}]
    • @prime read stackoverflow.com/questions/15332733/…是关于如何将arraylist转换为json格式
    • itemtitlestart.. 是我的对象值。当 calEvent 是 ArrayList 的对象时,我可以像访问任何其他 java 对象属性一样访问它们,例如 calEvent.item
    【解决方案2】:

    让我们试试我的例子:

     var date = new Date();
     var d = date.getDate();
     var m = date.getMonth();
     var y = date.getFullYear();
     var employees = [];
     employees.push({ id: 111, title: "Testing Event 001", start: new Date(y, m, d-5,10,30) , end: new Date(y, m, d-5,12,30),className: ['yellow-event', 'black-text-event']}); 
     employees.push({ id: 111, title: "Testing Event 002", start: new Date(y, m, d-3,10,30) , end: new Date(y, m, d-3,12,30),className: ['yellow-event', 'black-text-event']}); 
    

    events: employees 上放这样,, 希望对您有所帮助。谢谢你。。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      • 2016-01-06
      • 2010-11-05
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多