views:

45

answers:

1

Hi Guys and Gals,

I am trying to create a script using FLEXIGRID as a method to display the information from a database, but I want the users to able to input info into the database as well. I would like to be able to launch a modal window where the user can input the information and submit it. The way the buttons on the FLEXIGRID are coded is as following:

$(document).ready(function(){

 $("#flex1").flexigrid
   (
   {
   url: 'post2.php',
   dataType: 'json',
   colModel : [
    {display: 'ID', name : 'id', width : 40, sortable : true, align: 'center', hide: true},
......
   buttons : [
    {name: 'Add', bclass: 'add', onpress : test},
    {separator: true},
    {name: 'Delete', bclass: 'delete', onpress : test},
    {separator: true},
.... some more code ...

   }
   );   

});
function test(com,grid)
{
    if (com=='Add')
    {
              the code that triggers the modal window
    }
}

Okay, my problem:

When I press 'Add', I would like a modal popup window to appear to load using Ajax the content of a local file so the user can input the information.

What I have so far:

I tried using the code from JqModal: loaded the CSS and Javascript:

<link rel="stylesheet" type="text/css" href="css/jqmodal.css" /> 
<script type="text/javascript" src="js/jqModal.js"></script>
$().ready(function() {
$('#ex2').jqm({ajax: 'examples/2.html', trigger: 'a.ex2trigger'});
});

added the div at the bottom of the page:

<div class="jqmWindow" id="ex2">
Please wait... <img src="inc/busy.gif" alt="loading" />
</div>

but how to trigger the function?

Thanks, Cristian.

LE: This is the code I have right now and it still doesn't work:

IE says: Object doesn't support this property or method flexigrid, line 56 character 5 That's exactly where the $dialog function starts.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Flexigrid</title>
<link rel="stylesheet" type="text/css" href="css/flexigrid.css" />
<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.7.3.custom.css" />
<script type="text/javascript" src="js/jquery-1.2.3.pack.js"></script>
<script type="text/javascript" src="js/flexigrid.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.3.custom.min.js.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    $("#flex1").flexigrid
            (
            {
            url: 'post2.php',
            dataType: 'json',
            colModel : [
                {display: 'ID', name : 'id', width : 40, sortable : true, align: 'center', hide: true},
                {display: 'URL', name : 'url', width : 450, sortable : true, align: 'left'},
                {display: 'File Name', name : 'filename', width : 270, sortable : true, align: 'left'},
                {display: 'Availability', name : 'availability', width : 50, sortable : true, align: 'center'},
                {display: 'State', name : 'state', width : 40, sortable : true, align: 'center'},
                {display: 'Total Size', name : 'totalsize', width : 90, sortable : false, align: 'center'},
                {display: 'Current Size', name : 'currentsize', width : 90, sortable : false, align: 'center'},
                {display: 'Procent', name : 'procent', width : 40, sortable : true, align: 'center'},
                {display: 'Log',  width : 20, sortable : false, align: 'center'},
                ],
            buttons : [
                {name: 'Add', bclass: 'add', onpress : test},
                {separator: true},
                {name: 'Delete', bclass: 'delete', onpress : test},
                {separator: true},
                {name: 'Select All', bclass : 'selectall', onpress : test},
                {name: 'DeSelect All', bclass : 'deselectall', onpress : test},
                {separator: true}
                ],
            searchitems : [
                {display: 'URL', name : 'url'},
                {display: 'Filename', name : 'filename', isdefault: true}
                ],
            sortname: "state",
            sortorder: "asc",
            usepager: true,
            title: '',
            useRp: false,
            rp: 10,
            showTableToggleBtn: true,
            singleSelect: true
            }
            );   
});
$(document).ready(function() {
    $("#myDialog").dialog({
        autoOpen: false,
        resizable: false,
        position: 'center',
        stack: true,
        height: 'auto',
        width: 'auto',
        modal: true
     });

    $("#showDialog").button().click(function (event) {
        $("#myDialog").dialog('open');
    });
});
function test(com,grid)
{
    if (com=='Add') {
        $("#myDialog").dialog('open');
    }
    if (com=='Select All')
    {
        $('.bDiv tbody tr',grid).addClass('trSelected');
    }

    if (com=='DeSelect All')
    {
        $('.bDiv tbody tr',grid).removeClass('trSelected');
    }

    if (com=='Delete')
        {
           if($('.trSelected',grid).length>0){
               if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
                    var items = $('.trSelected',grid);
                    var itemlist ='';
                    for(i=0;i<items.length;i++){
                        itemlist+= items[i].id.substr(3)+",";
                    }
                    $.ajax({
                       type: "POST",
                       url: "delete.php",
                       data: "items="+itemlist,
                       success: function(data){
                        $('#flex1').flexReload();
                        alert(data);
                       }
                    });
                }
            } else {
                return false;
            } 
        }          
} 
</script>
</head>

<body>
<table id="flex1" style="display:none"></table>
<div id="myDialog" style="display:none" title=""></div>
</body>
</html>

LE2: How to load the external file thru ajax:

$(document).ready(function(){
    //define config object
   var dialogOpts = {
        modal: true,
        bgiframe: true,
        autoOpen: false,
        height: 500,
        width: 500,
        draggable: true,
        resizeable: true,
        open: function() {
        //display correct dialog content
        $("#myDialog").load("form.php");}
        };
    $("#myDialog").dialog(dialogOpts);  //end dialog

    $('#showdialog').click(function (event){
            $("#myDialog").dialog("open");
            return false;
        }
    );

});
+1  A: 

The following sample will work with jQuery UI dialog

First defines the needed markup for the dialog:

<div id="myDialog" style="display:none" title=""></div>

Then, on DOM ready, setup the dialog to be a jquery UI dialog

$("#myDialog").dialog({
    autoOpen: false, position: 'center', stack: true, height: 'auto', width: 'auto', modal: true
});

Please notice the autoOpen: false parameter.

When you want to show the dialog just call the open method inside of your code

function test(com,grid) {
    if (com=='Add') {
        $("#myDialog").dialog('open');
    }
}

hope it helps!

Update:

I have created a sample on jsbin.com for you. You can see it working here while you can see the code here.

Lorenzo
Hi Lorenzo, Unfortunately I can't get it to work, if I copy/paste your code the grid doesn't even load. Note sure what to say. Thanks,C.
Chris19
Did you included the jQuery UI javascript file and css?
Lorenzo
Look at my update at the end of the answer
Lorenzo
I updated my question, Thanks for your help.
Chris19
@Chris19: you are missing the jquery include script. Also your jquery Ui include has double ".js" at the end. Is that correct?
Lorenzo
I am using the jquery version 1.2.3 pack that came with flexigrid and you were right, I had a double 'js' there. After I deleted the double, if I press the add button I get the overlay but no message??!!
Chris19
I think that version is a little bit outdated and maybe is not the right one to use with jquery ui 1.7.3. From the flexigrid package I am only including flexigrid.js and flexigrid.css. Then I am using jquery latest versions of both the core and the UI.
Lorenzo
Okay, seems that I am not paying to much attention to your advices, you were right again, I loaded the newer version of jquery and now I get the message, displayed but IE keeps throwing me error messages about the dialog function ... not sure what's going on. Thanks for your help again :).
Chris19
which version of IE are you using? Can you try with another browser?
Lorenzo
With FF 3.6.11 works fine, with IE 8.0 is giving me that error. I tried to use the latest jquery-1.4.2.min.js and jquery-ui-1.8.5.custom.min.js and the flexigrid is not loading. So looks like, I'll have to stick to the previous versions. Any ideeas on how to get the form to load into that modal window?
Chris19
@Chris19: I have no idea on how to help more. I have used Flexigrid in a previous project I worked and everything was working fine. In the latest project I am not using it anymore because the flexigrid, despite the fact that is very nice and light, seems it is like a death project as is not updated from long time. We have switched on using jqGrid now...
Lorenzo
No worries, thanks a bunch for all ur help. Figured out how to do it, I updated my question and corrected the problem with the dialog function, there was no need for the .button there. Works great. I'll have a look at the jqGrid, I am not bound to use Flexigrid, was the first one I found and used it, maybe I'll change as well. Cheers, Cristian.
Chris19
Forgot to ask, when you were using it, did you find a way to auto refresh the grid?
Chris19
@Chris19: I dont think there was a feature for auto-rerfresh of the grid data. We have used a javascript function to do the job. Something like this: function `refreshData(){$("#hearingGrid").flexReload();}`. Dont forget to change the "hearingGrid" with your flexigrid ID...
Lorenzo