Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom value and update value in javascript #1

Open
lesonunique opened this issue Mar 3, 2016 · 5 comments
Open

Custom value and update value in javascript #1

lesonunique opened this issue Mar 3, 2016 · 5 comments

Comments

@lesonunique
Copy link

Hello,

I'm interested in your custom renderer but I just have two questions:

Is it possible to allow the user to enter a custom value (not present in the data)?

And, how can I update the values of a cell? setDataAtCell(row, col, value, source) no longer works with the custom renderer Chosen editor.

Thank you in advance.
Cordially.

Vincent LEVEQUE

@mydea
Copy link
Owner

mydea commented Mar 3, 2016

No, because this is a select box, it is not possible to add custom values. You could possibly do something like this manually and call this.$textarea.trigger("chosen:updated"); to trigger a rerender of the chosen field, but I am not sure how to do this in a nice way.

I would accept a PR adding setDataAtCell support - that would certainly be nice to have, I simply do not have the time to do it right now.

@lesonunique
Copy link
Author

I will try to create a function like setDataAtCell for Chosen.

Do you know in which direction am I looking? At what point are added element?

Thank you

@mehkar
Copy link

mehkar commented Mar 8, 2017

My solution:
there is no need to chosen.trigger:

function ChosenSelect(instance, td, row, col, prop, value, cellProperties) {
    var selectedId;
    var optionsList = cellProperties.chosenOptions.data;

//if exist get initial value -------------------
    if(saved_filter[prop])
        value=saved_filter[prop];
//---------------------------------------------

    var values = (value + "").split(",");
    var value = [];
    for (var index = 0; index < optionsList.length; index++) {
        if (values.indexOf(optionsList[index].id + "") > -1) {
            selectedId = optionsList[index].id;
            value.push(optionsList[index].label);
        }
    }
    value = value.join(", ");


    Handsontable.TextCell.renderer.apply(this, arguments);
}

@Rolok
Copy link

Rolok commented Sep 8, 2017

mydea, thanks again for having made this. I've been using it for a while now -- including using both id and value in one case. I recently had a request to allow adding values that didn't already exist. I've got something that mostly works... I modified a section of the handsontable-chosen-editor file. I'm sure you'll recognize where, from the first line of the code here.

In handsontable I pass no_results_text: 'Press Enter to add new entry:' for this column. I also put together a list of distinct values in all the columns when I first load the page, and that's the data that gets passed in.

Any/all feedback appreciated.

if(e.keyCode === Handsontable.helper.KEY_CODES.ENTER /*|| e.keyCode === Handsontable.helper.KEY_CODES.BACKSPACE*/) {
	if($(this).val()) {
		var enteredValue = $(self.TEXTAREA_PARENT).find("input").val();
		e.preventDefault(); 
		e.stopPropagation();							
		var newValueObject = [];
		newValueObject = JSON.parse('{"id":"' + enteredValue + '","label":"' + enteredValue + '"}');						
		var add = 1;
		var entryLength = 0;
		for ( i = 0; i < allRejectsObject.length; i++ ){
			// check to see if the new value is contained in an existing list.							
			entryLength = enteredValue.length;
			if (entryLength <= JSON.stringify(allRejectsObject[i].id).length){
			// the id of the allRejectsObject includes double quotes, so check for 1 rather than 0 if looking for start.
				if (JSON.stringify(allRejectsObject[i].id).toLowerCase().indexOf(enteredValue.toLowerCase()) !== -1 ){
					add = 0;
					e.preventDefault();
				}
			}
		}
		if (add == 1) {
			allRejectsObject.push(newValueObject);
			var el = null;
			el = $("<option />");
			el.attr("value", enteredValue);
			el.html(enteredValue);
			el.attr("selected", true);
			self.$textarea.append(el);
			self.close();
			self.finishEditing();
		}		
	} else {
		e.preventDefault();
		e.stopPropagation();
		self.close();
		self.finishEditing();
	}
}

@mydea
Copy link
Owner

mydea commented Sep 11, 2017

I'm sorry, but I'm currently not actively adding new features to this project. I'm willing to merge PRs though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants