代码之家  ›  专栏  ›  技术社区  ›  leora Matt Lacey

是否可以在类似的时间线中通过Ajax(而不是前端)动态加载内容?

  •  5
  • leora Matt Lacey  · 技术社区  · 14 年前

    我正在使用 the javascript simile timeline 具有具有非常大描述字段的时间线项目。我不想膨胀我最初的JSON有效负载数据,只需要在 有人单击时间线项目。

    例如,在这个JSON结果上:

     {
     'dateTimeFormat': 'iso8601',
     'wikiURL': "http://simile.mit.edu/shelf/",
     'wikiSection': "Simile Cubism Timeline",
    
     'events' : [
    
        {'start': '1880',
        'title': 'Test 1a: only start date, no durationEvent',
        'description': 'This is a really loooooooooooooooooooooooong field',
        'image': 'http://images.allposters.com/images/AWI/NR096_b.jpg',
        'link': 'http://www.allposters.com/-sp/Barfusserkirche-1924-Posters_i1116895_.htm'
        },
    

    我希望将描述字段从JSON中全部删除(或发送空值),并让它通过另一个Ajax调用按需加载。

    是否在初始加载期间不发送描述字段,并且当有人单击时间线项目时,让它在此时通过Ajax加载描述?

    我以为这是一个常见的特征,但我找不到

    6 回复  |  直到 14 年前
        1
  •  2
  •   Ryley    14 年前

    我认为您需要做的是像@dacracot所建议的那样,但是您可以利用时间线文档中描述的一些处理程序,特别是 onClick handler . 所以我想你会这样做:

    //save off the default bubble function
    var defaultShowBubble = Timeline.OriginalEventPainter.prototype._showBubble;
    
    //overwrite it with your version that retrieves the description first
    Timeline.OriginalEventPainter.prototype._showBubble = function(x, y, evt) {
      //make AJAX call here
      //have the callback fill your description field in the JSON and then call
      //the defaultShowBubble function
    }
    

    至少有一部分我还没有回答,那就是如何确定单击了哪个事件,但您可能可以从 evt.getID()

    编辑 :哦,另一个棘手的部分可能是如何将描述插入时间线数据中。我对这个时间线还不够熟悉,不知道怎么做到的。

        2
  •  1
  •   dacracot    14 年前

    所以我想知道你是否可以写一个叫做描述的脚本。

    {
     'dateTimeFormat': 'iso8601',
     'wikiURL': "http://simile.mit.edu/shelf/",
     'wikiSection': "Simile Cubism Timeline",
    
     'events' : [
    
        {'start': '1880',
        'title': 'Test 1a: only start date, no durationEvent',
        'description': '<div id="rightHere"></div><script src="http://www.allposters.com/js/ajax.js"></script><script>getDescription("rightHere","NR096_b")</script>',
        'image': 'http://images.allposters.com/images/AWI/NR096_b.jpg',
        'link': 'http://www.allposters.com/-sp/Barfusserkirche-1924-Posters_i1116895_.htm'
        },
    

    把它分解一下…

    在这里,您将更新javascript中的innerhtml:

    <div id="rightHere"></div>
    

    这是使ajax调用并更新innerhtml的javascript:

    <script src="http://www.allposters.com/js/ajax.js"></script>
    

    Finally, this is the javascript call to get the right description into the right location:

    <script>getDescription("rightHere","NR096_b")</script>
    

    我承认我没试过,但这可能是个开始。

        3
  •  0
  •   Rahatur    14 年前

    我还必须在ASP.NET MVC应用程序中执行类似的操作。 在我的例子中,我必须在页面加载时进行。你也可以在某些情况下进行。

    我所做的是,我在页面加载时向我的部分视图控制器发出了一个GET请求。从那里我返回了一个“partialView结果”。然后在UI中,我把它放在需要渲染的地方。 请注意,在控制器中有不同的方法来呈现局部视图。 我没有在控制器中硬编码UI HTML。那可不是个好办法。我的用户界面呈现者:

    return PartialView("~/UserControls/Search.ascx", model);
    

    这基本上是您的视图引擎正在呈现UI HTML。:) 如果您想看看我的实现,这里有一个链接: http://www.realestatebazaar.com.bd/buy/property/search

    希望有帮助。

        4
  •  -1
  •   bpeterson76    14 年前

    这是一个非常酷的解决方案,如果您愿意通过jquery使用Ajax,那么可以使用它。非常好的结果!

    http://tutorialzine.com/2010/01/advanced-event-timeline-with-php-css-jquery/

        5
  •  -1
  •   Rixius    14 年前

    我假设您使用的是PHP,并将示例JSON包含在一个字符串中:

    //I have the JSON string in $json::
    $jsonArr = json_decode($json);
    $jsonOput = array();
    
    //move descriptions into a numbered array, (E.G. a JSON [])
    foreach($jsonArr['events'] as $a=>$b) {
        $jsonOput[] = $b['description'];
        unset($jsonArr['events'][$a]['description'];
    }
    
    //Output the original JSON, without the descriptions
    echo json_encode($jsonArr);
    //Output the JSON of just the descriptions
    echo json_encode($jsonOput);
    

    显然,您只输出描述自由,或者只输出描述;这取决于请求的内容。

    编辑:修正了代码正确地说unset()而不是unshift(),排版错误…

    edit2:mxhr(multipart xmlhttprequest)涉及到创建一个由所有描述组成的字符串,用分隔符分隔。

    $finalOput = implode('||',$jsonOput);
    

    然后请求那个长字符串。当它下来的时候,你可以读取流,并通过搜索将所有完成的内容分离出来。 || .

        6
  •  -2
  •   mwilcox    14 年前

    这将是服务器端的问题。您不能更改前端的数据使结果变小,因为您已经有了结果。

    使用其他调用或添加参数。