0

This one seems simple, just not sure where to handle it. I am retrieving column name values from a SQL database via REST JSON. The values in the dropdown list have underscores as expected (Customer_Name). I want to display a "friendly" (Customer Name) version without underscores, yet still be sending my value (Customer_Name) to my REST service. I looked at Underscore.js but just not sure where to start and this may be simpler than I think. Thanks!

ripsin
  • 273
  • 2
  • 7
  • 19

1 Answers1

1

I don't know how you receive your data from your rest service.

But basically, you just have to map your data received by your REST service changing the value as you want.

Here is some sample code :

// Call to the REST service and when done call the callback function loadDDL
$.ajax({
  type: "POST",
  url: "my-rest-service"
}).done(loadDDL);


// Callback function when returning from the REST service
// -> load data in the DDL (here, it has the id "my-ddl")
function loadDDL(data) { 
  $("#my-ddl").kendoDropDownList({
    dataTextField: "text",
    dataValueField: "value",
    dataSource: _.map(data, makeFriendlyName),
    index: 0
  }); 
}


// Function used by the _.map function in order 
// change dynamically the labels in the DDL
function makeFriendlyName(obj) {
  return { 
    text: obj.text, 
    value: obj.value.replace("_", " ") 
  };
}

EDIT :

Based on OP's fiddle, here is a sample code using templates instead of directly changing the datasource :

function loadDDL(data) { 
  $("#my-ddl").kendoDropDownList({
    autoBind: true,
    dataTextField: "DOMAINQUERY",
    dataValueField: "COLUMN_NAME",
    dataSource: dataSourceSearch1,
    template: "${DOMAINQUERY.replace(/_/g, ' ')}"
  }); 
}

EDIT 2 :

In order to translate directly the datasource, I comes again to a re-mapping of the datasource by changing dynamically the text in the change event of the datasource :

var dataSourceSearch1 = new kendo.data.DataSource({
    transport: {
        read: {
            url: "http://demos.kendoui.com/service/Customers",
            dataType: "jsonp"
        }
    },
    change: changeDS // <-- Here add a change event : each time the datasource changes, this event is being raised
});


// This is the function call when the DS changes
// the data stuff is in the `items` property which is the object send via the REST service
function changeDS(datasource) {
    _.map(datasource.items, makeFriendlyName);
}


// Function to apply to each data -> here I just replace all spaces in the 
// `ContactName` field by `_`
function makeFriendlyName(data) {
    data.ContactName = data.ContactName.replace(/ /g, '_');
    return data;
}


// Fill the DDL with the previous datasource
var cboSearchField1 = $("#cboSearchField1").kendoDropDownList({
    dataTextField: "ContactName",
    dataValueField: "ContactName",
    filter: "contains",
    autobind: true,
    select: cboSearchField1_Selected,
    change: cboSearchField1_onChange,
    dataSource: dataSourceSearch1
}).data("kendoDropDownList");
Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
  • Samuel, thanks. I'm trying to follow and like how you are using the .done(loadDDL);. However, I'm doing it a tad different and maybe you can point out where I need to insert the _.map. I'm referencing the datasource by it's variable where you have the _.map. Not sure that gives enough info? – ripsin Oct 28 '13 at 18:11
  • @Scott can you please detail a little bit more your code? Here I just use `_.map` in order to make a friendly name base on your rest service and send it to the datasource of the DDL as you can see in my code... – Samuel Caillerie Oct 29 '13 at 08:22
  • Thanks @Samuel, I apologize if this is not as eloquent as possible, but here you can see what I'm working with. If you have suggestions how I can make it less complicated as well, that would be awesome. Thanks for your help, looking forward to your reply. [JSFiddle link](http://jsfiddle.net/ripsin/pKFH6/) – ripsin Oct 29 '13 at 09:27
  • @Scott I have updated your [fiddle](http://jsfiddle.net/pKFH6/7/). As your REST service is local, I have changed to one for Kendo UI's demos but the principle is the same : use a template ans inside `{` and `}` you can use javascript code, so here I have replace all spaces by `_` (since there is no `_` in their data). – Samuel Caillerie Oct 29 '13 at 10:08
  • Hey that's the easiest way I've seen yet! The dropdownlists are showing no underscores when opened, but when closed the selected item still shows with the underscore. – ripsin Oct 29 '13 at 14:00
  • @Scott sorry, I have not correctly checked the result item... I have updated my answer and the fiddle : http://jsfiddle.net/pKFH6/10/. – Samuel Caillerie Oct 29 '13 at 14:38
  • No problem, I also added this same line to the dataBound: event which also handled the replace on the initial load. Thanks so much for looking this over and if you have other time I've got another [question](http://stackoverflow.com/questions/19597450/filter-local-kendo-datasource-based-on-other-attribute-of-selected-value-in-drop) outstanding related to this JSFiddle – ripsin Oct 29 '13 at 16:04