admin管理员组文章数量:1122832
I was looking around to style events in Fullcalendar from a JSON feed, where I wanted to assign different colors to different projects. I started with simple eventSources, where I devided my data in two regional categories (north and south) using extraParams and styled them acording my needs. This worked fine, but couldn't be the way to manage around 40 different projects.
So I wonder how this could be done in an easy way?
I was looking around to style events in Fullcalendar from a JSON feed, where I wanted to assign different colors to different projects. I started with simple eventSources, where I devided my data in two regional categories (north and south) using extraParams and styled them acording my needs. This worked fine, but couldn't be the way to manage around 40 different projects.
So I wonder how this could be done in an easy way?
Share Improve this question edited 1 hour ago geom asked Jan 3 at 21:02 geomgeom 4231 gold badge5 silver badges18 bronze badges 3 |1 Answer
Reset to default 0After my first post, I realized, that I could adapt my JSON feed to include colors and textcolors by project categories, so that Fullcalendar could use them to style all events. This behavoir is mentioned in the documentation from Fullcalendar, as ADyson commented kindly, but I didn't understand it immediately. So thanks to ADyson for help and comments to clarify things.
So here just the code I used to realise this:
events: {
url: 'https://example.com/data/get_events_json.php',
},
Here the part of the php script to produce the JSON feed:
<?php
# Connect to PostgreSQL database
require("../database/db_settings.php");
# Retrive URL variables
if (empty($_GET['start'])) {
echo "Start date missing!";
exit;
} else
$start = $_GET['start'];
if (empty($_GET['end'])) {
echo "End date missing";
exit;
} else
$end = $_GET['end'];
# Query string with color and "textColor" to style events in Fullcalendar
$sql = "select id, title, start, \"end\", color, \"textColor\" from events where start >='" . $start . "' and \"end\" <= '" . $end ."'";
# Try query or error
$rs = pg_query($db_handle, $sql);
if (!$rs) {
echo "An SQL error occured.\n ";
echo $sql;
exit;
} elseif (pg_fetch_all($rs) == false) {
$resultArray = [];
} else {
$resultArray = pg_fetch_all($rs);
}
//echo $output;
echo json_encode($resultArray);
?>
The JSON feed looks like this:
[{
"id":"5014",
"title":"1 (5014)",
"start":"2025-01-02",
"end":"2025-01-03",
"color":"lightgray",
"textColor":"black"
},
{
"id":"5012",
"title":"2 (5012)",
"start":"2025-01-03",
"end":"2025-01-07",
"color":"slateblue",
"textColor":"white"
},
{
"id":"6080",
"title":"1 (6080)",
"start":"2025-01-13",
"end":"2025-01-15",
"color":"lightgray",
"textColor":"black"
}]
Here the result: Numbers are ids from projects (instead of project names) so they all get the same color, whereas numbers inside parentheses are ids from different project actions.
版权声明:本文标题:Is there an easy way to style events by project category in FullCalendar from JSON feed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284699a1927305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
I wan't to report this here since the documentation in FullCalendar does not mention this possibility
...yes, it does: fullcalendar.io/docs/event-parsing clearly describes thecolor
andtextColor
properties you've mentioned (and some other colour properties) saying that they can be used to set the colours of individual events. It's also mentioned here that you can do the same per event-source, if someone prefers, or here for the whole calendar. – ADyson Commented 2 days ago