Rows not returned after column search

Rows not returned after column search

aber231aber231 Posts: 15Questions: 7Answers: 0
edited September 2022 in Free community support

http://live.datatables.net/logekawe/1/edit

For some reason when the Search Rows button is clicked, every row in the table is sent to the console instead of only the search results (search result should only include where column 2 = "5"). Any ideas?

This question has an accepted answers - jump to answer

Answers

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

    First if you want to apply the search to the table display you need to use draw(). Next if you want the rows() API to return only the filtered rows then use the selector-modifier of {search: "applied"}. Something like this:
    http://live.datatables.net/zagizunu/1/edit

    Kevin

  • aber231aber231 Posts: 15Questions: 7Answers: 0
    edited September 2022

    @kthorngren Thanks for the reply and our goal is to search the table (without changing how it is displayed to the user) and then iterate over the rows that are in the search results.

    In your example, the search still seems to return all rows versus only rows where column 2 = "5"

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

    without changing how it is displayed to the user)

    Remove the draw() API call that I added.

    the search still seems to return all rows versus only rows where column 2 = "5"

    The $(table.columns(2).search("5") results in the following number of rows being displayed:

    Showing 1 to 11 of 1,355 entries (filtered from 5,000 total entries)
    

    Which is what is displayed in the console. Sounds like you want an exact match. The default search mode is "Smart Search" which returns the 1355 rows instead of just the one you expect. Read about the different search modes in the search() docs. If you want an exact match then use a regex search, like this:
    http://live.datatables.net/zagizunu/2/edit

    I left the .draw() so you can see the result. Remove it if you don't want the table display updated. Keep in mind that if the user performs an operation that causes a table draw, like sorting, the filter will be applied to the table display then.

    Maybe instead of using column().search() you want filter() if all you want is the filtered data set instead of actually filtering the table.

    Kevin

  • aber231aber231 Posts: 15Questions: 7Answers: 0
    edited September 2022

    @kthorngren agreed, sounds like we need to use filter() instead since we are only interested in working with the data. We built out an example (link below) but it does not work. Our goal is to loop over the search results and post to the Console the value in column 2 for each row.

    http://live.datatables.net/luroxori/1/edit

    Edit: and I set your previous post as Answer 1 to this problem (looking forward to additional answers)

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

    The filter() function results in the filtered column data. You don't need this loop for this:

      filteredData.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
          var data = this.data();
          console.log(data.cell[2]);
      } );
    

    See the updated example:
    http://live.datatables.net/jejurowa/1/edit

    I added the toArray() for the result to be a Javascript array. Otherwise the result would be an API instance with the array of data.

    Kevin

  • blankengineerblankengineer Posts: 4Questions: 0Answers: 0

    @kthorngren I'm trying to do something similar with .filter() except I want to retrieve the indices for the cell where the value of the cell == someValue. I took the liberty of using the example that is already posted on this current thread to illustrate what I'm trying to do.
    Wondering if there is someway I can search for someValue in a column of the table and get the indices back for the cells in that column that were equal to someValue.
    Test example: http://live.datatables.net/diqisuru/1/edit

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

    Instead of filter() you can use rows() with the row-selector as a function to get the full row data for the rows that match your criteria. Then you have access to the row to get the data or other API related details. See the example in the row-selector docs.

    Kevin

  • blankengineerblankengineer Posts: 4Questions: 0Answers: 0

    @kthorngren Appreciate your help, thank you, I will try that.

  • blankengineerblankengineer Posts: 4Questions: 0Answers: 0

    Hey @kthorngren I am trying to use the .rows() with the row-selector as a function but I can't seem to get it working for my scenario.

    Basically I am trying to do something like this: search column 2 where value in the cell == someValue. Get the rows back for where that is true. Then in those rows I got back, search column 3 of those rows for where the value in the cell == someValue.

    I'm wondering how can I store the rows from the first 'search' and then only search those rows.

    This is the example I created for this: http://live.datatables.net/jocalusa/2/edit

    Any advice is appreciated.

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

    I made a couple updates
    http://live.datatables.net/jocalusa/4/edit

    I added a z to column 3 for one of the rows if the just for clarity. The second loop I used rows().every() with the rows().indexes() as the row-selector. There isn't a way, that I know of, to pass the row indexes and use a function at the same time.

    The other option is to just use the first loop and perform all the comparisons. I added this to the example.

    Kevin

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

    Also note that it is expected that you return a boolean value when using row-selector as a function. Returning the row index is not doing what you expect. Here is another updated example returning a boolean and displaying the row indexes:
    http://live.datatables.net/fehevine/1/edit

    Kevin

  • blankengineerblankengineer Posts: 4Questions: 0Answers: 0

    Hey @kthorngren I'm still trying to figure out the correct logic in order to avoid a full table scan as its very inefficient. I'm working on a table that has more than 100k rows, so I am trying to be as efficient as possible.

    I have included some pseudo code within my example of what I am trying to do. Basically I want to search a specific column's rows for a value. Then search another column for another value. Then compare where those search results from both of those columns intersect. Then hopefully perform some updates to a certain column that is already known for those rows that intersect. I tried to accomplish this using .rows() as well but it was very slow.

    Any ideas/advice on how to do this efficiently is appreciated! Thank you!

    Here is my example: http://live.datatables.net/xizufeyi/1/edit

    Sam

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

    The filter() API will return the data that matches the condition but nothing you can reference to know which rows they came from.

    I updated the example to show two options; one using cells().every() and the other rows().every(). They both do the same thing checking for matches in columns 2 and 3. If both match then column 1 is updated. Is this what you are looking for?
    http://live.datatables.net/fehevine/4/edit

    Not sure if there is any difference in performance. The cell loop calls the cell() API three times where the rows loop calls the rows() API twice. With 100k rows I would expect both to be slow.

    The end of the loop has //table.draw();. Just want to show that the data is updated with the cell().data() and row().data(). However the table isn't updated with regards to sorting and searching until draw() is called. I set the sort to the Name column. Uncomment this to see how draw() affects the table. You definitely won't want to call draw() inside the loops - once at the end is all that is needed.

    If this is still too slow then maybe performing the work server side would be better. You can enable server side processing which requires a server script to support the SSP protocol. The Scroller works with SSP and will pick up the updated data from the server.

    Or possibly you can create an async function to background process the loop.

    Kevin

Sign In or Register to comment.