EasyUI Forum
September 14, 2025, 08:15:20 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Two identical records in DataGrid  (Read 11641 times)
mzeddd
Full Member
***
Posts: 223



View Profile
« on: November 19, 2012, 11:19:11 AM »

Hi,

How to protect DataGrid from multiple entries of one record?

I have DataGrid where I have some data (loaded from PHP script).
I add new record by colling the following code

Code:
$.post('getData.php?id='+id,
        function(data){
                $('#trTable').datagrid('insertRow',{
                        index: 0,
                        row: jQuery.parseJSON(data)
                });
        }
);

But if I call this code more then once for the same "id" then I have duplicated lines in my datagrid.

Is there any way to protect datagrid from multiple load of one record?

PS: Yes, I know that I can check data before I insert it. But I wonder if datagrid itself could do this kind of job.
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: November 20, 2012, 07:28:28 PM »

It is easy to extend a new method to prevent from inserting duplicate records.

Code:
$.extend($.fn.datagrid.methods,{
insertUniqueRow: function(jq,param){
return jq.each(function(){
var opts = $(this).datagrid('options');
var rows = $(this).datagrid('getRows');
for(var i=0; i<rows.length; i++){
if (rows[i][opts.idField] == param.row[opts.idField]){
return;
}
}
$(this).datagrid('insertRow',param);
});
}
});

Now call 'insertUniqueRow' method to insert your records.

Code:
$.post('getData.php?id='+id,
        function(data){
                $('#trTable').datagrid('insertUniqueRow',{
                        index: 0,
                        row: jQuery.parseJSON(data)
                });
        }
);
Logged
tooletime
Newbie
*
Posts: 8


View Profile Email
« Reply #2 on: March 23, 2016, 08:06:34 AM »

this should be part of the build itself.  this just saved me a ton of work!

Thank you!!!!
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!