weixin_33749242 2011-07-18 11:45 采纳率: 0%
浏览 113

带有Ajax内容的jQuery弹出窗口

I have a Rails application. I show multiple rows of data on the page. For each row I want to create a button that will show some extra data on a popup for that row.

I checked following

  1. jQuery ui Dialog ( http://jqueryui.com/demos/dialog/ )

    • Doesn't give option to get data with ajax query,
    • I don't want hidden data for each row
  2. jQPOOOP jQuery plugin http://plugins.jquery.com/project/jQPOOOP

    • looks like works on html data, I want something that may work on json data

Is there any way to build something using only jquery ui dialog but which work on json data retrieved from an ajax request ?

  • 写回答

1条回答 默认 最新

  • weixin_33698043 2011-07-18 11:57
    关注

    Yes, don't think of it as the dialog being able to consume json data. Do this:

    1. fire your ajax
    2. in your handler prepare and reveal the dialog
    3. in the button handler for the dialog, if you want, fire more ajax and then handle the results.

    The key is to think of the dialog differently than the examples you see on the jQuery UI website. Populate the dialog dynamically by plowing through the JSON return values and use the jQuery selectors to find what you need, create more and insert new elements to the dialog content.

    Here is a more concrete example:

    $( "#dialog" ).dialog({
    modal: true,
    buttons: {
          Ok: function() {
            fire_ok_ajax_with_handler(); //pretend the handler is ok_handler
    
        }
      }
    });
    
    
    // this method is called when the action the user takes wants to
    // open the dialog. Note that it doesn't actually open the dialog
    // but instead starts the ajax process of getting the data it needs
    // to prepare the dialog
    $( "#opener" ).click(function() {
      $( "#dialog" ).dialog( "open" );
        fire_ajax_to_start_stuff();
        return false;
    });
    
    function fire_ajax_to_start_stuff(...) {
        // assume start_handler method
    }
    
    function start_handler(data) {
      //process data, which can be json if your controller formats it that way
      // use the data to dynamically setup the dialog,
      // show the dialog
      $( "#dialog" ).dialog( "open" );
    }
    
    function fire_ok_ajax_with_handler() {
      // this is where you set up the ajax request for the OK button
    }
    
    function ok_handler(data) {
      // handle possible errors messages
      // close the dialog
      $( this ).dialog( "close" );
    }
    

    Please note that there is a LOT of pseudocode/hand waving in that example but it should give you the basic approach.

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用