EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: mzeddd on November 19, 2012, 11:19:11 AM



Title: Two identical records in DataGrid
Post by: mzeddd 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.


Title: Re: Two identical records in DataGrid
Post by: stworthy 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)
                });
        }
);


Title: Re: Two identical records in DataGrid
Post by: tooletime 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!!!!