views:

51

answers:

3

eHello everyone,the following is my code to display a jquery dialog window with a closing button "OK":

<script type="text/javascript">  
$(function(){
    $("#dialog").dialog({
        autoOpen:false,
        bgiframe:true,
        buttons: { "OK": function() { $(this).dialog("close"); } }, 
        width:500, 
        height: 350, 
        modal: true,   
        show: 'slide', 
        hide:'slide', 
        title:"Similar Trends Detected in 2nd DataSet"
    });

    $("#userid").focus();
});

function showForm(matches){
    $("#dialog").html(matches).dialog("open");
}

Currently it runs by supplying a string variable "matches",then the content of the variable gets displayed on the dialog frame. Now me and my teammate want to extend this dialog a little,we want to attach a button to every line inside the html content("matches" variable),please note that we don't want buttons in the dialog(like another "OK" button),but we want buttons "inside" the frame (the actual html content).

So I would like some help here,how could I modify my "matches" variable,to have buttons also shown inside the dialog. Thanks.

+2  A: 

what do you mean by every line? can you post a sample value for the matches variable? why not just include the buttons in the matches string value?

anyway, you can also provide a callback function to the dialog widget's 'open' event.

$("#dialog").dialog({
    autoOpen:false,
    bgiframe:true,
    buttons: { 
        "OK": function() { 
            $(this).dialog("close"); 
        }
    }, 
    width:500, 
    height: 350, 
    modal: true,   
    show: 'slide',
    hide:'slide',
    title:"Similar Trends Detected in 2nd DataSet",
    open: function() {
        var targetElements = 'br';
        $(this).find(targetElements).after('<button>click me</button>');
    }
});

after every br tag in the content, a button will be appended after it... every time the dialog is shown, the open callback will be triggered.

ricecake5
Hi,ricecake,I followed your code,but now the dialog doesn't show.Could you please give me some idea?
Robert
you still need to use your showForm() method to open the dialog.I'll update the code to fit the 'br' issue
ricecake5
+2  A: 

EDIT: Updated based on comments from OP

function showForm(matches){
      // Of course, you'll need to modify with your own button.
      // I also added a valid <br>, assuming you want it there.
    matches = matches.replace( /<\/br>/g, '<button>my button</button><br>' );

    $("#dialog").html( matches ).dialog("open"); // Insert new HTML content
}

Does the matches variable contain HTML?

You could just make a jQuery object out of it, and traverse it like any other HTML:

function showForm(matches){
      // Of course, you'll need to modify with your own button.
      // I also added a valid <br>, assuming you want it there.
    matches = matches.replace( /<\/br>/g, '<button>my button</button><br>' );

    $("#dialog").html( matches ).dialog("open"); // Insert new HTML content
}

Relevant jQuery docs:

patrick dw
Hey,patrick,thx for that.My matches variable is just a string variable,doesn't contain any HTML,could I still follow your method?Thanks.
Robert
@Robert - No. If you don't have any HTML, then jQuery's selectors won't be any good to you. You'll have to use regular expressions. I can help with that, but will need to see a sample of the text. Since you said something about lines, I assume there's something in the string that indicates the end of a line?
patrick dw
yes,Patrick,my teammate passed me the variable matches,which is a string and the end of line is indicated by "</br>".How shall we carry out the next step,plz?Many thx.
Robert
@Robert - Well, `</br>` is invalid HTML, so we might as well replace it anyway. I'll update my answer with a regular expression that should do the trick.
patrick dw
Thanks a lot!Patrick!
Robert
You're welcome. Let me know if it works.
patrick dw
Brillian Patrick,it works!Could I add you as friend?
Robert
@Robert - I'm sorry, but I'm not sure what that means. Like social networking sites? Believe it or not, I don't belong to any. Probably the last person on the planet. :o)
patrick dw
Hey Patrick,I mean adding you as a friend here on stackoverflow,I am not sure whether this website has this function,but I will keep in touch with you ,mate.
Robert
@Robert - I don't think it does (as far as I know). Otherwise, you could. I'm around quite a bit, though. I'm sure I'll see you around.
patrick dw
+1  A: 

So the matches content is some static set of HTML. Once it has been added to the DOM you can use the same selectors and controls you use for everything else. So let us assume for the moment that the matches field contains a list of elements.

function showForm(matches){
  $("#dialog").html(matches).dialog("open");
  var b = $("<input type='button' value='clickme'/>");
  $("#dialog ul li").append(b);
}

Of course this is only really going to work if you have some conception of what match contains. If you know for example that it is a set of divs with a certain class that will help in making the selector.

Gabriel