indexOf data returning -1

indexOf data returning -1

NolenNolen Posts: 6Questions: 1Answers: 0

I'm trying to create my own search function so that I can accept 3 inputs in order to search for items on the table in a specific way (a price range and a text search between columns 0/2)
The following is what I've come up with so far but when I try to compare the text input (using indexOf search or match) with the data from either column 0 or 2 it invariably returns -1 I'm at my wits end can anyone suggest where I might be going wrong?

Also I was uncertain as to what category to put this under so sorry if I posted this question under the wrong one my brain is pretty frazzled at the moment because of this.

      $.fn.dataTable.ext.search.push(
          function( settings, data, dataIndex) {
            var minPrice = parseInt($("#item-price-min").val());
            var maxPrice = parseInt($("#item-price-max").val());
            var value = $("#searchinput").val().toLowerCase();
            var tAllow = true;
            var pAllow = true;
            var name =data[0].toLowerCase();
            var price = parseFloat( data[1] ) || 0;
            var category = data[2].toLowerCase();


            if (!maxPrice && !minPrice || price >= minPrice && !maxPrice || price >= minPrice && price <= maxPrice || !minPrice && price <= maxPrice) {
                pAllow = true;
            }
            else {
              pAllow = false;
            }

            if(value !== ""){
              alert(value.indexOf(name));
              if (value.search(name>=0)) {
                tAllow = true;
              } else if (value.search(category>=0)) {
                tAllow = true;
              } else {
                tAllow = false;
              }
            }
            return tAllow && pAllow;
          }
        );

This question has an accepted answers - jump to answer

Answers

  • NolenNolen Posts: 6Questions: 1Answers: 0

    ok I just realised that I am using search in that particular example and that I had my variables the wrong way around it should've been

    name.indexOf(value>=0)
    category.indexOf(category>=0)
    

    but I still get -1 as a return now even with a full string match (as in literally copy+paste what's in the first cell into the search field)

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769

    If you want a substring search you will want the search value first, for example, value.indexOf(name);. See this indexOf string doc for more details.

    Without seeing a running example with your data its hard to say what the problem might be. Please provide a simple test case with an example of what you are doing so we can help debug. Otherwise use console.log statements or the browser's debugger to analyze the values of the variables within the search plugin.

    Kevin

  • NolenNolen Posts: 6Questions: 1Answers: 0

    Hi from the doc you gave my search term is value and the paragraph is both name and category but it only works with an 100% match the opposite way around oddly enough

    here's a link to the github for the project https://github.com/Rexofshadows/Rexofshadows-Twitchtoolkit-list/blob/master/test.html

    and here's the prototype being hosted again by github -
    please be warned it takes a moment to load the 3k plus entries and once something is entered in the search bar it will spam the hell out of the console (whilst anything is entered in it) I couldn't get the logs to fire in the last else of the search function otherwise
    https://rexofshadows.github.io/Rexofshadows-Twitchtoolkit-list/test.html

  • tangerinetangerine Posts: 3,349Questions: 37Answers: 394

    Your page shows the error "Uncaught TypeError: Cannot read property 'price' of undefined".

  • NolenNolen Posts: 6Questions: 1Answers: 0

    Yeah it's an unrelated error I'm not entirely sure whats causing it but all the entries that should be loaded do get loaded as far as I can tell (the JSON file has more entries than get loaded but I filter out any with a price below 0) and I'm not keen to look through the 3k+ entries looking for whatever one is actually causing an issue

  • NolenNolen Posts: 6Questions: 1Answers: 0
    edited July 2021

    Oh ok I think I see the problem with that unrelated error the end of the JSON file has a total of 3607 but a quick search for "price": through the file have 3483 entries so it looks like the file is mis-generated but like I said before that is an issue unrelated to the question fortunately

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    Answer ✓

    Using value.indexOf(name>=0) isn't going to work. The docs I linked to state the first parameter is expected to be a string. In your case the first parameter is a boolean. You need to perform the check of the result of indexOf() outside the method call.

    I made a simple test case for you:
    http://live.datatables.net/kemenata/1/edit

    Does this help?

    Kevin

  • NolenNolen Posts: 6Questions: 1Answers: 0

    wow ok my brain really must be frazzled I didn't catch I was trying to do my check (boolean conversion for the lack of a better term) inside the argument I was thinking maybe I made that mistake after I realised I also had my variables around the wrong way but actually looking at my question is the case there as well...
    Well thanks very much for the help I feel like I need to stop coding for 12 hours at a time now...

Sign In or Register to comment.