Search code examples
jqueryonclickflexigrid

Jquery Flexigrid reload only if flexigrid initially ran


I'm trying to use flexigrid to show results using the values from a form. So when a person clicks a form I want it to load the flexigrid function if its the first time the person clicked the button to initialize the table. Then if someone adjusts the values in the form then clicks the button again it then reloads the flexigrid table with the new form values. How can I do this in one function? Right now I have the flexigrid code running on page load, then if they click the button it runs this:

<script type="text/javascript">
$(function() {

$('#RunReport').click( function() {
    var report_name = $('input[name=report-name]').val();
    var report_cell = $('input[name=report-cell]').val();
    var query_url = encodeURI('name=' + report_name + '&cell=' + report_cell);
    $('#reporting').flexOptions({ url: '/report.php?' + query_url }).flexReload();
});

});
</script>

But instead of having it in both functions I wanted it all in one function only running the reload if flexigrid was already initialized. Hope this makes sense, still new at all of this.


Solution

  • <script type="text/javascript">
    
    function runreport() {
        $('#RunReport').click( function() {
            var report_name = $('input[name=report-name]').val();
            var report_cell = $('input[name=report-cell]').val();
            var query_url = encodeURI('name=' + report_name + '&cell=' + report_cell);
            $('#reporting').flexOptions({ url: '/report.php?' + query_url }).flexReload();
        });
    }
    
    $(document).ready(function() {
        runreport();
    
        $('#RunReport').click( function() {
            runreport();
        });
    });
    </script>