如何使用Javascript搜索/过滤解析的JSON数据?

时间:2018-06-05 03:44:09

标签: javascript json parsing search filter

我正在尝试创建一个实时搜索字段,以使用Javascript过滤已解析的JSON数据。 JSON数据正在呈现我想要的HTML和HTML格式。 CSS但实时过滤不起作用。

我是新手,所以它可能是显而易见的事情,但我已经尝试过& amp;没能在网上找到答案,我被困住了。我无法调用数据来过滤它 - 至少我认为这是问题所在。

HTML:

<input type="text" placeholder="Search" id="filter_listings"/>
<section>   
</section>

Javascript:

var requestURL = 'atf.json';
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'text';
request.send();
request.onload = function() {
    var directoryText = request.response;
    var directory = JSON.parse(directoryText);
    render_listings(directory);

}

var render_listings = function(jsonObj){   


    var listings = jsonObj['eateries']; 
    for(var i = 0; i < listings.length; i++) {

// where the html template is rendered & linked to JSON data  
        var myWrapper = document.createElement('div'); 
        var myHeading = document.createElement('h1');  
        myHeading.textContent = listings[i].name;    
        section.appendChild(myWrapper).className = "wrapper"; 
        myWrapper.appendChild(myHeading).className = "heading";    
//etc       
}
    var filterListings = function(event){
        keyword = input.value.toLowerCase();
        var filtered_listings = listings.filter(function(user){
            user = user.toLowerCase();
            return user.indexOf(keyword) > -1; 
        });

        render_listings(filtered_listings);
    }  

// filtering it
    input = document.getElementById('filter_listings');
    input.addEventListener('keyup', filterListings); 
}

这一行是中断的地方:user = user.toLowerCase();控制台说user.toLowerCase是类型错误,而不是函数。

我只是不确定如何将其链接到JSON数据。我已经尝试了目录,jsonObj,餐馆和列表。我还尝试将过滤器代码移到listings for onload函数下<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relative_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.android.raviagl.netlandingcostcalculate.MainActivity"> <TextView android:id="@+id/sap" android:layout_width="120dp" android:layout_height="72dp" android:layout_marginLeft="16dp" android:layout_toRightOf="@id/nlc" android:gravity="center_vertical" android:text="SAP" android:textColor="@color/colorPrimaryDark" android:textSize="20sp" /> <Button android:id="@+id/reset_all" android:layout_width="120dp" android:layout_height="72dp" android:layout_alignParentEnd="true" android:layout_alignParentTop="true" android:layout_marginEnd="13dp" android:text="Reset" /> <TextView android:id="@+id/nlc" android:layout_width="120dp" android:layout_height="72dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="8dp" android:gravity="center_vertical" android:text="NLC" android:textColor="@color/colorPrimaryDark" android:textSize="20sp" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.constraint.ConstraintLayout android:id="@+id/constraint_layout_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:layout_editor_absoluteY="81dp"> <EditText android:id="@+id/dp" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:layout_marginTop="88dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/allowance" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginRight="16dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/dp" /> <EditText android:id="@+id/gst" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/allowance" /> <EditText android:id="@+id/slab" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/gst" /> <RadioGroup android:id="@+id/slab_radio_group" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/slab"> <RadioButton android:id="@+id/radio_slab_value" android:layout_width="170dp" android:layout_height="31dp" android:layout_marginRight="16dp" android:layout_weight="1" android:checked="true" android:text="Use Fixed Amount" android:textColor="@color/colorPrimaryDark" android:textSize="14sp" app:layout_constraintRight_toRightOf="parent" tools:layout_editor_absoluteY="248dp" /> <RadioButton android:id="@+id/radio_slab_percent" android:layout_width="170dp" android:layout_height="31dp" android:layout_marginRight="16dp" android:layout_weight="1" android:text="Use SAP Percent" android:textColor="@color/colorPrimaryDark" android:textSize="14sp" app:layout_constraintRight_toRightOf="parent" tools:layout_editor_absoluteY="248dp" /> </RadioGroup> <EditText android:id="@+id/sell_through" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:layout_marginTop="48dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/slab" /> <EditText android:id="@+id/sell_out" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/sell_through" /> <EditText android:id="@+id/display" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/sell_out" /> <RadioGroup android:id="@+id/display_radio_group" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/display"> <RadioButton android:id="@+id/radio_display_value" android:layout_width="170dp" android:layout_height="31dp" android:layout_marginRight="16dp" android:layout_weight="1" android:checked="true" android:text="Use Fixed Amount" android:textColor="@color/colorPrimaryDark" android:textSize="14sp" app:layout_constraintRight_toRightOf="parent" tools:layout_editor_absoluteY="248dp" /> <RadioButton android:id="@+id/radio_display_percent" android:layout_width="170dp" android:layout_height="31dp" android:layout_marginRight="16dp" android:layout_weight="1" android:text="Use SAP Percent" android:textColor="@color/colorPrimaryDark" android:textSize="14sp" app:layout_constraintRight_toRightOf="parent" tools:layout_editor_absoluteY="248dp" /> </RadioGroup> <EditText android:id="@+id/tie_up" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:layout_marginTop="48dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/display" /> <EditText android:id="@+id/handling" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/tie_up" /> <EditText android:id="@+id/cash_discount" android:layout_width="175dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:ems="10" android:gravity="right" android:inputType="number" android:textColor="@color/colorPrimaryDark" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/handling" /> <TextView android:id="@+id/d_dp" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginStart="8dp" android:text="DP" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_allowance" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="Allowance" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/allowance" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_gst" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="GST" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/gst" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_slab" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="Slab" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/slab" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_sell_through" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="Sell Through" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/sell_through" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_sell_out" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="Sell Out" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/sell_out" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_display" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="Display" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/display" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_tie_up" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="Tie UP" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/tie_up" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_handling" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="Handling" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/handling" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/d_cash_discount" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="Cash Discount" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" app:layout_constraintBaseline_toBaselineOf="@+id/cash_discount" app:layout_constraintStart_toStartOf="parent" /> </android.support.constraint.ConstraintLayout> </ScrollView> </RelativeLayout> for循环下方的不同位置,但我得到了相同的错误。

0 个答案:

没有答案