Page 1 of 1

How checkboxes keep checked after reload with CustomAction?

Posted: Mon Oct 03, 2022 9:51 am
by Mylovetron

Hi, I have a custom action in my list page(with 2 page). I checked some item in page 1 (by checking checkboxes), when I next page 2 and return then some checked item in page 1 uncheck(reset). Could I keep checked item in page 1 after reload?
my code Page_Load

$this->CustomActions["button"] = new ListAction("myfunciton", "button", IsLoggedIn(), "",ACTION_MULTIPLE, "Y/N?", "far fa-file-word ew-icon");

Thanks!


Re: How checkboxes keep checked after reload with CustomAction?

Posted: Mon Oct 03, 2022 10:39 am
by arbei

Row custom action are supposed to be selected in the current page and then submitted. Even you write some code yourself to keep the checked status across pages, when you submit, only the checked and shown records in the current page will be submited. If you must do that, you need to write your code to keep the primay keys of all checked records (e.g. by JavaScript) and then submit by Ajax (instead of posting pack).


Re: How checkboxes keep checked after reload with CustomAction?

Posted: Wed Oct 05, 2022 10:08 am
by Mylovetron

please suggest to me where could i code JavaScript?
thanks!


Re: How checkboxes keep checked after reload with CustomAction?

Posted: Wed Oct 05, 2022 11:33 am
by arbei

Mylovetron wrote:

$this->CustomActions["button"] = new ListAction("myfunciton", "button", IsLoggedIn(), "", ACTION_MULTIPLE, "Y/N?", "far fa-file-word ew-icon");

Since your 4th argument is "", submission is by post back (not Ajax).

If you view the HTML source, you'll find that each checkbox has the primary key as value, e.g.

<input type="checkbox" name="key_m[]" value="xxx" ...>

When you submit the form, the server side received the selected records by "key_m[]".

So you may add your "click" handler for checkboxes:

  • if checked, save the selected value at some storage (e.g. window.localStorage)
  • if unchecked, remove the selected value from your storage
  • Before submission, check if the selected records are already in the form, if not, add hidden tags (with name="key_m[]") to the form so the values will also be posted back
    e.g. in Startup Script
    $(document).on("click", "input[name='key_m[]']", function() {
        // your code to save the selected records
    });
    $("#f<mytable>list").on("beforesubmit", function() { // Replace <mytable> by your table name
        // your code to add hidden tags
    });

Re: How checkboxes keep checked after reload with CustomAction?

Posted: Thu Oct 06, 2022 3:10 pm
by Mylovetron

that's great!
Thanks so much!

this my code:

function removeItem(arr, item){
 return arr.filter(f => f !== item)
}

$('input[name="key_m[]"]').on("change", function() {
	// your code to save the selected records
	var array = [];
	array =localStorage.getItem('myArray');
	array = JSON.parse(array);

  	if(this.checked) {
		array.push($(this).val());
  		
	}
	else
	{
		array = removeItem(array, $(this).val());
	}
	localStorage.removeItem('myArray');
	localStorage.setItem('myArray', JSON.stringify(array));
  	console.log(array.join(", "));
});

$(function(){
	 var array=[];
	 array= localStorage.getItem('myArray');
	 array = JSON.parse(array);
	  for(i=0;i<array.length;i++)
	   	$('input[name="key_m[]"]').each(function(index) {
			if ($(this).val() == array[i])
				$(this).prop('checked', true);
		});
});