ParamQuery grid support forum

General Category => Help for ParamQuery Pro => Topic started by: nuno.nogueira on February 10, 2014, 04:42:58 pm

Title: Prevent deleting rows having foreign keys
Post by: nuno.nogueira on February 10, 2014, 04:42:58 pm
I want to prevent users from deleting rows from a table where there are related records in another table. I'm using MySQL.

In PHP i have:

Code: [Select]
$msg=array();

    if(empty($whatever)){
        $msg['cenas']="N
Title: My previous question got stripped off
Post by: nuno.nogueira on February 10, 2014, 04:45:36 pm
In the attached file you may find my previous question, since the code was stripped off..
Title: Re: Prevent deleting rows with records
Post by: paramvir on February 10, 2014, 08:40:23 pm
You can think of couple of ways to do this:

1) Keep a hidden boolean column which tells you whether the row has associated records.
rowData["related"] == true / false

Either don't display delete button in the row having related records or show a message when Delete button is clicked.
Code: [Select]
column.render = function(ui){
  if(ui.rowData["related"]){
    return "";
  }
  else{
    return "<button>Delete</button>";
  }
}

2) Check Server side after deleteList is posted. In this case you don't need jQuery.parseJSON because dataType is already "json"
obj received by success callback is constructed in PHP.
Format of obj is {related => boolean, msg => string, rows=> Array of rows }

Code: [Select]
if (deleteList.length) {
                $.ajax($.extend({}, ajaxObj, {
                    data: { "deleteList": deleteList },
                    dataType:"json",
                    success: function (obj) {
                    //var obj = jQuery.parseJSON(rows);//not required.
                        if(obj.related){
                            //show message
                           alert(obj.msg);
                       $grid.pqGrid("rollback", { type: 'delete' });
                        }
                        else{     
                       $grid.pqGrid("commit", { type: 'delete', rows: obj.rows });
                        }
                    },
                    complete: function (response) {
                        $grid.pqGrid("hideLoading");
                        $grid.pqGrid("rollback", { type: 'delete' });
                    }
                }));
            }



Title: Re: Prevent deleting rows having foreign keys
Post by: nuno.nogueira on February 11, 2014, 02:59:13 pm
Solution 2 is working fine for now although  I think solution 1 could be more effective (less server requests).
I'll dig into it.

Thanks!
 :D