Search

Search

menashemenashe Posts: 196Questions: 43Answers: 2

Well, I'm back!

After a boot drive failure, I sent my SSD to a firm in Los Angeles. They were able to recover over 99% of the data--my web development (under Apache24) and my PostreSQL (under Program Files). I thought that I never would see my stuff again. And now I store my PostgreSQL database elsewhere! :)

Anyway... When I click in the Search box at the top left of a page, what event is triggered? I find that--if I have first selected a row on my datatable--the

table.on('select.dt', function(e, dt, type, indexes) {

is still triggered after I type the first letter/character in the Search box.

How can I stop this?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    The search and draw events will fire.

    If you are using server side processing then the selected rows will be reselected after the page update, for example:
    https://live.datatables.net/qoliyehi/126/edit

    Select a Name beginning with A then type a and after the server side processing ajax response is received you will see the console.log output in the select event.

    Kevin

  • menashemenashe Posts: 196Questions: 43Answers: 2

    Kevin,

    Thanks for the example. That exactly how mine behaves.

    What is the best way around that? (How can or deselect the row first? Or how can I recognize that it's "selected" after entering the first character?)

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    Answer ✓

    Is there something in your select event that causes issues when searching? What are you doing in the event handler?

    Here are some options I can think of:

    1. Use search.return so the Datatables search is performed only on hitting enter.
    2. Use event preXhr to deselect all the rows using rows().deselect().
    3. Use a flag to determine if the code in the select event executes. Set the flag variable globally to true. Set the flag to true at the end of the select event. Set the flag to false in preXhr. In select place all the code inside an if statement and only execute it if the flag is true.

    There are probably other ways to handle this. It just depends on the problem you are having and what behavior you really want.

    Kevin

  • menashemenashe Posts: 196Questions: 43Answers: 2

    Kevin,

    That is exactly the issue!

    I "borrowed" from your parent-child detail examples. Rather than only expanding the child details when the row header is clicked, I can also show "hierarchy" data when the row is selected. But I want/need to check first if there is hierarchy data. However, on the second hit (when the first character is typed in the Search), "ih2" in line 15 is undefined. I do not understand why; since the row is still selected, why isn't "dt" defined?

        itemsTable.on('select.dt', function(e, dt, type, indexes) {
            e.stopPropagation();
    
            // var x = table.rows( { selected: true } ).data();
    
            if (dt.rows({
                    selected: true
                }).count() > 1) {
                $(".itemsEditButton").text('Edit Items')
                $(".itemsDeleteButton").text('Delete Items')
            } else {
                $(".itemsEditButton").text('Edit Item')
                $(".itemsDeleteButton").text('Delete Item')
    
                let parent = dt.data().ih2.parent;
    
                if (parent != null) {
                    createsubItemsTableChildRow(dt);
                }
            }
        });
    
  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    Answer ✓

    let parent = dt.data().ih2.parent;

    dt is a variable representing the Datatables API.. So dt.data() will return all data not just row or rows selected. You will probably want to get the selected row, like line 4, except use the singlerow, for example:

    var x = table.row( { selected: true } ).data();
    let parent = x.ih2.parent;
    

    I'm not sure what ih2 is and if it contains the object parent. That part is dependent on your data structure.

    Kevin

  • menashemenashe Posts: 196Questions: 43Answers: 2

    Hi Kevin,

    Thank you!

    Incidentally, the variable ih2 in question comes from the Editor PHP equivalent of the following SQL (I have to see it in SQL to write PHP), and it does contain the field parent:

    SELECT i.id,
           item,
           prefix,
           suffix,
           CASE coalesce(ih2.parent, 0) WHEN 0 THEN 0 ELSE 1 END parent
    FROM items i
             LEFT JOIN items_hierarchy ih1 ON ih1.child = i.id
             LEFT JOIN (SELECT DISTINCT parent FROM items_hierarchy) ih2 ON ih2.parent = i.id
    WHERE i.item LIKE '%Ice Cr%'
      AND ih1.child IS NULL
    ORDER BY i.item;
    

    Basically, only an item that has subordinate items will have a "1" for parent; all others will have a "0".

Sign In or Register to comment.