DataTable: how to prevent row removal

Hi !

I tried to prevent row removal in the DataTable by diferent ways, but wiithout any success:

  1. DataTable validator:

        fd.spRendered(function() {	
         	fd.control('dtStages').addValidator({
                 error: 'Error message',
                  validate: function(value) {
                       this.error = "Custom error message";
                       return false;            
                  }
             });
          });
    
  2. set back the original data to the DataTable

          fd.spRendered(function() {
              fd.control('dtStages').$on('remove',  function(e) {  	  
                 e.widget.dataSource.data(window.jsonStage);			
                 //e.widget.setDataSource({ data: window.jsonStage});	
                 //fd.control("dtStages").widget.setOptions({dataSource:window.jsonStage}); 
              });
           });
    

where:

  • dtStages - the Internal name of the DataTable
  • window.jsonStage - the global variable with json data

My scope is to prevent row removal by some criteria.
Please help me!

Dear @Vladimir.Bazgan,
If you want to hide the delete column, you can use CSS code for that, as shown in this example - Remove/hide trash can on DataTables

Dear Nikita,

No, the scope is to prevent remove the row by some criteria. For example, if the item of the DataTable is used in another place (DataTable).

Dear @Vladimir.Bazgan,
I'm sorry, what? I don't think I quite get it - what do you mean by "prevent remove the row by some criteria"? The only way to remove row right now is via the button, unless you mean something else.

Also, what item? If we're talking about DataTable control, it has no items - just rows of data. Do you mean List or Library control?

Dear Nikita,

You are right, row.
And I wrote about removing via the button.

Dear @Vladimir.Bazgan,
Okay, so I guess you can use the same CSS, just via JS code, so it can be dynamically applied, no?

Try the following to hide button:

$('.fd-datatable table tr th:last-child').style.display = 'none';
$('.fd-datatable table tr td:last-child').style.display = 'none';

And the following to show:

$('.fd-datatable table tr th:last-child').style.display = '';
$('.fd-datatable table tr td:last-child').style.display = '';

Dear, Nikita,

Thank you for your response, but as I wrote before I need to prevent remove row by some criteria.
I just resolved my issue by using global array:

 fd.control('dtStages').$on('remove', function(e) {
     var checkExist = checkExistRights('Stage', e.model.Stage);
	 if (checkExist) {
	 	 alert('The stage "'+e.model.Stage+'" is used by rights settings');			            
		 fd.control("dtStages").widget.setOptions({dataSource:window.jsonStage});
         fd.control('dtStages').widget.refresh();			
	 }		
 });

Dear @Vladimir.Bazgan,
Okay, I see! Thank you for the code, I think it will be very useful for future users! Unfortunately, I didn't get that the criteria applied to each individual row in your case, not the whole DataTable as I thought.