vis.js重绘和更改数据

时间:2016-06-28 14:38:37

标签: javascript jquery ajax redraw vis.js

我使用vis.js时间轴显示事件,并从ajax调用中显示所有项目。

目前我在页面上有一个工作过滤器,所以时间线到达一个特定的范围,这很好。

我要做的是缩小显示的信息范围。我对如何做到这一点的初步想法是更改ajax调用,以便它返回一个只需要我需要的信息的json。

一个例子:

  1. 选择地区:
    • 英格兰北部
    • 英格兰南部
  2. 调用的Ajax将过滤数据库,因此它只显示在特定列中具有该属性的项目
  3. 然后我会使用ajax调用中的新json重绘时间轴
  4. 我要解决的问题是第1部分和第2部分,是否有人知道我将如何指定不同的Ajax网址以及如何重绘表格?

    我已经尝试过timeline.redraw():没有运气,所以我先破坏了桌子,但仍然没有骰子(timeline.destroy(); timeline.redraw();)摧毁comman有效但不是重绘。

    这是我的代码(注意日期过滤器工作正常) HTML

    <!-- this is just the filter section -->
    <div id="filters" style="margin: 10px; background-color: RGB(229,229,229); border-radius: 10px; padding: 5px">
            <div id="filterscontainer" style="display: inline-block">
                <table>
                    <tr>
                        <td>
                            <div style="float: left; font-size: 1.25em">Filters</div>
                    </td>
                    <td></td>
                    <td>
                        <div id="expandcollapseFilters" class="chevron bottom" title="Click to expand the filter menu" style="float: left"></div>
                    </td>
                </tr>
            </table>
        </div>
        <div id="filtercontent" style="display: none">
            <table>
                <tr><td colspan="2"><button id="reset">Reset all filters</button></td></tr>
                <tr>
                    <td>
                        <table>
                            <tr>
                                <td style="text-align: right">Date from:</td>
                                <td>
                                    <input id="dateFrom" class="date"/></td>
                                <td style="text-align: right">Date to:</td>
                                <td>
                                    <input id="dateTo" class="date"/></td>
                                <td>
                                    <button id="dateRangeFilter">Apply Filter</button></td>
                            </tr>
                        </table>
                    </td>
                    <td></td>
                </tr>
            </table>
        </div>
    </div>
    <div id="currentlyShowing"><h3>Currently showing: <span class="currentlyShowing">All items</span></h3></div>
    <div id="mytimeline"></div>
    <div id="loading">loading...</div>
    

    使用Javascript / jquery的:

    <script type="text/javascript">
        $("#filterscontainer").on("click", function () {
            $('#currentlyShowing').slideToggle('slow');
            $('#filtercontent').slideToggle('slow');
            if ($("#expandcollapseFilters").prop("class") === "chevron top") {
                $('#expandcollapseFilters').prop('collapseFilters', "Click here to collapse the filter menu");
                $('#expandcollapseFilters').addClass('bottom').removeClass('top');
            } else {
                $('#expandcollapseFilters').prop('collapseFilters', "Click to expand the filter menu");
                $('#expandcollapseFilters').addClass('top').removeClass('bottom');
            }
        });
        $.ajax({
            type: "POST",
            url: 'Ajax.asp?RT=RoadMap', data: {},
            success: function (data) {
                var result = JSON.parse(JSON.parse(JSON.stringify(data)));
                document.getElementById('loading').style.display = 'none';
                var items = new vis.DataSet(result);
                var options = {
                    orientation: 'top',
                    groupOrder: 'order',
                    showCurrentTime: false,
                    align: 'left',
                    editable: false,
                    minHeight: '300px',
                    min: new Date(2016, 0, 1),                // lower limit of visible range
                    max: new Date(2018, 0, 1),                // upper limit of visible range
                    zoomMin: 1000 * 60 * 60 * 24 * 7,         // one week in milliseconds
                    zoomMax: 1000 * 60 * 60 * 24 * 365        // about a year
                };
                var container = document.getElementById('mytimeline');
                var timeline = new vis.Timeline(container, items, options);
                document.getElementById('dateRangeFilter').onclick = function () {
                    var dateFrom = $("#dateFrom").val();
                    var dateTo = $("#dateTo").val();
                    if (dateFrom.length < 1 || dateTo.length < 1 || dateFrom > dateTo) { //checking if the date to date is after the from date
                        alert("Please select a valid date range");
                    } else { //converting the resturn results to a date format accepted by timeline
                    var dateFromDay = dateFrom.substr(0, 2);
                    var dateFromMonth = dateFrom.substr(3, 2);
                    var dateFromYear = dateFrom.substr(6, 4);
                    var dateToDay = dateTo.substr(0, 2);
                    var dateToMonth = dateTo.substr(3, 2);
                    var dateToYear = dateTo.substr(6, 4);
                    timeline.setWindow(dateFromMonth + "-" + dateFromDay + "-" + dateToYear, dateToMonth + "-" + dateToDay + "-" + dateToYear);
                    }
                };
            }
        });
        $().ready(function () {
            $("#reset").on("click", function () {
                $.datepicker._clearDate("#dateFrom");
                $.datepicker._clearDate("#dateTo");
            });
            $("#dateFrom").datepicker({
                beforeShowDay: $.datepicker.noWeekends,
                dateFormat: "dd/mm/yy"
            });
            $("#dateTo").datepicker({
                beforeShowDay: $.datepicker.noWeekends,
                dateFormat: "dd/mm/yy"
            });
        });
    </script>
    

1 个答案:

答案 0 :(得分:3)

我得到了它的工作,为了面对这个问题的任何其他人,这里是你如何做到/绕过它。

在ajax调用之前声明变量“items”, 在声明变量“timeline”之前放置事件处理程序 在此范围内,清除变量“items”(items.clear();)并添加它的新内容(items.add();)。你可以在parethesis中构建字符串,或者像我使用ajax调用并在里面返回数据。

下面的

是现在过滤特定区域的工作代码:

    var items;
    var timeline;
    $.ajax({
        type: "POST",
        url: 'Ajax.asp?RT=RoadMap', data: {},
        success: function (data) {
            var AllData = JSON.parse(JSON.parse(JSON.stringify(data)));
            document.getElementById('loading').style.display = 'none';
            items = new vis.DataSet();
            items.add(AllData);
            var options = {
                orientation: 'top',
                groupOrder: 'order',
                showCurrentTime: false,
                align: 'left',
                editable: false,
                minHeight: '300px',
                min: new Date(2016, 0, 1),                // lower limit of visible range
                max: new Date(2018, 0, 1),                // upper limit of visible range
                zoomMin: 1000 * 60 * 60 * 24 * 7,         // one week in milliseconds
                zoomMax: 1000 * 60 * 60 * 24 * 365        // about a year
            };
            var container = document.getElementById('mytimeline');
            $("#regionFilter").on("click", function () {
                var regionSelected = $("#regionSelector").val();
                if (siteSelected === "") {
                    alert("Please Select the region you would like to see the information of");
                } else {
                    items.clear(); // note this is the clear function which removes the data but doesn't destroy the timeline
                    $.get("Ajax.asp?RT=RoadMapUpdate&Region=" + regionSelected, function (AjaxReturn) {
                        var AjaxReturn = JSON.parse(JSON.parse(JSON.stringify(AjaxReturn)));
                        items.add(AjaxReturn); // this is where the timeline get's re-populated
                    });
                }
            });
            $("#reset").on("click", function () { // this function is tied to a button I added which resets the filters, it puts wverything back to the way it was at load
                items.clear();
                items.add(AllData);
            });
            timeline = new vis.Timeline(container, items, options);