DataTable Delete Based on Select

DataTable Delete Based on Select

whefnerwhefner Posts: 9Questions: 0Answers: 0
edited November 2011 in General
All,

I am new to using data tables and I have gotten my add and update to work, but having trouble on how to have this delete a row based on a radio button selection and delete from DB and DataTable.

The issue I am having is exactly how to get the delete function working, but please feel free to comment if I could be doing the add and update in a better way. Definitely not certain I have done this is the standard way.

I appreciate any help that you can provide as I have been fighting with this and I am sure it is simple but am missing the pieces to pull it together.

My Code is below and will add comments as

[code]
<?php

session_start();

require_once 'util/RoleBitMaskHelper.class.php';

if(!isset($_SESSION['username']) || !RoleBitMaskHelper::isAuthorized($_SESSION['rolebm'],RoleBitMaskHelper::ADMIN_ROLE)) {
header("location:accessdenied.php");
}

$username = $_SESSION['username'];

?>











var oTable;

oTable = $(document).ready(function() {
$('#messages').dataTable( {
"sScrollX": "100%",
"sScrollXInner": "110%",
"bScrollCollapse": true,
"sPaginationType": "full_numbers"
} );

/* Add a click handler for the delete row */
/* Need this event handler to delete row from the DataTable and delete row from mysql database by calling php function to delete this table */


$('#delete').click( function() {
var anSelected = fnGetSelected( oTable );

oTable.fnDeleteRow( anSelected[0] );
} );
} );


/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();

for ( var i=0 ; i 0 && foundselected==false) {
alert('Please select an item to work on');
return;
}

document.location.href=str;
}




@import "DataTable/css/esc_table.css";




<?php



// MySQL host name, user name, password, database, and table
require_once 'db_params.php';

?>

Update Messages
To edit a message click on the source link.
Select the check box next to a message and click delete to delete it.
Columns can be sorted in ascending or descending order by clicking on the column name.











Select    
Source
Message
Message Date
Date Added
Date Modified
User




Select
Source
Message
Message Date
Date Added
Date Modified
User



<?php require_once 'retrieveusers.php';?>

/* Actual generated data from this looks like the below content */
/* I want to select the radio button and then press the delete button and have both the mysql and datatable updated */



announcementsChurch is cancelled due to weather
2011-11-162011-11-16 19:38:23
2011-11-16 19:38:23
whefner



testtest2
0000-00-00
0000-00-00 00:00:00
0000-00-00 00:00:00
whefner












[/code]

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Possibly a daft question, but where is fnGetSelected defined? Are you not getting an error in the console about it not being present?

    Allan
  • whefnerwhefner Posts: 9Questions: 0Answers: 0
    Sorry a typo, I will add it to the original comment
  • whefnerwhefner Posts: 9Questions: 0Answers: 0
    I have the delete button working now and it redraws on the client side. I now need to take the value of one of the columns, specifically the Source column and pass that to a php function to delete the row.

    Can you please direct, me on how to get the value of a specific table cell.
  • dnagirldnagirl Posts: 36Questions: 2Answers: 0
    [code]
    var oTT = TableTools.fnGetInstance( 'messages' );
    var aSelectedTrs = oTT.fnGetSelected();
    $(aSelectedTrs).has('td:eq(1):contains("announcements")'); //selected rows that have "announcements" in the Source cell
    [/code]
  • whefnerwhefner Posts: 9Questions: 0Answers: 0
    This isn't exactly what I was looking for, in your example above aSelectedTrs is an array of tr's and I need to get the content of the first td tag in each row. Appreciate the help.
  • whefnerwhefner Posts: 9Questions: 0Answers: 0
    I have gotten so I can select the one row, but can't seem to get the first TD in that row selected also.

    $('#Update').click( function() {
    var anSelected = fnGetSelected( oTable );
    var source = $('anSelected[0]');
    var b=source;
    //alert(source);
    } );
  • whefnerwhefner Posts: 9Questions: 0Answers: 0
    Figured it out with:

    $('#Update').click( function() {
    var anSelected = fnGetSelected( oTable );
    var tds = $(anSelected).eq(0).find('td');
    var source = tds.eq(0).text();

    alert(source);
    } );
This discussion has been closed.