Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Click in a row event #43

Closed
diclonius opened this issue Jan 23, 2020 · 3 comments
Closed

Click in a row event #43

diclonius opened this issue Jan 23, 2020 · 3 comments
Labels

Comments

@diclonius
Copy link

diclonius commented Jan 23, 2020

Hi, again me: D
I have the following case and I hope you can help me.

I am adding the following code by jquery:

$('.datagird tr').click(function(){
	var code = "";

	code = $(this).find("td").eq(0).find("input").val();
	$("hdfCode").val(code);

	$(".selectRow").toggleClass('selectRow');
	$(this).find("td").toggleClass('selectRow');
});

Everything works fine, the problem occurs when I make a filter or change of page, then the click event of the row stops working, I imagine it is due to the restructuring of the table.

Any idea to keep the click event in the row working, even after making a filter or changing the page.

@Holt59
Copy link
Owner

Holt59 commented Jan 24, 2020

The <tr> are destroyed each time an operation is performed, but there are ways to make the event working. You can use any of the following ways (not tested):

$(".datagird").on("click", "tr", function() { });
  • Put your code in the afterRefresh callback of the datatable.
  • Use a custom lineFormat option when constructing the datatable and apply the .click within the function.

I cannot write actual code to show you how to use these right now but if you have issue with the above directions, feel free to ask for clarification.

@diclonius
Copy link
Author

Hello, the first option was great, but the question remains. How can I use the "afterRefresh" and place a code that I need.

@Holt59
Copy link
Owner

Holt59 commented Jan 24, 2020

Hello, the first option was great, but the question remains.

So the first option did not solve your issue?

How can I use the "afterRefresh" and place a code that I need.

This should work (when constructing the datatable:

$(".datagird").datatable({
    /* Your other options... */
    afterRefresh: function() {
         // Or $(this).find("tr").click 
        $('.datagird tr').click(function(){
            var code = "";

            code = $(this).find("td").eq(0).find("input").val();
            $("hdfCode").val(code);

            $(".selectRow").toggleClass('selectRow');
            $(this).find("td").toggleClass('selectRow');
        });
    }
});

@Holt59 Holt59 closed this as completed Jan 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants