Data binding

17 Dec 20185 minutes to read

Field mapping

The ListBox component has a field property (object) which holds the properties to map with datasource fields. For example, the field object has a text property which is necessary to map with specific field in the datasource to render the items in the ListBox component.

The field object contains the following properties such as text, id, etc.

Local data

The local data can be an array of JSON objects which is assigned for the datasource property of ListBox component.

Here the empId and text are fields with it’s mapped to the id and value fields of object respectively.

  • HTML
  • <div id="control">
            <ul id="selectbike" ej-listbox e-datasource="dataList" e-fields-id="id" e-fields-value="value"></ul>
        </div>
  • JAVASCRIPT
  • $scope.dataList = [
                  { empId: "bk1", text: "RTR" }, { empId: "bk2", text: "CBR 150-R" }, { empId: "bk3", text: "CBZ Xtreme" },
                  { empId: "bk4", text: "Discover" }, { empId: "bk5", text: "Dazzler" }, { empId: "bk6", text: "Flame" },
                  { empId: "bk7", text: "FZ-S" }, { empId: "bk8", text: "Pulsar" },
                  { empId: "bk9", text: "Shine" }, { empId: "bk10", text: "R15" }, { empId: "bk11", text: "Unicorn" }
                 ];
         $scope.id = "empId";
         $scope.value = "text";

    FieldSetting Listbox

    Remote data

    OData

    OData is a standardized protocol for creating and consuming the data. You can retrieve data from OData service by using ej.DataManager.

    Here the CustomerID field is mapped with text property of the field object. The queries can be created using ej.Query().

  • HTML
  • <div id="control">
            <ul id="selectcustomer" ej-listbox e-datasource="dataList" e-query="query" e-fields-text="text"></ul>
        </div>
  • JAVASCRIPT
  • $scope.dataList = dataManger = ej.DataManager({
                     url: "http://mvc.syncfusion.com/Services/Northwnd.svc/"
                 });
         $scope.query = ej.Query().from("Customers").take(10);
         $scope.text = "CustomerID";

    Alt text

    WebAPI

    ASP.NET Web API is a Framework for building HTTP services. You can retrieve data from ASP.NET Web API by using ej.DataManager.

  • HTML
  • <div id="control">
            <ul id="selectcustomer" ej-listbox e-datasource="dataList" e-fields="fields"></ul>
        </div>
  • JAVASCRIPT
  • $scope.dataList = ej.DataManager({
                     url: "http://mvc.syncfusion.com/UGService/api/Orders",
                     crossDomain: true
                 });
     $scope.fields = { text: "CustomerID" };

    NOTE

    In the above data manager configuration, “crossDomain” must be set to true to access the data from Web API.

    See Also

    Cross domain

    Alt text

    Handling errors

    In remote binding, the server might not return data sometimes due to various reasons. In such cases we need to handle the error properly. We can handle it using the “actionFailure” event.

    See Also

    actionComplete and actionSuccess

  • JAVASCRIPT
  • <div id="control">
            <ul id="selectcustomer" ej-listbox e-datasource="dataList" e-fields-text="text" e-query="query" e-actionfailure="onFailure"></ul>
        </div>
  • JAVASCRIPT
  • $scope.dataList = ej.DataManager({
                     url: "http://mvc.syncfusion.com/Services/Northwnd.svc/",
                     crossDomain: true
                 });
                 $scope.query = ej.Query().from("Customers");
                 $scope.text = "CustomerID";
                 $scope.onFailure = function (args) {
                        //handle errors
                 };