4

I'm posting a very simple example of what I want to do because it's easier to explain that way.

  1. I have a datatable populated with a cfquery.
  2. When you double click on a table row, the JQuery script will send the RecordID to an action page as a url variable.
  3. The action page takes the url variable and passes it into a delete query which deletes from the SQL table that populates the table on the page.
  4. If you refresh/reload the cfm page, the query runs again and the table is updated, minus the row that was just deleted.

So, here's my issue. I want this table to be updated automatically on the double click without having to reload the page. I realize that this isn't setup the proper way for that, but I need advice. Just telling me that "you need an ajax response" doesn't help much. I need examples or a better explanation. I've been reading for days, but I can't figure out the server side processing and getting a json or ajax response. I need help.

Start by using this simple data...

CREATE TABLE [dbo].[TEST]([RecordID] [int] NULL,[Name] [varchar](25) NULL)

Insert into TEST (RecordID, Name)
Values ('1','Mike')

Insert into TEST (RecordID, Name)
Values ('2','Bill')

Insert into TEST (RecordID, Name)
Values ('3','Joe')

Next, here's the cfm page...

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="../JQuery/js/datatables/DataTables-1.9.4/media/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/smoothness/jquery-ui.min.css"/>
<link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables_themeroller.css"/>


<script>
    $(document).ready(function() {
        var table1 = $("#table1").dataTable({bJQueryUI:true});

        //when you double click a row in this table it will grab the RecordID
        $("#table1 tr").dblclick(function() {
            var RecordID = $(this).find("#RecordID").text();

            //If the RecordID is defined, show it in an alert, then send back to the action page where it uses the URL variable in a delete query.
            if(RecordID) {
                alert(RecordID);
                $.post('TESTACTION.cfm?RecordID='+ RecordID);       
            }
        });
    });
</script>

<!---This query populates the table--->
<cfquery name="Query1" datasource="x">
    Select RecordID, NAME
    From TEST
</cfquery>

    <form>
        <div id ="div1" class="dataTables_wrapper">
            <table id="table1" class="dataTable">
                <thead>
                        <tr>
                            <th>RecordID</th>
                            <th>Name</th>
                        </tr>
                </thead>
                <tbody>
                    <cfoutput query = "Query1">
                        <tr>
                            <td id="RecordID">#Query1.RecordID#</td>
                            <td>#Query1.NAME#</td>
                        </tr>
                    </cfoutput>
                </tbody>
            </table>
        </div>

    </form>

Finally, the TESTACTION.cfm page...

<cfif isdefined("url.RecordID")>
    <cfquery name="x" datasource="x">
        Delete from TEST where RecordID = '#url.RecordID#'
    </cfquery>
</cfif>
madvora
  • 1,717
  • 7
  • 34
  • 49
  • 1
    Sounds like you want to refresh the div that contains the data table. A google search on "coldfusion refresh div" should give you something to look at. – Dan Bracuk Apr 09 '14 at 19:48
  • OK, I did find a way to delete the row on the client side after the post. I can add this after the $.post line... `code........ var row = $(this).closest("tr").get(0); table1.fnDeleteRow(table1.fnGetPosition(row)); ` However, I don't think this is the proper way to do this. I know I should really be using ajax somehow instead of posting to an action page – madvora Apr 09 '14 at 19:58
  • What you wrote to delete the row is how I have done it in the past. – abbottmw Apr 09 '14 at 20:00

1 Answers1

1

Add an id to your rows and use the success function of the jquery post to delete the row.

CF code:

<cfoutput query = "Query1">
    <tr id="row_#Query1.RecordID#>
        <td id="RecordID">#Query1.RecordID#</td>
        <td>#Query1.NAME#</td>
    </tr>
</cfoutput>

jquery:

$.post('TESTACTION.cfm?RecordID='+ RecordID, function(){
    $("#row_" + RecordID).remove();
});

jquery .post documentation

Edit from comments:

If you use fnDeleteRow, you should be able to do it by the row id:

$.post('TESTACTION.cfm?RecordID='+ RecordID, function(){
    //$("#row_" + RecordID).remove();
    table1.fnDeleteRow(table1.fnGetPosition($("#row_" + RecordID)));
});
jk.
  • 14,365
  • 4
  • 43
  • 58
  • Yes, this works. Thank you. My only concern is that I'd like the datatable to always show the results of the query populating it, so if I post a change, the action page deletes a record, and I would like the datatable to show the current results of the query (the same as what would happen if I reloaded the page) but without the code that just removes the on-screen display of the row in the table. The reason for this is because I'm going to be doing the same with another table populated by a query that will show new added data instead of removing deleted data. – madvora Apr 10 '14 at 12:49
  • From what I've read, I believe this needs to be an ajax call to another page which displays the data in an array. It's that array that gets refreshed. I'm having a hard time figuring out how to do this. – madvora Apr 10 '14 at 12:56
  • Just noticed that .remove() doesn't work well with datatables because it doesn't update the count. Using fnDeleteRow will do this. – madvora Apr 10 '14 at 16:19
  • This works for the posted question, so I'll consider this a complete answer. Thanks. I'll have to post another question for Ajax method. – madvora Apr 11 '14 at 12:34