angular-xeditable icon indicating copy to clipboard operation
angular-xeditable copied to clipboard

Custom input (several fields) from xeditable demo page

Open myisis opened this issue 12 years ago • 19 comments

Is it possible to do something like the "Custom input, several fields" example from the xeditable demo page (http://vitalets.github.io/x-editable/demo-bs3.html) with the Angular version? If so, I could do with an example or some pointers for guidance... Thanks.

myisis avatar Dec 07 '13 22:12 myisis

I just found this (#47) question. I'll have a play with it and see how far I get.

myisis avatar Dec 08 '13 01:12 myisis

that would be awesome (for your already awesome library)!!!

hannesstockner avatar Dec 09 '13 18:12 hannesstockner

I can't see how it'd be possible to create a more complex input like the one from the original xeditable demo page (#47 is only a simple select). I can hook into the directive by adding an onshow method (to https://github.com/vitalets/angular-xeditable/blob/master/src/js/directives/select.js) which allows loading some data to be populated, but render pretty much messes up the elements that I specified in inputTpl as it assumes a single input element to be present. I would need to override and modify render (and maybe more). In summary, a more flexible way to generate custom inputs would be appreciated (and maybe an example demonstrating the functionality).

myisis avatar Dec 10 '13 13:12 myisis

Actually render turns out to be quite useful to modify the input. I refrain from any DOM manipulation in onshow and do this in render instead. This seems to be working (although it's not particularly pretty)... See: https://github.com/vitalets/angular-xeditable/blob/master/src/js/directives/input.js (editableRange).

myisis avatar Dec 11 '13 07:12 myisis

@myisis your last comment is quite correct. render is exactly for dom manipulation. Is you task is solved now or there is something unclear?

Sorry for missing documentation of creating custom directives. Plan to do it in future when it will be more or less fixed.

vitalets avatar Dec 13 '13 14:12 vitalets

It works quite well actually. The only remaining problem is the need for having an inputTpl defined. This is assumed to carry the input that binds to $data. However, in many circumstances there may be more than 1 DOM element involved (for instance in your "Custom input, several fields" example). The problem is that if I add a div, it gets some classes assigned which take up unnecessary screen space. I have to manually remove it in render. It'll be great if this dependency/assumption could be removed, so the user does not need to define a dummy inputTpl.

Maybe there's already another way of doing this which I wasn't aware of...

myisis avatar Dec 15 '13 02:12 myisis

Just on my last comment on removing the element defined in inputTpl, it turns out that this removes the DOM element which potentially contains an error message (in case onbeforesave returns a string). It's not the best idea to remove the element. Is there any way of doing a (pre)validation in the directive before doing a more specific validation in the application specific scope? If there isn't, what would be the best way of implementing it? My aim is to implement a simple range element which has 2 inputs (lower and upper bound). Constraint to be enforced/checked in directive: lower bound < upper bound.

Here's what I got so far:

 mod.directive('editableBounds', ['editableDirectiveFactory',
    function(editableDirectiveFactory) {
      return editableDirectiveFactory({
        directiveName: 'editableBounds',
        inputTpl: '<div></div>',
        render: function() {
          this.parent.render.call(this);
          var lowerEl = '<input class="editable-input ng-pristine ng-valid" type="number" ng-model="$data.lower">';
          var higherEl = '<input class="editable-input ng-pristine ng-valid" type="number" ng-model="$data.higher">';
          this.inputEl.before(lowerEl);
          this.inputEl.before(higherEl);
//          this.inputEl.parent().find('div').remove();
        },
        autosubmit: function() {
          var self = this;
          self.inputEl.bind('change', function() {
            self.scope.$apply(function() {
              self.scope.$form.$submit();
            });
          });
        }
      });
  }]);

Markup:

<a href='#' editable-bounds='myBound' onbeforesave='saveBound($data)'>{{ myBound }}</a>

In the controller's scope I define $scope.myBound = {lower: 0, higher: 100};

myisis avatar Dec 17 '13 03:12 myisis

hi @myisis Your last comments moved me to some thoughts and investigation. It seems to me that bounds directive should not be directly based on editableFactory. Because 2 main ideas of editable are:

  1. field by field editing (not many at once)
  2. same markup in view state and edit state

If your task fulfills it - the solution will be like this: http://jsfiddle.net/NfPcH/362/ You can pack it into simple directive without editableFactory stuff.

Another case, if you want to edit both bounds at once: Here, you want to edit complex object {lower: 0, higher: 100} that can be displayed in varity of ways. For such case normal html form is imho more suitable way. (I know that mentioned address input of x-editable implements it but currrently I'm sure it's not good sample and confusing people) So, I tryed to solve it without angular-xeditable - and it looks very nice. The only problem was angular's 2-way-binding that breaks the idea of cancel button (as changes comes instantly). I ended up with simple lazy-model directive that saves changes only when form is submited. I described it in corresponding issue on stackoverflow

Finally, second solution of your task without xeditable is: http://jsfiddle.net/H4V9D/1/

vitalets avatar Dec 18 '13 18:12 vitalets

Hmm... I read a comment of your's before in which you expressed quite a strong view favouring (1). I wonder why you think this way. From my perspective, bound is a self-contained "field". Manipulating and validating only parts of it doesn't make much sense (especially when you want to enforce lower<higher). You have this wonderful example of Custom input, several fields Moscow, Lenina st., bld. 12 at http://vitalets.github.io/x-editable/demo-bs3.html which is essentially what I try to achieve with the Angular version of xeditable. Singling out individual components of the address and validating them without the user being able to also change other fields would cause the validation procedure to unnecessarily raise an alert (let's say you'd change the city to 'London' and left the rest - an error message would raise that 'Lenina st., bld. 12' does not exist in 'London'). This seems much less intuitive than validating the whole (complex) "field" at once. With such a custom input, you could refrain from using a lazy-model as whenever you'd return from the input, the validation had already passed successfully (or the user cancelled in which case it would revert). A completely different reason why to have custom inputs could be that a widget that was able to manipulate multiple values at once (think about a slider in the case of bound or something similar).

I think angular-xeditable would greatly benefit from having means to easily implement more complex inputs... What do you think?

myisis avatar Dec 19 '13 08:12 myisis

@myisis you are absolutely right about having consolidated widget, such a blackbox, where you provide input datos and get result (that is always automatically validated and correct) In example with bounds you expect some editor ui where you provide current lower and higher values and want to get new changed values, automatically validated that lower < higher.

I agree totally. My point is that using traditional html form is more suitable for that task then editable approach as is. Please have a look on this updated fiddle: http://jsfiddle.net/H4V9D/2/ I've created my-bounds directive that has pretty simple usage:

<my-bounds lower="myBounds.lower" higher="myBounds.higher" visible="formVisible">

If you see some pitfalls here - feel free to write.

I'm not against creating custom xeditable widgets, of course :) But from my vision it should be simple building blocks relevant to standart value types (string, number, date, etc). It seems more satisfying core idea of in-place-editing: click on something and edit instantly. Complex objects requires complex forms and various validtion rules. They also should be packed into directives (and angular is awesome for that!) but these directives are on another level than xeditable ones.

vitalets avatar Dec 19 '13 19:12 vitalets

Ok. I think I understand what you mean. You would recommend xeditable for simple data types and recommend forms for more complex use cases. I guess to some extent angular has taken over some of what's been possible with the original xeditable (hence the custom input example for the non-angular xeditable version only), which is possibly why you recommend a custom angular directive as opposed to using xeditable for complex forms. I think that's a fair separation of the two very good libraries.

I've looked at your jsfiddle. Thanks a lot for that - it works quite nice. I took the liberty to change it a bit (http://jsfiddle.net/LUhTu/1/). One problem it has is that if lower >= higher -> press save -> press cancel, the value is saved regardless. I'm not sure how to get around this, as it would require the lazyModel submit handler to fire last (I think). Anyway, thanks for your comments. They were very helpful.

myisis avatar Dec 21 '13 11:12 myisis

Hi @vitalets,

I've been playing with your lazyModel and am not sure any more if I can use it in conjunction with a validation function that may reject values. There are basically two sequences in which I can see the submit events to fire ( (a) 1: myBounds.controller, 2: lazyModel.postLink or (b) 1: lazyModel.postLink, 2: myBounds.controller). In both cases (a) and (b), the utility of lazyModel is limited. Let me explain:

  • Case (a):
    • myBounds can check the value and reject if necessary; unfortunately, these are the old values, as the modified values are still kept in lazyModel.buffer (which will be executed afterwards)
  • Case (b):
    • the value will be written from buffer in lazyModel before myBounds can validate it.

I assumed that the point of lazyModel is to buffer a value and only write it back to the original scope data, once it passed validation. This would have been great, but I don't see how it could work... Any thoughts?

PS: Another thing I noticed is that intermittently the input fields don't get set with the lazyModel. Once I set priorities (myBounds.priority > lazyModel.priority), this works better (deterministically).

myisis avatar Dec 22 '13 09:12 myisis

Yes, validation is important point for lazy-model. Angular way is instant validation - so we can disable submit button when form became invalid. I've made some fixes: http://jsfiddle.net/LUhTu/2/

But we cant totally rely on disabled submit button. for that reason I've modified lazy-model to save new values only if form is $valid. http://jsfiddle.net/LUhTu/3/

Let me know if there are any questions or ideas, because lazy-model approach look very interesting for me)

vitalets avatar Dec 26 '13 14:12 vitalets

Thanks for the fixes. I've been playing a little with your jsfiddle examples and was able to find some issues.

For http://jsfiddle.net/LUhTu/2/, try the following:

  • Set min to 100
  • click cancel
  • click link again
  • => error message is still appearing, despite min showing as 99 (also button is disabled)

For http://jsfiddle.net/LUhTu/3/, try the above steps, but press ok instead (same result).

myisis avatar Dec 26 '13 22:12 myisis

Yes, we should manually call validate() when showing form. check http://jsfiddle.net/LUhTu/4/

vitalets avatar Dec 28 '13 15:12 vitalets

Hey! I like this approach and would like to use it, unfortunately I found out that it does not work with values that are updated externally.

Check http://jsfiddle.net/Wh4qP/ and click on change higher. The value is changed in the label but not in the form. I'll have a look at it myself but a hint on how to fix would be welcome ;-)

ni-ka avatar Mar 05 '14 21:03 ni-ka

I've added a watch on the parent scope and it solves the issue, but there might be a nicer solution. Check http://jsfiddle.net/Wh4qP/1/

ni-ka avatar Mar 05 '14 21:03 ni-ka

Also what would be the cleanest way to clean one field based on the change of another? I don't seem to be able to find a way to set the model value (I can read it using $scope.frm.xxx.$modelValue)

ni-ka avatar Mar 05 '14 23:03 ni-ka

Hello Team, Are there any ways to use themes for custom xeditable components? E.g. I want to create component editableTextareaLimited.

According to sources there are limited number of components to which themes are applied.

switch (this.directiveName) {
                    case 'editableText':
                    case 'editableSelect':
                    case 'editableTextarea':
                    case 'editableEmail':
                    case 'editableTel':
                    case 'editableNumber':
                    case 'editableUrl':
                    case 'editableSearch':
                    case 'editableDate':
                    case 'editableDatetime':
                    case 'editableBsdate':
                    case 'editableTime':
                    case 'editableMonth':
                    case 'editableWeek':
                        this.inputEl.addClass('form-control');
                        if (this.theme.inputClass) {
                            // don`t apply `input-sm` and `input-lg` to select multiple
                            // should be fixed in bs itself!
                            if (this.inputEl.attr('multiple') &&
                                (this.theme.inputClass === 'input-sm' || this.theme.inputClass === 'input-lg')) {
                                break;
                            }
                            this.inputEl.addClass(this.theme.inputClass);
                        }
                        break;
                    case 'editableCheckbox':
                        this.editorEl.addClass('checkbox');
                }

Thanks

asakalou avatar Jul 07 '15 11:07 asakalou